My overload functions are :
void *operator new(size_t size)
{
return my_alloc(size) ;
}
void operator delete(void *ptr)
{
if (ptr)
my_free(ptr) ;
}
The functions my_alloc() and my_free() are just calling malloc() and
free(), but in debug version they add size counter and special data
that are used to verify memory leak and buffer overflow.
All this works perfectly well in non-MFC projects, but when I try
linking my library in a MFC project, I get the following errors :
mylib.lib(objects.obj) : error LNK2005: "void * __cdecl operator
new(unsigned int)" (??2@YAPAXI@Z) already defined in
nafxcwd.lib(afxmem.obj)
mylib.lib(objects.obj) : error LNK2005: "void __cdecl operator
delete(void *)" (??3@YAXPAX@Z) already defined in
nafxcwd.lib(afxmem.obj)
I already tried linking libraries in different orders with no
success...
Can anyone give me advice on it ?
> All this works perfectly well in non-MFC projects, but when I try
> linking my library in a MFC project, I get the following errors :
I think it is because MFC uses preprocessor #define to kind of "overload"
operator new, or better create a lexical (preprocessor) substitution to C++
new.
In MFC wizard-generated code, you can read something like this:
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
I think you may try to change definition of MFC new, like this:
#ifdef _DEBUG
#define mfc_new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#else
#define mfc_new new
#endif
So, if you want to use MFC's overload of operator new, you must type
"mfc_new".
Giovanni
Thanks for this answer. I think this should work.
Anyway, I found a way to make my code work fine with automatically
generated code from MFC : I just built a "Base class" in my lib, and
made all my objects heriting from it. Then, the new and delete
operators are overloaded at the base class level.
This way, I have a better control on what objects are allocated
through my functions, and I can let others uses the default (global)
operators, being redirected by MFC or not...
This seems cleaner this way.
Thank you again !
Regards,
L. J.
> I have overloaded new and delete operators in a library that I use in
> every program I create. Everything is fine EXCEPT when I use my
> library in a MFC project.
>
> My overload functions are :
> void *operator new(size_t size)
> {
> return my_alloc(size) ;
> }
> void operator delete(void *ptr)
> {
> if (ptr)
> my_free(ptr) ;
> }
BTW, how about overloading new[]() and delete[]() as well?
> The functions my_alloc() and my_free() are just calling malloc() and
> free(), but in debug version they add size counter and special data
> that are used to verify memory leak and buffer overflow.
You are reinventing the wheel. Many of this is already implemented in
the CRT. You can use the _CrtCheckMemory function as a starting point in
the online documenation, then use the link "Debuig Routines" at the
bottom of the page.
> All this works perfectly well in non-MFC projects, but when I try
> linking my library in a MFC project, I get the following errors :
>
> mylib.lib(objects.obj) : error LNK2005: "void * __cdecl operator
> new(unsigned int)" (??2@YAPAXI@Z) already defined in
> nafxcwd.lib(afxmem.obj)
> mylib.lib(objects.obj) : error LNK2005: "void __cdecl operator
> delete(void *)" (??3@YAXPAX@Z) already defined in
> nafxcwd.lib(afxmem.obj)
MFC overloads new and delete with additional debug checks. These
conflict with your version. Cou can look at them in
%ProgramFiles%\Microsoft Visual Studio 8\VC\atlmfc\src\mfc\afxmem.cpp file.
I see no easy way to remove the MFC new/delete checks with your code
becasue the same file contains other global functions that will probably
pull the module into your project. You should be prepared to give up
your new/delete for MFC applications.
Try to create a single module that just contain the two functions
new/delete, that might allow the MFC version to be used without
conflicting error messages. The only way I know to force a specific
module into an application is to explicitly link the obj file.
Norbert
> Thanks for this answer.
You're welcome.
> Anyway, I found a way to make my code work fine with automatically
> generated code from MFC : I just built a "Base class" in my lib, and
> made all my objects heriting from it. Then, the new and delete
> operators are overloaded at the base class level.
This is fine. But I tought that was your intention to overload the global
new/delete operators.
Giovanni