Search code examples
c++stlcontainersshared-ptrallocator

C/C++ custom allocator memory leak


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.


Solution

  • 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.