Why not to inherit from std::allocator

后端 未结 4 2095
天命终不由人
天命终不由人 2020-12-01 10:47

I created my own allocator like so:

template
class BasicAllocator
{
    public:
        typedef size_t size_type;
        typedef ptrdiff_t         


        
4条回答
  •  再見小時候
    2020-12-01 11:43

    Well, the destructor is not virtual. This is not a direct problem if you don't use the allocator polymorphically. But consider this case, where BasicAllocator inherits from std::allocator:

    std::allocator* ptr = new BasicAllocator();
    // ...
    delete ptr;
    

    The destructor of BasicAllocator is never called, leading to a memory leak.

提交回复
热议问题