1 c++ memory multithreading boost
*检测到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! */
...
}
Run Code Online (Sandbox Code Playgroud)
你能帮我吗?
我不知道这是否有助于您解决具体问题,但有时需要使用make_shared并避免使用new.
所以:
return boost::make_shared<Object>(/* any arguments to constructor here */);
Run Code Online (Sandbox Code Playgroud)
此外,你可以尝试std::shared_ptr而不是boost::shared_ptr.他们可能完全相同,但也许不是?要通过TR1使用它,我相信你#include <tr1/memory>.我通常只是通过C++ 0x使用它,在这种情况下,它会#include <memory>添加-std=c++0x到你的g ++标志中.
| 归档时间: |
|
| 查看次数: |
1417 次 |
| 最近记录: |