www.digitalmars.com         C & C++   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
    printf("A::A() this=%08x\n", this);
    parent = instance;
    instance = this;

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

  static void destroy()
    delete instance;

  A *parent;
  static A *instance;

A *A::instance = 0;

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


  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
A::~A() this=40ab1a5c

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