Jump to content
  • Advertisement
Sign in to follow this  
schupf

operator new/delete

This topic is 2607 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello,

I have overloaded the global operator new() to write some debug infos about each allocated memory block into a std::map:
void * operator new (size_t size, A& a, B& b, C& c, D& d); {

// write entry into a map and allocate memory with malloc()

}


void operator delete (void* p, A& a, B& b, C& c, D& d); {

// search entry in map and delete if found. Then delete memory with free()

}



...
A a;
B b;
C c;
D d;

int* p = new(a,b,c,d) int; // this calls my operator new with 5 parameters
delete p; // this does NOT call my operator delete with 5 parameters
[/quote]

Unfortunately delete p does not call my overloaded operator delete. What could I do that my operator delete() is called so I can remove the entry from the map?


Share this post


Link to post
Share on other sites
Advertisement


void operator delete (void* p, A& a, B& b, C& c, D& d); {

// search entry in map and delete if found. Then delete memory with free()

}

...
A a;
B b;
C c;
D d;

int* p = new(a,b,c,d) int; // this calls my operator new with 5 parameters
delete p; // this does NOT call my operator delete with 5 parameters


How can it, when you don't give it any parameters? You'd need to do:


operator delete(p, a, b, c, d);

Share this post


Link to post
Share on other sites

[quote name='schupf' timestamp='1304246771' post='4805004']
void operator delete (void* p, A& a, B& b, C& c, D& d); {

// search entry in map and delete if found. Then delete memory with free()

}

...
A a;
B b;
C c;
D d;

int* p = new(a,b,c,d) int; // this calls my operator new with 5 parameters
delete p; // this does NOT call my operator delete with 5 parameters


How can it, when you don't give it any parameters? You'd need to do:


operator delete(p, a, b, c, d);

[/quote]

Thats not the same. That won't call the destructor. And I definitely don't want to replace every delete p with p->~T(); operator delete(p, a, b, c, d);


Share this post


Link to post
Share on other sites

Thats not the same. That won't call the destructor.

Quite true.


And I definitely don't want to replace every delete p with p->~T(); operator delete(p, a, b, c, d);
[/quote]
Then you're out of luck without modifying your existing system :(

Share this post


Link to post
Share on other sites

I have overloaded the global operator new() to write some debug infos about each allocated memory block into a std::map:


Are you making sure to use a different allocator for the map?
By default the map will probably use your overloaded operator new, which can cause it to crash if you insert into it from within operator new. (Though if you only use your special operator new with the other arguments I guess that's not a problem).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!