Sign in to follow this  

Overriding new and delete.

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

Greets, Would someone happen to know how to override global operators new and delete without getting an "already defined" linker error? At the moment, I have this: Main.cpp

#include <windows.h>
#include <crtdbg.h>



void* operator new(size_t size)
{
	MessageBox(NULL, "My New!", NULL, 0);
	return malloc(size);
}

void operator delete(void* p)
{
	MessageBox(NULL, "My Delete!", NULL, 0);
	free(p);
}



int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nShowCmd)
{
	int* temp = new int;

	delete temp;

	return 0;
}



Thanks! EDIT: I'm using VC++.NET 2003

Share this post


Link to post
Share on other sites
Because your versions of new and delete were declared in the global namespace they have a greater chance of causing link conflicts with the runtime libs. In your case it the signatures of both new and delete match the implementations of new and delete in the runtime.

When you changed project types chances are different libs were included that did not contain new and delete with the same signatures. In some cases this is quite common between debug and release (and multithreaded vs. single thread) libs. The reason is that depending on which windows headers get included 'new' and 'delete' might actually be changed via #define. This becomes less obvious and even harder to track down. Since you are using VS right click on 'new(...)' and select the option to go to it's definition. You might find that it's actually a #define.

Depending on who you talk to you'll get recommendations for using the following approaches:

1. declare your new/delete operators in a namespace. Include 'using namespace' any time you want them used.

2. declare your new/delete operators in a class (i.e. 'Object') and have all classes requiring the custom memory handlers inherit from it.

3. Use a seemingly unique argument signature for new/delete to avoid conflicts and use #define for specific/seemless use. MFC apps do this with the DEBUG_NEW macro.


Each of those suggestions work great for different situations and I don't consider any of them to be the absolute right (or wrong) way.

Share this post


Link to post
Share on other sites
Quote:
Original post by Wavarian

void* operator new(size_t size)
{
MessageBox(NULL, "My New!", NULL, 0);
return malloc(size);
}

void operator delete(void* p)
{
MessageBox(NULL, "My Delete!", NULL, 0);
free(p);
}



This doesn't look like overloadding, it looks like your trying to redefine, also it looks like your trying redefine the nothrow versions of global operators new/delete in which case your definitions are wrong plus operator delete should never throw exceptions so it should have a no throw exception specification. You need to include header new, with all that in mind:


#include <cstddef> // size_t
#include <cstdlib> // malloc
#include <new> // new/delete


void* operator new(std::size_t size) throw() {
return std::malloc(size);
}

void operator delete(void* p) throw() {
std::free(p);
}

Share this post


Link to post
Share on other sites

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