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

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

## Recommended Posts

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!

##### Share on other sites
Quote:
 Original post by MauManAfter a little stepping through the debugger I found that a pooled new in #include was being called in the STL at times. However when I overrode it I got a compiler error that it was already defined in . However, when I remove the reference to 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.

##### Share on other sites
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?

##### Share on other sites
Quote:
 Original post by MauManSo 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]

##### Share on other sites
Quote:
 Original post by MauManAgain 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.

##### Share on other sites
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.

##### Share on other sites
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.

1. 1
2. 2
3. 3
Rutin
22
4. 4
frob
17
5. 5

• 9
• 33
• 13
• 13
• 10
• ### Forum Statistics

• Total Topics
632580
• Total Posts
3007194

×