简体   繁体   中英

Custom allocator leaking memory

So the implementation of my custom allocator has a base class with 2 static variables, one to track the instances of allocators and one is the memory-pool.

template <typename T>
class Allocator : public Base_Allocator
{

public:
  // Required types
  typedef T           value_type;
  typedef size_t      size_type;
  typedef ptrdiff_t   difference_type;
  typedef T*          pointer;
  typedef const T*    const_pointer;
  typedef T&          reference;
  typedef const T&    const_reference;

  template <typename U>
  struct rebind
  {
    typedef Allocator<U> other;
  };

  // Required Opeartions

  explicit Allocator(void) : Base_Allocator() 
  { 
  }
  ~Allocator(void) { }

  Allocator(const Allocator& a) : Base_Allocator()
  {

  } // copy constructor

  pointer address(reference value) const { return &value; }

  const_pointer address(const_reference value) const { return &value; }

  size_type max_size(void) const { size_type m = 4096; return m; }

  pointer allocate(size_type n) 
  {
    return static_cast<value_type*>( Base_Allocator::m_pMemMngr->Alloc( sizeof(value_type) * n) );
  }

  void deallocate(pointer p, size_type n) {
    Base_Allocator::m_pMemMngr->Free(p);
  }

  void construct(pointer p, const T& value) {
    new((T*)p) T(value);
  }

  void destroy(pointer p) { 
    p->~T();
  }

  bool operator == (const Allocator& right) const { return true; }

  bool operator != (const Allocator& right) const { return false; }

};

and here is the baseclass...

class Base_Allocator
{
public:
  static int m_icount;
  static MemoryManager* m_pMemMngr;  

public:
  Base_Allocator(void)
  {
    m_icount++;
    if(!m_pMemMngr)
    {
      NEW(m_pMemMngr);
      m_pMemMngr->Init();
    }

  }

  ~Base_Allocator(void)
  {
    m_icount--;
    if(m_icount<0)
    {
      SAFE_DELETE(m_pMemMngr);
    }
  }

};

Here is the definition of the static members

#include "Base.h"

int Base_Allocator::m_icount = 0;
MemoryManager* Base_Allocator::m_pMemMngr = nullptr;

my thing here is that the memory is never being released. I'm passing it to a forward_list and this forward list creates 3 allocators but it also deletes 3. That's the reason I have the base class only free the memory once it is less than 0. But things haven't really worked out too well. -1 is never reached so I'm never releasing the memory in the memorypools. Any ideas would be appreciated.

from effective STL

Make your allocator a template, with the template parameter T representing the type of objects for which you are allocating memory.

Satisfied

Provide the typedefs pointer and reference, but always have pointer be T* and reference be T&.

Satisfied

Never give your allocators per-object state. In general, allocators should have no nonstatic data members.

Not Satisfied, you have private member in ur allocator!

Remember that an allocator's allocate member functions are passed the number of objects for which memory is required, not the number of bytes needed. Also remember that these functions return T* pointers Ma the pointer typedef), even though no T objects have yet been constructed.

Satisfied

Be sure to provide the nested rebind template on which standard containers depend.

Satisfied

so remove ur private member...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM