overriding the pooled new in C++ (specifically Microsoft C++ 7.1 (VS .net 2003))

Started by
5 comments, last by MauMan 19 years, 7 months ago
I've been debugging some memory problems in some code at work where Microsoft's CRT memory checkers did not help. So I overloaded the global operators new, new[], delete and delete[] with versions that would do things like writing/checking pad words on allocated memory and other things that might be useful. It worked nicely for the main test case where I was seeing some problems and I quickly found our bugs. Deciding that this would be a nice thing to keep in our library I ran it against all of our test cases and found some deletes that were not matched by any of my news. After a little stepping through the debugger I found that a pooled new in #include <new> was being called in the STL at times. However when I overrode it I got a compiler error that it was already defined in <new>. However, when I remove the reference to <new> in my code I lose the definition of std::bad_alloc() which I need to properly override global new. I did some googling on the subject to no avail but perhaps I did not use the proper terms. Have any of you encountered this? If so how did you overcome the problem? An example implementation would be great! Thanks!
---CyberbrineDreamsSuspected implementation of the Windows idle loop: void idle_loop() { *((char*)rand()) = 0; }
Advertisement
Quote:Original post by MauMan
After a little stepping through the debugger I found that a pooled new in #include <new> was being called in the STL at times. However when I overrode it I got a compiler error that it was already defined in <new>. However, when I remove the reference to <new> in my code I lose the definition of std::bad_alloc() which I need to properly override global new.


pooled new? or do you mean placement new? Anyways from what i remember you have to write your own allocator type that uses your versions of new & delete.
Ahh placement new! I had the wrong term. I'll google on that and see if it helps. Thanks for that!

Again the problem is that I cannot figure out how to override the placement new. So in the STL vector class placement new is being called to allocate and later my delete is being called on the pointer returned by the placement new. Since it did not go through any of my overrides of new it looks like a bad delete.

So back to the original but restated question: how do you properly override placement new in Microsoft C++ 7.1?

---CyberbrineDreamsSuspected implementation of the Windows idle loop: void idle_loop() { *((char*)rand()) = 0; }
Quote:Original post by MauMan
So back to the original but restated question: how do you properly override placement new in Microsoft C++ 7.1?


You can't override free functions but you can certainly overload them.

The normal new & delete operators are just function prototypes so you don't override it you just give it a different implementation from the default when you define it.

Where as placement new & delete are not function prototypes, there declared & defined in header new so you can't replace them.

EDIT: I think i should explain the purpose of placement new, its just used with uninitialized memory, its way to call the constructor to intialize the memory. Thats why you can't replace them it wouldn't make sense if you could.

[Edited by - snk_kid on August 27, 2004 12:20:18 PM]
Quote:Original post by MauMan
Again the problem is that I cannot figure out how to override the placement new. So in the STL vector class placement new is being called to allocate and later my delete is being called on the pointer returned by the placement new. Since it did not go through any of my overrides of new it looks like a bad delete.


Write a custom allocator.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
if you could post your version of new & delete one of us could write you an allocator type to use with STL that uses them, i'm sure it wouldn't take that long.
I looks like the real problem was a corrupted install of VS.Net. I was seeing funny things in the debugger that were of the "this really cannot be happening" variety. I was able to reduce it to a case that should not be failing that would blow up in delete with Microsoft's or my version of new/delete. The following would blow up in delete on "b.push_back( 2 );":

#include <vector>int main( int, char** ){   std::vector<int> a;   std::vector<int> b;   a.push_back( 1 );   a.push_back( 2 );   a.push_back( 3 );   a.push_back( 4 );   a.push_back( 5 );   a.push_back( 6 );   a.push_back( 7 );   a.push_back( 8 );   a.push_back( 9 );   a.push_back( 10 );   a.push_back( 11 );   a.push_back( 12 );   a.push_back( 13 );   a.push_back( 14 );   a.push_back( 15 );   a.push_back( 16 );   b.push_back( 1 );   b.push_back( 2 );   b.push_back( 3 );   b.push_back( 4 );   return 0;}


I took the code home and built it on my home machine. It would fail until I rebuilt it then it worked fine. I tried it on several other machines and the same thing. I de/re-installed VS 2003 on my work machine and the problem went away.

The odd thing is that I cannot figure out what was bad on my work machine. I thought I might have accidentally modified one of the sytem headers but a date check did not find anything amiss.

Sorry for the bad thread.
---CyberbrineDreamsSuspected implementation of the Windows idle loop: void idle_loop() { *((char*)rand()) = 0; }

This topic is closed to new replies.

Advertisement