public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/29164]  New: Overloaded operator delete[] doesn't get called
@ 2006-09-21 10:44 Andreas dot Kowarz at tu-dresden dot de
  2006-09-21 10:47 ` [Bug c++/29164] " Andreas dot Kowarz at tu-dresden dot de
                   ` (9 more replies)
  0 siblings, 10 replies; 11+ messages in thread
From: Andreas dot Kowarz at tu-dresden dot de @ 2006-09-21 10:44 UTC (permalink / raw)
  To: gcc-bugs

Hi,

recently I wrote the tiny program attached below to test overloading of new and
delete. Unfortunately, the overloaded delete[] operator does not get called.
:-(

System: Linux HOSTNAME 2.6.17-1.2187_FC5 #1 SMP Mon Sep 11 01:16:59 EDT 2006
x86_64 x86_64 x86_64 GNU/Linux
g++: g++ (GCC) 4.1.1 20060525 (Red Hat 4.1.1-1)
     g++32 (GCC) 3.2.3 20030502 (Red Hat Linux 3.2.3-55.fc5)

Regards,

Andreas Kowarz

=================================================================

#include <iostream>
using namespace std;

class myclass {
    public:
        static void * operator new (std::size_t size) throw() {
            cout << "myclass new operator\n";
            return NULL;
        }
        static void * operator new[] (std::size_t size) throw() {
            cout << "myclass new[] operator\n";
            return NULL;
        }
        static void operator delete (void *p) throw() {
            cout << "myclass delete operator\n";
        }
        static void operator delete[] (void *p) throw() {
            cout << "myclass delete[] operator\n";
        }
};

int main() {
    myclass *ad;

    ad = new myclass;
    ad = new myclass[10];
    delete[] ad;
    delete ad;

    return 0;
}

=================================================================

Output:
myclass new operator
myclass new[] operator
myclass delete operator


-- 
           Summary: Overloaded operator delete[] doesn't get called
           Product: gcc
           Version: 4.1.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: Andreas dot Kowarz at tu-dresden dot de


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=29164


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug c++/29164] Overloaded operator delete[] doesn't get called
  2006-09-21 10:44 [Bug c++/29164] New: Overloaded operator delete[] doesn't get called Andreas dot Kowarz at tu-dresden dot de
@ 2006-09-21 10:47 ` Andreas dot Kowarz at tu-dresden dot de
  2006-09-21 11:25 ` rguenth at gcc dot gnu dot org
                   ` (8 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: Andreas dot Kowarz at tu-dresden dot de @ 2006-09-21 10:47 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from Andreas dot Kowarz at tu-dresden dot de  2006-09-21 10:47 -------
*** Bug 29163 has been marked as a duplicate of this bug. ***


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=29164


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug c++/29164] Overloaded operator delete[] doesn't get called
  2006-09-21 10:44 [Bug c++/29164] New: Overloaded operator delete[] doesn't get called Andreas dot Kowarz at tu-dresden dot de
  2006-09-21 10:47 ` [Bug c++/29164] " Andreas dot Kowarz at tu-dresden dot de
@ 2006-09-21 11:25 ` rguenth at gcc dot gnu dot org
  2006-09-21 12:00 ` Andreas dot Kowarz at tu-dresden dot de
                   ` (7 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2006-09-21 11:25 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from rguenth at gcc dot gnu dot org  2006-09-21 11:25 -------
EDG ends up not even calling the overloaded delete.  Looking at the std I can
see no reason why your program should not use the overloaded deletes.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=29164


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug c++/29164] Overloaded operator delete[] doesn't get called
  2006-09-21 10:44 [Bug c++/29164] New: Overloaded operator delete[] doesn't get called Andreas dot Kowarz at tu-dresden dot de
  2006-09-21 10:47 ` [Bug c++/29164] " Andreas dot Kowarz at tu-dresden dot de
  2006-09-21 11:25 ` rguenth at gcc dot gnu dot org
@ 2006-09-21 12:00 ` Andreas dot Kowarz at tu-dresden dot de
  2006-10-11  3:43 ` bangerth at dealii dot org
                   ` (6 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: Andreas dot Kowarz at tu-dresden dot de @ 2006-09-21 12:00 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from Andreas dot Kowarz at tu-dresden dot de  2006-09-21 11:59 -------
(In reply to comment #2)
> EDG ends up not even calling the overloaded delete.  Looking at the std I can
> see no reason why your program should not use the overloaded deletes.
> 

For the moment, I can work around the bug by calling the operator directly,
e.g. myclass::operator delete[] (ad)    --- (in form of a preprocessor macro).
Seems, I should provide macros for all of the four operators since my source
code is intended to be compiled with other compilers, too. :-)


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=29164


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug c++/29164] Overloaded operator delete[] doesn't get called
  2006-09-21 10:44 [Bug c++/29164] New: Overloaded operator delete[] doesn't get called Andreas dot Kowarz at tu-dresden dot de
                   ` (2 preceding siblings ...)
  2006-09-21 12:00 ` Andreas dot Kowarz at tu-dresden dot de
@ 2006-10-11  3:43 ` bangerth at dealii dot org
  2007-03-08 21:49 ` fang at csl dot cornell dot edu
                   ` (5 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: bangerth at dealii dot org @ 2006-10-11  3:43 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #4 from bangerth at dealii dot org  2006-10-11 03:43 -------
Confirmed. 12.5/4 reads to me as if myclass::operator delete[] should be
called. Indeed icc doesn't call either user defined operator in the
array case. I think that's just a convergence of bugs, though.

This appears to have been broken since at least 2.95.

W.


-- 

bangerth at dealii dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bangerth at dealii dot org
             Status|UNCONFIRMED                 |NEW
     Ever Confirmed|0                           |1
           Keywords|                            |wrong-code
      Known to fail|                            |2.95.3 3.3.6 4.2.0
   Last reconfirmed|0000-00-00 00:00:00         |2006-10-11 03:43:06
               date|                            |


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=29164


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug c++/29164] Overloaded operator delete[] doesn't get called
  2006-09-21 10:44 [Bug c++/29164] New: Overloaded operator delete[] doesn't get called Andreas dot Kowarz at tu-dresden dot de
                   ` (3 preceding siblings ...)
  2006-10-11  3:43 ` bangerth at dealii dot org
@ 2007-03-08 21:49 ` fang at csl dot cornell dot edu
  2007-03-08 22:58 ` fang at csl dot cornell dot edu
                   ` (4 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: fang at csl dot cornell dot edu @ 2007-03-08 21:49 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #5 from fang at csl dot cornell dot edu  2007-03-08 21:49 -------
Ouch, this one seems particularly nasty to me... seeings as this isn't a
regression (at least from 2.95), I don't expect this to be fixed for 4.2.  Is
there any chance of this getting attention on the (4.3) mainline?  

I'm having trouble finding an elegant workaround to this -- I'm expecting a
smart pointer-to-array class to 'do the right thing' during destruction (call
the right operator delete []), but I can't just force it to call
Class::operator delete [], if the Class doesn't override it.  I just might have
to "use the force" (SFINAE)...

Incidentally, also known to fail 3.4.6, 4.0.1, 4.1.0.  


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=29164


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug c++/29164] Overloaded operator delete[] doesn't get called
  2006-09-21 10:44 [Bug c++/29164] New: Overloaded operator delete[] doesn't get called Andreas dot Kowarz at tu-dresden dot de
                   ` (4 preceding siblings ...)
  2007-03-08 21:49 ` fang at csl dot cornell dot edu
@ 2007-03-08 22:58 ` fang at csl dot cornell dot edu
  2007-03-08 23:41 ` fang at csl dot cornell dot edu
                   ` (3 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: fang at csl dot cornell dot edu @ 2007-03-08 22:58 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #6 from fang at csl dot cornell dot edu  2007-03-08 22:58 -------
Subject: Re:  Overloaded operator delete[] doesn't get called

This following test case is 'interesting':

---------------->8 snip 8<---------------------
#include <iostream>
using std::cout;

class one_array_only {
private:
        static one_array_only                           pool[256];
public:

static void * operator new[] (std::size_t size) throw() {
        cout << "class new[] operator\n";
#if WTF
        return pool;
#else
        return NULL;
#endif
}

static void operator delete[] (void *p) throw() {
        cout << "class delete[] operator\n";
}
};

one_array_only
one_array_only::pool[256];

int
main(int argc, char* argv[]) {
        one_array_only* p = new one_array_only[12];
        delete [] p;
        return 0;
}

---------------->8 snip 8<---------------------

Above, in operator new[], If WTF is false, returning NULL, I reproduce the
same error (missing call to class operator delete []).  If WTF is true
(returning pool), the right operator delete [] is called.

// output with WTF false
class new[] operator

// output with WTF true
class new[] operator
class delete[] operator

Tested on: powerpc-apple-darwin8-g++-4.0.1

Why on earth would the definition inside operator new[] influence whether
or not operator delete[] is called?


Fang


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=29164


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug c++/29164] Overloaded operator delete[] doesn't get called
  2006-09-21 10:44 [Bug c++/29164] New: Overloaded operator delete[] doesn't get called Andreas dot Kowarz at tu-dresden dot de
                   ` (5 preceding siblings ...)
  2007-03-08 22:58 ` fang at csl dot cornell dot edu
@ 2007-03-08 23:41 ` fang at csl dot cornell dot edu
  2007-03-09  9:37 ` Andreas dot Kowarz at tu-dresden dot de
                   ` (2 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: fang at csl dot cornell dot edu @ 2007-03-08 23:41 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #7 from fang at csl dot cornell dot edu  2007-03-08 23:41 -------
Subject: Re:  Overloaded operator delete[] doesn't get called

> Above, in operator new[], If WTF is false, returning NULL, I reproduce the
> same error (missing call to class operator delete []).  If WTF is true
> (returning pool), the right operator delete [] is called.
>
> // output with WTF false
> class new[] operator
>
> // output with WTF true
> class new[] operator
> class delete[] operator
>
> Tested on: powerpc-apple-darwin8-g++-4.0.1

The same result ALSO happens if I de-inline the class operator new/delete
[], and compile their definitions in a different translation unit:

------------->8 snip 8<-------------------
// de-inlined class definitions
#include <iostream>
#include "one_array_only.h"

using std::cout;

bool
one_array_only::allocated = false;

one_array_only
one_array_only::pool[256];

void*
one_array_only::operator new[] (std::size_t size) throw() {
        cout << "class new[] operator\n";
#if WTF
        return pool;    // does not trigger bug
#else
        return NULL;    // triggers bug: no operator delete[] called
#endif
}

void
one_array_only::operator delete[] (void *p) throw() {
        cout << "class delete[] operator\n";
}

------------->8 snip 8<-------------------

The main translation unit is still calling one_array_only::operator
delete[](void*) (from an asm dump).
This suggests that one_array_only::operator delete[] is somehow being
mis-compiled.

Fang


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=29164


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug c++/29164] Overloaded operator delete[] doesn't get called
  2006-09-21 10:44 [Bug c++/29164] New: Overloaded operator delete[] doesn't get called Andreas dot Kowarz at tu-dresden dot de
                   ` (6 preceding siblings ...)
  2007-03-08 23:41 ` fang at csl dot cornell dot edu
@ 2007-03-09  9:37 ` Andreas dot Kowarz at tu-dresden dot de
  2007-03-11  3:48 ` fang at csl dot cornell dot edu
  2007-03-12  9:30 ` Andreas dot Kowarz at tu-dresden dot de
  9 siblings, 0 replies; 11+ messages in thread
From: Andreas dot Kowarz at tu-dresden dot de @ 2007-03-09  9:37 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #8 from Andreas dot Kowarz at tu-dresden dot de  2007-03-09 09:37 -------
It seems that the bug triggers only when returning NULL in the new operators.
Returning something different ( tested with (void *)1 ) is a workaround for the
problem. Checking for not returning NULL in the new operators seems reasonable
even if it does not solve the mentioned mis-compilation of the delete
operators.

Tested for
- g++ 3.2.3
- g++ 4.1.1
- icpc 9.1 (returning NULL prevents both delete operators from being called
correctly)

Andreas


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=29164


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug c++/29164] Overloaded operator delete[] doesn't get called
  2006-09-21 10:44 [Bug c++/29164] New: Overloaded operator delete[] doesn't get called Andreas dot Kowarz at tu-dresden dot de
                   ` (7 preceding siblings ...)
  2007-03-09  9:37 ` Andreas dot Kowarz at tu-dresden dot de
@ 2007-03-11  3:48 ` fang at csl dot cornell dot edu
  2007-03-12  9:30 ` Andreas dot Kowarz at tu-dresden dot de
  9 siblings, 0 replies; 11+ messages in thread
From: fang at csl dot cornell dot edu @ 2007-03-11  3:48 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #9 from fang at csl dot cornell dot edu  2007-03-11 03:47 -------
In fact, I'm having trouble reproducing the problem when operator delete []
returns anything BUT NULL.  It's as if, the actual call to operator delete []
is guarded by a NULL check.  Now, if I'm RTHS (reading the holy standard)
correctly, 

in [basic.stc.dynamic.deallocation] 3.7.4.2/3:
"The value of the first argument supplied to a deallocation functions may be a
null pointer value; if so, and if the deallocation function is one supplied in
the standard library, the call has no effect."

This seems to apply specifically to the standard library, but not necessarily
non-standard implementations of the operator.  Does that mean that the call to
operator delete [] can be elided legally if the argument is NULL, or that the
implementation of operator delete [] (NULL) is required to have no effect? 


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=29164


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug c++/29164] Overloaded operator delete[] doesn't get called
  2006-09-21 10:44 [Bug c++/29164] New: Overloaded operator delete[] doesn't get called Andreas dot Kowarz at tu-dresden dot de
                   ` (8 preceding siblings ...)
  2007-03-11  3:48 ` fang at csl dot cornell dot edu
@ 2007-03-12  9:30 ` Andreas dot Kowarz at tu-dresden dot de
  9 siblings, 0 replies; 11+ messages in thread
From: Andreas dot Kowarz at tu-dresden dot de @ 2007-03-12  9:30 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #10 from Andreas dot Kowarz at tu-dresden dot de  2007-03-12 09:30 -------
THS (The Holy Standard :-) ) 3.7.4.2/3 reads to me that for standard library
implementations the delete operators must be called in any case but return
immediately if the first argument is NULL => NULL-check inside the operator.

Taking the context into account, it reads to me as if standard library delete
calls can be treated as special cases. In the more general case, i.e., the user
supplied delete operators, no special assumptions can be done. Again, the
operators must be called in any case but it is up to the user to handle special
cases correctly. Whatever the latter may look like.

Andreas


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=29164


^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2007-03-12  9:30 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-09-21 10:44 [Bug c++/29164] New: Overloaded operator delete[] doesn't get called Andreas dot Kowarz at tu-dresden dot de
2006-09-21 10:47 ` [Bug c++/29164] " Andreas dot Kowarz at tu-dresden dot de
2006-09-21 11:25 ` rguenth at gcc dot gnu dot org
2006-09-21 12:00 ` Andreas dot Kowarz at tu-dresden dot de
2006-10-11  3:43 ` bangerth at dealii dot org
2007-03-08 21:49 ` fang at csl dot cornell dot edu
2007-03-08 22:58 ` fang at csl dot cornell dot edu
2007-03-08 23:41 ` fang at csl dot cornell dot edu
2007-03-09  9:37 ` Andreas dot Kowarz at tu-dresden dot de
2007-03-11  3:48 ` fang at csl dot cornell dot edu
2007-03-12  9:30 ` Andreas dot Kowarz at tu-dresden dot de

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).