Click here to Skip to main content
15,915,093 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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:
C++
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!

Revised:::
Thanks very much for your answers,

To answer answer 1, I am afraid you can actually do that. You can actually do pMem->~A(); and then call delete pMem;

To question answer2, I have read the parashift tutorials long ago, the problem is that they do not apply to my situation at all ;) . I am trying to do a different thing. Thanks very much for the reply though :)
Posted
Updated 11-Feb-10 6:54am
v3

Why don't you simply provide a clear() method in your object that takes care of cleaning your object ?

By the way, your design of the pool doesn't look very C++ to me: first you are calling malloc (which is pure C) and then you are manipulating void pointers and casting them to char pointers... It really seems to be a mess.
In general, when you want to have a pool, it is mainly used to store objects of the same type. So in that case there's no need for all these ugly stuff you are doing.

What are your requirements in fact ? What are you trying to do precisely ?
 
Share this answer
 
Lets go for steps:

  • Allocate without contruct: just use malloc, or cast the return of a new char array.
    A* pA;
    pA = (A*)new char[sizeof(A)];

  • Construct what is rawly allocate: use placement new:
    new(pA)A(...);

  • Destroy without deallocate: explicitly call the destructor:
    pA->~A();

  • Deallocate what has already been destroyed: use free or free the char array as you allocated it
    delete[] (char*)pA; pA = 0;

  • Allocate and construct: just use new
    pA = new A(...);

  • Destroy and deallocate: just use delete
    delete pA; pA = 0;

This is what the STL implementation does inside std::allocator.
 
Share this answer
 
You may call it, but you shouldn't. See here [^] for details.
:)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900