This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
operator new and delete overloading problems.
- To: <gcc-bugs at gcc dot gnu dot org>
- Subject: operator new and delete overloading problems.
- From: "Bas Vodde" <basv at medialab dot lostboys dot nl>
- Date: Fri, 14 Jan 2000 18:33:01 +0100
Hello,
I've been implementing a memory leak detector and out of bounds checker for
a project I'm working on.
While developing I had a few problems with the operator new and delete
overloads. It looks like bugs in the
compiler. I'm compiling with the latest gcc compiler version 2.95.2. I wrote
2 simpel C++ files which
contain the problems: (I also included the files as attachment)
Also got a question (possibly a feature request) which I describe at the
end.
Problem 1:
When overloading operator new in global space and also in a namespace the
assembler gets allready defined
symbol of building_new. I excluded a lot of info to make the example small.
The same problem happens when
overloading operator new[], operator delete and operator delete [].
Code:
namespace Foo
{
void* operator new (size_t inSize);
};
void* operator new (size_t inSize) {return Foo::operator new (inSize);}
void* Foo::operator new (size_t inSize) {printf ("new"); return NULL;}
Problem 2:
I can still use the global overloads for operator new using a workaround.
This problem only occurs when
using a shared library. I create a header file with an inline global
operator new that just forwards the new request
to the Foo::operator new (Foo is a namespace). The Foo::operator new is
implemented in a cpp file and just
does a malloc. The second cpp file just includes the header file with the
operator new overloaded in and does
a new and a delete. When compiling the implementation of the Foo::operator
new as a shared library the
a.out will get a stack overflow on the buildin_new. (recursive call). When I
just compile them in the same a.out
then it works fine.
Code:
foo.h
#include <sys/types.h>
namespace Foo
{
void* operator new (size_t inSize);
};
#ifndef __INHEADER
inline void* operator new (size_t inSize) {return Foo::operator new
(inSize);}
#endif
foo.C
#define __INHEADER
#include "foo.h"
#undef __INHEADER
#include <stdio.h>
#include <malloc.h>
void* Foo::operator new (size_t inSize) {printf ("new"); return malloc
(inSize);}
main.C
#include "bug2.h"
int main ()
{
int* p = new int;
delete p;
return 1;
}
If I compile this with
g++ foo.C main.C
the output wil just be a working a.out (printing "new")
if I compile this with
g++ -shared ./foo.C -o ./foo.so
g++ ./main.C ./foo.so
then I will get the recursive buildin_new
Question:
I'd like to call a function before all object are created (also the one in
global space) and one after all the objects
are destroyed (also the one in global space). On some (non linux) compilers
this can be easily done by a #pragma
or by creating an own entry point and calling the normal entry point from
there. I wonder is there is a clean way to
do one of these things with gcc. If not then it would be a nice feature.
Thanks.
Bas Vodde
Software Developer
Lost Boys Medialab.