Destroy an object, but without freeing the memory?

    This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

    • Destroy an object, but without freeing the memory?

      Hello people,

      I am writing a memory pool application and I have the below problem:

      When I start, the application is allocating (by using malloc) a big amount of memory. Then, a list of char pointers point to different places of the allocated memory. When I call allocate, one of these pointers is returned.

      The problem is, when I call deallocate, I only want to explicitly call the destructor of the object and then make the memory block available again. In other words, I do not want to free the memory.

      So my deallocate looks like that:

      void Pool:: Deallocate(void* pObj)
      {
      .. Call object destructor here ..

      m_ListOfSlices.push_back((char*)pObj);
      m_AllocLeft += 1;
      }

      How can I call the destructor of the object that pObj points to?
      Keep in mind that I do not really want a template solution.

      There must be a way, since operator delete, is actually doing the same thing! I tried to debug the operator delete, but I had no luck.

      Please help!
    • There's none. void * can be anything, so you can't even assume that it's a valid C++ object and not just some POD.
      But as long as you still now the type of the object, say T, you can just call pObj->~T(); (but I guess you already knew that, because you said you don't want a template-based solution).

      Why don't you call the destructor before you call Deallocate()? And how do you call the constructor? I suppose you call Allocate() and then you use the in-place constructor/new, am I right? If that's the case you should let the caller worry about destruction, like you let her worry about construction (and you get a symmetric interface - that's good!).

      I never did it myself, but did you consider to overload operator new and delete (they could use your Pool class for memory allocation)?



      HTH,

      Turing.

      The post was edited 1 time, last by Turing ().

    • Hi Turing, and thanks loads for the response!

      Well, yeah I didn't really liked the template version... So, I did exactly what you said, I overloaded the new and delete operators. What that allowed me to do, is that when I try to delete the object, the overloaded (class specific, not global) delete operator is calling the destructor of the object implicitly, then I just have to make the memory available!

      Thus, in other words, I need not do the destruction of the object, since the overloaded delete operator does it for me!

      Regards,

      Torous
    • Originally posted by Torous
      Hi Turing, and thanks loads for the response!

      Well, yeah I didn't really liked the template version... So, I did exactly what you said, I overloaded the new and delete operators. What that allowed me to do, is that when I try to delete the object, the overloaded (class specific, not global) delete operator is calling the destructor of the object implicitly, then I just have to make the memory available!

      Thus, in other words, I need not do the destruction of the object, since the overloaded delete operator does it for me!

      Regards,

      Torous

      Glad that I could help :)
      Actually it's not the operator delete that calls the destructor but the keyword "delete" (which calls operator delete do free the memory). Yes, I know that's quite a bit confusing!



      Turing.

      The post was edited 1 time, last by Turing ().