Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8054955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:15:42+00:00 2026-06-05T08:15:42+00:00

In my custom stack allocator, I allocate a large amount of memory at when

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T08:15:43+00:00Added an answer on June 5, 2026 at 8:15 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a custom TCP stack using Python 2.6.5 on Windows 7
I'm using custom classes for my CoreData stack. Class properties are set properly in
I have stack panel with custom controls in it. I attach standard MouseDragElementBehavior to
I have stack panel with custom controls in it. User can add or remove
I have a custom UIViewController subclass, which gets pushed on a UINavigationController stack. I
the custom cursor is a sprite. using it to click another sprite no longer
I have a custom view controller that I push onto my navigation stack as
I've a Java Stack created and some custom objects added to it. These objects
I have created a custom allocator/presenter that works fine for playback of normal media
I'm making a stack based pseudo-assembler virtual machine. Here are some custom types used

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.