If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

'this' pointer corruption or am I fooling myself ?

I'm working on a project which uses gtk+ and gtkmm. We use them in preference to MFC because the program needs to be cross-platform. For quite a long time, customers on OS-X and Linux have sometimes complained that the program would crash during shutdown but the Windows version (which I work on) never seemed to suffer. However, I'm now transferring my build environment to a new PC and I'm noticing the same shutdown crashes. It's a bit complicated so let me start with a small example:-

Suppose I run the above program. When it stops at breakpoint #1 I make a note of the value of pA. Eventually the program reaches breakpoints #2 and #3. At each point my this pointer is exactly the same number. If the value of pA was 0x03604fb0, my this pointer is identical at both stages.

Suppose I run the real example. At breakpoint #1 the value of pW is 0x03604fb0. But by the time I reach breakpoint #2 my this point is slightly different:- 0x03604fcc. Can anyone think of a reason for this behaviour? It doesn't seem right to me and I'm wondering if it might be contributing to our shutdown crashes

Re: 'this' pointer corruption or am I fooling myself ?

The 'this' pointer value of a derived class is not required to be the same as the 'this' pointer value of it's parent class.

Sometimes this will be the case, and sometimes it won't. It's a wrong assumption that they should always be the same.

there are many reasons why this could happen the most obvious one is that one of the intermediate classes has multiple inheritance. But there could be many other reasons.

If you have a reason to assume the pointer is getting changed incorrectly. make a note of the pointer values as they enter the class constructor and see if those match with the values of classes being destroyed.

Re: 'this' pointer corruption or am I fooling myself ?

Originally Posted by jwbarton

I can't get it to happen with the example that you posted.

Sorry, I should have mentioned that my 'sample' wasn't the full code. I posted an abridged version to illustrate that none of the classes in the chain has more than one parent class. Also (OReubens) I take your point about the respective this pointers for a parent class and a derived class - but I was making the point that if I create an object of type Gtk::Window, its this pointer should surely be the same as the value returned from new Gtk::Window; ??

In any case - believe it or not this appears to be a problem with the debugger output window!!! I modified function Gtk::Window::~Window() to look like this:-

The above code allows me to retrieve the window's title (i.e. so I can check it's the right window). Then I print its this pointer. Lo and behold I get the correct result! But the value of this (as reported in the debugger output window) is always wrong for this particular object type.

Re: 'this' pointer corruption or am I fooling myself ?

VC8 (VS 2005)

I wouldn't be surprised if that has never been reported before. I tried other types of object without seeing this problem at all. It seems to be present with several types of gtkmm object though. Of course, given that it's an 8 year old compiler I don't feel inclined to complain too much!

Re: 'this' pointer corruption or am I fooling myself ?

Oh BTW - literally a minute ago I discovered the reason for the crash. It is connected with object destruction but nothing to do with the rogue this anomaly. this turned out to be a complete red herring but (by a stroke of sheer luck) it led me to investigate the object destruction code, where I've just found an actual coding bug! How jammy is that!!!