Using Alternate Memory Heaps
-
Is there a way to tell the C++ runtime which heap to use to allocate objects with "new"? Can it be changed on the fly, or must the program be restarted? I'd like to create custom heaps to hold objects, and then destroy the heaps when no longer needed, then create new heaps to take their places. I have some legacy classes that leak memory, and I'm hoping to be able to mitigate the leaks by creating the objects in separate heaps, and then destroying the heaps after X many hours of run time.
The difficult we do right away... ...the impossible takes slightly longer.
-
Is there a way to tell the C++ runtime which heap to use to allocate objects with "new"? Can it be changed on the fly, or must the program be restarted? I'd like to create custom heaps to hold objects, and then destroy the heaps when no longer needed, then create new heaps to take their places. I have some legacy classes that leak memory, and I'm hoping to be able to mitigate the leaks by creating the objects in separate heaps, and then destroying the heaps after X many hours of run time.
The difficult we do right away... ...the impossible takes slightly longer.
-
Is there a way to tell the C++ runtime which heap to use to allocate objects with "new"? Can it be changed on the fly, or must the program be restarted? I'd like to create custom heaps to hold objects, and then destroy the heaps when no longer needed, then create new heaps to take their places. I have some legacy classes that leak memory, and I'm hoping to be able to mitigate the leaks by creating the objects in separate heaps, and then destroying the heaps after X many hours of run time.
The difficult we do right away... ...the impossible takes slightly longer.
The "canonical" way to do this is with class-specific new / delete:
class Foo
{
public:
void* operator new(size_t blockSize)
{
// allocate 'blockSize' and return it, or throw std::bad_alloc (defined in )
}void operator delete(void\* p) { // release 'p'. Note that p == nullptr is allowed! } /\* rest of class \*/
};
As long as you use the same heap for both allocation and deletion, things should work fine. As Richard MacCutchan says, a quick-and-dirty way to do this in Windows is by using the Windows heap APIs.
Freedom is the freedom to say that two plus two make four. If that is granted, all else follows. -- 6079 Smith W.
-
The "canonical" way to do this is with class-specific new / delete:
class Foo
{
public:
void* operator new(size_t blockSize)
{
// allocate 'blockSize' and return it, or throw std::bad_alloc (defined in )
}void operator delete(void\* p) { // release 'p'. Note that p == nullptr is allowed! } /\* rest of class \*/
};
As long as you use the same heap for both allocation and deletion, things should work fine. As Richard MacCutchan says, a quick-and-dirty way to do this in Windows is by using the Windows heap APIs.
Freedom is the freedom to say that two plus two make four. If that is granted, all else follows. -- 6079 Smith W.
Thank you Daniel. Does this solution mean that I must call the constructor myself?
The difficult we do right away... ...the impossible takes slightly longer.
-
Thank you Daniel. Does this solution mean that I must call the constructor myself?
The difficult we do right away... ...the impossible takes slightly longer.
No. When constructing an object using new (e.g. Foo* p = new Foo()), the compiler chooses the new operator to be called as follows: 1. A class-specific operator new() (if present) 2. The global operator new It is the same for the delete operator. You may also replace the global new and delete operators with your own code:
// place outside any namespace!
void* operator new(size_t blockSize)
{
// allocate 'blockSize' and return it, or throw std::bad_alloc (defined in )
}void operator delete(void* p)
{
// release 'p'. Note that p == nullptr is allowed!
}Note that operators new and delete deal only with memory allocation/release. The compiler still calls the class' constructor to turn the raw memory into a class instance, and the destructor in order to turn a class instance back into raw memory.
Freedom is the freedom to say that two plus two make four. If that is granted, all else follows. -- 6079 Smith W.
-
No. When constructing an object using new (e.g. Foo* p = new Foo()), the compiler chooses the new operator to be called as follows: 1. A class-specific operator new() (if present) 2. The global operator new It is the same for the delete operator. You may also replace the global new and delete operators with your own code:
// place outside any namespace!
void* operator new(size_t blockSize)
{
// allocate 'blockSize' and return it, or throw std::bad_alloc (defined in )
}void operator delete(void* p)
{
// release 'p'. Note that p == nullptr is allowed!
}Note that operators new and delete deal only with memory allocation/release. The compiler still calls the class' constructor to turn the raw memory into a class instance, and the destructor in order to turn a class instance back into raw memory.
Freedom is the freedom to say that two plus two make four. If that is granted, all else follows. -- 6079 Smith W.
Thank you. :)
The difficult we do right away... ...the impossible takes slightly longer.