Note that there are some explanatory texts on larger screens.

plurals
  1. POCustom allocator leaking memory
    primarykey
    data
    text
    <p>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.</p> <pre><code>template &lt;typename T&gt; 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&amp; reference; typedef const T&amp; const_reference; template &lt;typename U&gt; struct rebind { typedef Allocator&lt;U&gt; other; }; // Required Opeartions explicit Allocator(void) : Base_Allocator() { } ~Allocator(void) { } Allocator(const Allocator&amp; a) : Base_Allocator() { } // copy constructor pointer address(reference value) const { return &amp;value; } const_pointer address(const_reference value) const { return &amp;value; } size_type max_size(void) const { size_type m = 4096; return m; } pointer allocate(size_type n) { return static_cast&lt;value_type*&gt;( Base_Allocator::m_pMemMngr-&gt;Alloc( sizeof(value_type) * n) ); } void deallocate(pointer p, size_type n) { Base_Allocator::m_pMemMngr-&gt;Free(p); } void construct(pointer p, const T&amp; value) { new((T*)p) T(value); } void destroy(pointer p) { p-&gt;~T(); } bool operator == (const Allocator&amp; right) const { return true; } bool operator != (const Allocator&amp; right) const { return false; } }; </code></pre> <p>and here is the baseclass...</p> <pre><code>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-&gt;Init(); } } ~Base_Allocator(void) { m_icount--; if(m_icount&lt;0) { SAFE_DELETE(m_pMemMngr); } } }; </code></pre> <p>Here is the definition of the static members</p> <pre><code>#include "Base.h" int Base_Allocator::m_icount = 0; MemoryManager* Base_Allocator::m_pMemMngr = nullptr; </code></pre> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload