Sign in to follow this  

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

This topic is 4856 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

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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
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 this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
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 this post


Link to post
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.

Sorry for the bad thread.

Share this post


Link to post
Share on other sites

This topic is 4856 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this