overloading new/delete

Michael Pyne mpyne at purinchu.net
Thu Mar 8 22:03:39 GMT 2007


On Thursday 08 March 2007 14:03, Christian Ehrlicher wrote:
> Hi,
>
> overloading new/delete seems to be different for msvc than for mingw.
> For mingw I need to use new/delete this way:
>
> void* operator new(size_t size, int myVar);
> void operator delete(void* ptr, size_t size);
>
> but msvc (http://msdn2.microsoft.com/en-us/library/cxdxz3x6.aspx) needs
> this:
>
> void* operator new(size_t size, int myVar);
> void operator delete(void* ptr, int myVar);
>
>
> Is this correct? Any idea how to fix this (for example in
> khtml/rendering/render_object.cpp/.h)?

It looks like the MSVC way is correct.  See the C++ FAQ, part 11.14: 
http://www.parashift.com/c++-faq-lite/dtors.html#faq-11.14

In the section that talks about solving the fact that memory will leak with 
placement new if an exception is thrown, it tells you to define a 
corresponding delete operator, with the same signature as the operator new, 
except that the size_t is not included.  (Think about it, what deallocation 
functions ever need the size of the allocated block? ;)

i.e.            void* operator new    (size_t nbytes, MyType value);
corresponds to  void  operator delete (void*  ptr,    MyType value);

So here, if you're correct about mingw's use of operator new and delete then 
mingw has a bug.  You could possibly use #ifdef'ed macros or something to 
work around it but are you sure that mingw has that bug?

Looking at render_object.h, the only operator new which is available to run 
is:
void* operator new(size_t sz, RenderArena *renderArena) throw();

There is no corresponding operator delete.  But this is only a problem 
(according to both MSDN and the C++ FAQ) if the constructor can throw an 
exception, and this one is marked as not throwing any.  So although defining 
an operator delete appropriately would rid us of a compiler warning, I don't 
see the next to risk breaking things which seem to be working.

But you're complaining about problems with overloading.  What problems are you 
having exactly?

Regards,
 - Michael Pyne
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
URL: <http://mail.kde.org/pipermail/kde-core-devel/attachments/20070308/95c7fe9b/attachment.sig>


More information about the kde-core-devel mailing list