www.digitalmars.com Home | Search | C & C++ | D | DMDScript | News Groups | index | prev | next
Archives

D Programming
D
D.gnu
digitalmars.D
digitalmars.D.bugs
digitalmars.D.dtl
digitalmars.D.dwt
digitalmars.D.announce
digitalmars.D.learn
digitalmars.D.debugger

C/C++ Programming
c++
c++.announce
c++.atl
c++.beta
c++.chat
c++.command-line
c++.dos
c++.dos.16-bits
c++.dos.32-bits
c++.idde
c++.mfc
c++.rtl
c++.stl
c++.stl.hp
c++.stl.port
c++.stl.sgi
c++.stlsoft
c++.windows
c++.windows.16-bits
c++.windows.32-bits
c++.wxwindows

digitalmars.empire
digitalmars.DMDScript

c++ - bad code generated when deleting object

I finally tracked down the crash in omniidl (I reported earlier) to this
code generation problem:


#include <stdio.h>

void operator delete(void *x)
{
  printf("delete(%08x)\n", x);
}

struct A
{
  A()
  {
    printf("A::A() this=%08x\n", this);
    parent = instance;
    instance = this;
  }

  ~A()
  {
    printf("A::~A() this=%08x\n", this);
    instance = parent;
  }

  static void destroy()
  {
    delete instance;
  }

 private:
  A *parent;
  static A *instance;
};

A *A::instance = 0;

int main()
{
  A *a1 = new A;
  A *a2 = new A;

  A::destroy();
  A::destroy();

  return 0;
}


When compiled with DM ("dmc test.cpp" or "dmc -o+all test.cpp") I get the
following output:

A::A() this=40ab1a5c
A::A() this=40ab1a68
A::~A() this=40ab1a68
delete(40ab1a5c)
A::~A() this=40ab1a5c
delete(00000000)

The interesting part is that the wrong memory block is freed (the destructor
is called for 40ab1a68, but the object at 40ab1a5c is freed - Digital Mars
assumes that the destructor for A doesn't change A::instance...).


bye, Christof

-- 
http://cmeerw.org                                 JID: cmeerw jabber.at
mailto cmeerw at web.de

...and what have you contributed to the Net?
Dec 13 2002