[英]shared_ptr<EVP_PKEY> with EVP_PKEY_free as custom deleter causes heap corruption
基本上,這就是我所擁有的:
{
shared_ptr<EVP_PKEY> evpKeyPtr{EVP_PKEY_new(), EVP_PKEY_free};
//get key from file
}
這會導致堆損壞。 以及:
shared_ptr<EVP_PKEY> evpKeyPtr{EVP_PKEY_new(), EVP_PKEY_free};
//same code as above to get key from file
EVP_PKEY_free(evpKeyPtr.get());
//evpKeyPtr does not go out of scope before the corruption occurs
但這不是:
EVP_PKEY* evpKey = EVP_PKEY_new();
//same code as above to get key from file
EVP_PKEY_free(evpKey);
我不知道為什么會發生這種情況。 我知道不建議使用第二個代碼,但是我這樣做是為了測試會發生什么。
您只是獲得了指針,但是shared_ptr一直是它的所有者。 它將在作用域末尾刪除兩次。
如果您希望能夠獲得所有權,請使用unique_ptr(它具有release()方法)。
我只是意識到自己在做錯什么,(幾乎)與std::shared_ptr
沒有任何關系。 我試圖以一種錯誤的方式使用指向std::shared_ptr
的基礎指針的臨時目錄。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.