'this' pointer corruption or am I fooling myself ?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

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

  1. #1
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,239

    '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:-

    Code:
    namespace Whatever {
    
    class B {
    public:
    	virtual ~B();
    
    private:
    	int bb;
    };
    
    class A : public B {
    public:
    	virtual ~A();
    
    private:
    	int aa;
    };
    
    B::~B()
    {
    	bb = 0; // <--- Breakpoint #3 here
    }
    
    A::~A()
    {
    	aa = 0; // <--- Breakpoint #2 here
    }
    
    } /* namespace Whatever */
    
    int main (int argc, char *argv[])
    {
    	Whatever::A* pA = new Whatever::A;
    	delete pA; // <--- Breakpoint #1 here
    
    	return 0;
    }
    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.

    Now let's consider the real example:-

    Code:
    namespace Gtk {
    
    class Widget {
    public:
      virtual ~Widget() {}
    };
    
    class Container : public Widget {
    public:
      virtual ~Container() {}
    };
    
    class Bin : public Container {
    public:
    	virtual ~Bin() {}
    };
    
    class Window : public Bin {
    public:
    	virtual ~Window();
    
    protected:
    	void destroy_();
    };
    
    Window::~Window()
    {
      destroy_(); // <--- Breakpoint #2 here
    }
    
    } /* namespace Gtk */
    
    int main (int argc, char *argv[])
    {
    	Gtk::Window* pW = new Gtk::Window;
    	delete pW; // <--- Breakpoint #1 here
    
    	return 0;
    }
    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
    "A problem well stated is a problem half solved. - Charles F. Kettering

  2. #2
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,845

    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.

  3. #3
    Join Date
    Jan 2001
    Posts
    251

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

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

    One situation that can cause this to happen is if the classes use virtual inheritance.

    Code:
    class A
    {
    public:
       virtual ~A()
       {
       }
    
       int a;
    };
    
    class B
    {
    public:
       virtual ~B()
       {
       }
    
       int b;
    };
    
    class C : public virtual A, public virtual B
    {
    public:
       virtual ~C()
       {
          // Breakpoint #2 here
       }
    
       int c;
    };
    
    
    int main (int argc, char *argv[])
    {
       C *pC = new C;
       delete pC; // <--- Breakpoint #1 here
    
       return 0;
    }

  4. #4
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,239

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

    Quote Originally Posted by jwbarton View Post
    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:-

    Code:
    namespace Gtk {
    
    Window::~Window()
    {
      Glib::ustring s = get_title();
      printf ("Window \"%s\" is at 0x%x\n", s.c_str(), this);
      
      destroy_();
    }
    
    };
    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.
    "A problem well stated is a problem half solved. - Charles F. Kettering

  5. #5
    Join Date
    Apr 1999
    Posts
    27,426

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

    Quote Originally Posted by John E View Post
    But the value of this (as reported in the debugger output window) is always wrong for this particular object type.
    What version of the compiler are you using?

    Regards,

    Paul McKenzie

  6. #6
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,239

    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!
    "A problem well stated is a problem half solved. - Charles F. Kettering

  7. #7
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,239

    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!!!
    "A problem well stated is a problem half solved. - Charles F. Kettering

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center