🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: gnu.g++.bug
Article #1837

Overloaded delete operator bug

#1837
From: Graeme Dixon
Date: Mon, 10 Apr 1989 12:26
73 lines
1985 bytes
There is a bug in the code generated by g++ when the delete operator
is overloaded that results in the delete operator being called once for
each derived class in a hierarchy. The following program illustrates
the bug:

#include <stream.h>

class A
{
public:
    A()  { cout << "A::A\n"; }
    ~A() { cout << "A::~A\n"; }

    void* operator new(long size)  { cout << "A::new\n"; return malloc(size); }
    void operator delete(A* todel) { cout << "A::delete\n"; free(todel); }
};

class B: public A
{
public:
    B()  { cout << "B::B\n"; }
    ~B() { cout << "B::~B\n"; }
};

class C: public B
{
public:
    C()  { cout << "C::C\n"; }
    ~C() { cout << "C::~C\n"; }
};

main()
{
    B *b = new B;
    delete b;      // OK

    C *c = new C;
    delete c;      // not OK - delete called twice
}

and produces the following when compiled with g++ (version 1.34.0) and
linked with /usr/lib/debug/malloc.o (using
g++ -o bug bug.cc /usr/lib/debug/malloc.o):

A::new
A::A
B::B
B::~B
A::~A
A::delete
A::new
A::A
B::B
C::C
C::~C
B::~B
A::~A
A::delete
A::delete
free: block 0x21804 was already free

Abort (core dumped)

Each successively derived class results in an additional invocation of
the delete operator when an instance of that class is deleted.

Graeme Dixon -
  Computing Laboratory, University of Newcastle upon Tyne, UK

JANET = Graeme.Dixon@uk.ac.newcastle
ARPA  = GraemeDixon@newcastle.ac.uk
UUCP  = ...!ukc!newcastle.ac.uk!Graeme.Dixon
PHONE = +44 91 222 8067 
Technical Details
Message-ID: <AA26080.8904101226.snipe@uk.ac.newcastle>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!NSS.CS.UCL.AC.UK!Graeme.Dixon%newcastle.ac.uk

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!NSS.CS.UCL.AC.UK!Graeme.Dixon%newcastle.ac.uk
From: Graeme.Dixon%newcastle.ac.uk@NSS.CS.UCL.AC.UK (Graeme Dixon)
Newsgroups: gnu.g++.bug
Subject: Overloaded delete operator bug
Message-ID: <AA26080.8904101226.snipe@uk.ac.newcastle>
Date: 10 Apr 89 12:26:40 GMT
Sender: daemon@tut.cis.ohio-state.edu
Distribution: gnu
Organization: GNUs Not Usenet
Lines: 73