Jump to content

'this' pointer of base class changing in destructor


photo

Recommended Posts

Hi,

 

when deleting an object of a derived class in UnigineScript and checking its 'this' pointer (and type) in the destructor of its base class, the pointer has changed. Let's take a look at the example to clarify what I mean:

 

class Base
{
    Base()     { log.warning(" Base   : %s\n", typeinfo(this)); }
   ~Base()     { log.warning("~Base   : %s\n", typeinfo(this)); }
};

class Derived : Base
{
    Derived()  { log.warning(" Derived: %s\n", typeinfo(this)); }
   ~Derived()  { log.warning("~Derived: %s\n", typeinfo(this)); }
};

 

Create an object of Derived and delete it:

Derived derived = new Derived();
delete derived;

 

And we get the following output:

Base   : Derived 08554D20 (1:0:0)17:29:49
Derived: Derived 08554D20 (1:0:0)17:29:49
~Derived: Derived 08554D20 (1:0:0)17:29:49
~Base   : Base 08554D60 (0:0:0)17

 

As you can see, the pointer in the base class destructor is different!

 

The problem is, that we use the constructor to register the object in a global map (map.append(this)) and we need to deregister it in the destructor. Since the pointer isn't the same anymore, we can't find it in the map to remove it and are stuck with pointer pointing to already deleted objects.

Link to comment

Thank you. Fixed.

 

class Foo {
Foo() { printf("Foo::Foo(): %s\n",typeinfo(this)); }
~Foo() { printf("Foo::~Foo(): %s\n",typeinfo(this)); }
};
class Bar : Foo {
Bar() { printf("Bar::Bar(): %s\n",typeinfo(this)); }
~Bar() { printf("Bar::~Bar(): %s\n",typeinfo(this)); }
};
Bar b = new Bar();
delete b;

 

Foo::Foo(): Bar 0x9f0e2a0 (524289:0:0)
Bar::Bar(): Bar 0x9f0e2a0 (524289:0:0)
Bar::~Bar(): Bar 0x9f0e2a0 (524289:0:0)
Foo::~Foo(): Bar 0x9f0e2a0 (524289:0:0)

Link to comment
×
×
  • Create New...