[英]Memory corruption when using boost::shared_ptr in a multithreaded environment
*檢測到glibc * malloc():內存損壞(快速):***
這是在多線程環境中執行以下部分代碼時遇到的錯誤:
/// Some declarations
typedef boost::shared_ptr<Object> ObjectPtr;
ObjectPtr getObject()
{
return ObjectPtr(new Object);
}
/// What is actually executed in a thread
void executeWork()
{
...
ObjectPtr object = getObject(); /* <--- Memory corruption here! */
...
}
你能幫我嗎?
我不知道這是否對您的特定問題有所幫助,但有時最好使用make_shared
並避免使用new
。
所以:
return boost::make_shared<Object>(/* any arguments to constructor here */);
另外,您可以嘗試使用std::shared_ptr
而不是boost::shared_ptr
。 它們可能完全相同,但是可能不一樣嗎? 要通過TR1使用它,我相信您#include <tr1/memory>
。 我通常只是通過C ++ 0x使用它,在這種情況下,它是#include <memory>
並將-std=c++0x
添加到您的g ++標志中。
對象可能是一個問題。 如果將Object更改為int,會發生什么?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.