C/C++自定义分配器内存泄漏
我创建一个自定义内存分配器,如下所示:
class pool_allocator
{
// required methods
// ...
private:
boost::shared_ptr<MemoryChunks> m_t;
};
该分配器的目的是共享不同容器分配的内存,并且仅在所有容器被删除时才释放它们。因此,我使用 boost::shared_ptr
。
但是在VS2008中运行后,我检测到内存泄漏。我不知道为什么。
如果我将 boost::shared_ptr
更改为 MemoryChunks
,内存泄漏就会消失。
I create a custom memory allocator like following:
class pool_allocator
{
// required methods
// ...
private:
boost::shared_ptr<MemoryChunks> m_t;
};
The purpose of this allocator is to share memory allocating by different container and only deallocate them when all container and deleted. therefore, I use boost::shared_ptr
.
However after running it in VS2008, I detect a memory leak. I don't know why.
If I change boost::shared_ptr
to MemoryChunks
, the memory leak goes away.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否知道分配器在 C++03 中被视为无状态?尝试将分配器与 Boost.Containers 结合使用(它刚刚被接受,但我认为它们已经是 Boost.Interprocess 的一部分),它尊重分配器。如果没有
MemoryChunks
的定义,就很难说别的了。Are you aware that allocators are treated as stateless in C++03? Try using your allocator in conjuntion with Boost.Containers (It was just accepted, but I think they are already part of Boost.Interprocess), which respects allocators. Not easy to say anything else without the definition for
MemoryChunks
.