c++ - bad code generated when deleting object
- Christof Meerwald (53/53) Dec 13 2002 I finally tracked down the crash in omniidl (I reported earlier) to this
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