简体   繁体   中英

Freeing memory in a custom stack allocator

In my custom stack allocator, I allocate a large amount of memory at when the program launches using malloc() , then at program shutdown I free() all the memory allocated.

So basically it looks like this:

//start up
m_pInitialPosition = malloc(STACK_SIZE);

//shutdown
free(m_pInitilaPosition);

When I need to create a new object I call allocateNew() :

 Actor* pActor = getStackAllocator().allocateNew<Actor>();
 *pActor = Actor();

This is what allocateNew() looks like:

template <class T>
T* allocateNew()
{
   //allocate returns void*
   return new (allocate(sizeof(T), __alignof(T))) T;
}

The problem occurs (_BLOCK_TYPE_IS_VALID(pHead->nBlockUse exception) if I call:

delete pActor;

If I simply remove that line the problem disappears and there are no memory leaks because the I still call free() in the stack allocator, but the destructor of Actor is not called...

So what can I change to ensure that the destructor is called?

When you call delete , the object pointed by pActor will be destructed and the memory will be freed using the default deallocator (could be free()), not yours. Since the object has been allocated by your allocator, it cannot work.

Define a new method to handle object deletion like this one :

template<class T> void deleteObject(T *obj) {
   if (obj!=nullptr) {  // do nothing is obj is null
      obj->~T(); // call the object's destructor
      deallocate(obj);  // your method to handle deallocation in your 'memory pool'
   }
}

And replace your delete by :

 getStackAllocator().deleteObject(pActor);

This article explains how to make a custom memory allocator : http://bitsquid.blogspot.fr/2010/09/custom-memory-allocation-in-c.html


Moreover, I have a doubt about your code. What are these lines supposed to do ?

Actor* pActor = getStackAllocator().allocateNew<Actor>();
*pActor = Actor();

1- If allocateNew() performs both allocation & default construction, it is ok but the second line is useless ? 2- If allocateNew() only performs allocation without initializing the object, the second line is wrong : it calls the assignment operator on an uninitialized object.

delete pActor is not the appropriate complement to placement new . To "undo" the object construction new (allocate(sizeof(T), __alignof(T))) T you need to call the destructor only, not use delete which will also try to deallocate memory at the location pointer to by pActor

Eg

pActor->~Actor();

You should probably wrap this up in a member function of your stack allocator class so that it's clients don't have to magically know how to deallocate objects that you give them and to give you the freedom to update your allocator's implementation.

Eg

template <class T>
void deallocate(T* t)
{
    t->~T();
}

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