easy way find that *this* is deleted

This is a discussion on easy way find that *this* is deleted within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> using namespace std; class Test { public: void immortal() { cout << "I am Immortal!" << endl; ...

  1. #1
    Banned
    Join Date
    Nov 2007
    Posts
    678

    easy way find that *this* is deleted

    Code:
    #include <iostream>
    using namespace std;
    
    class Test
    {
    public:
    	void immortal() { cout << "I am Immortal!" << endl; }
    };
    
    int main()
    {
    	Test* t = new Test;
    	t->immortal();
    	delete t;
    	t->immortal();
    	return 0;
    }
    The above code works fine (in debug and non debug mode).
    I have a similar situation in my project. Where an object gets deleted, and then a successful call to it's method is
    made. But when that method tried to access some data member (in this case an integer)
    my program crashed!

    How to detect where the object gets deleted? There is no obvious hint form. Also function calls working fine confuse me more

  2. #2
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    Well since you have deleted "t", it no longer has the allocated memory from new, so it makes sense it crashed. Personally I would of done this also:

    Code:
    delete t;
    t = NULL; // ensure the pointer holds nothing
    I'm just trying to be a better person - My Name Is Earl

  3. #3
    Banned
    Join Date
    Nov 2007
    Posts
    678
    no it does not crash at all! work fine!

    but when i access a data member, only then the function seems to need *this* and that's when i get a crash.
    and at that point i don't have the point of return, where i can go an check why was the object deleted?

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,625
    Accessing a stray pointer is undefined behavior.

    If you want to call a method after an object dies, make it a static member (a search friendly term) so that the behavior is defined.
    Code:
    #include <iostream>
    class widget {
      public:
        static void f () { std::cout << "f(), call me anytime\n"; }
    };
    
    int main ()
    {
      { // intentional scoping
        widget w;
        w.f();
      }
      widget::f();
    }
    Last edited by whiteflags; 04-11-2008 at 01:19 AM.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    Quote Originally Posted by manav View Post
    no it does not crash at all! work fine!

    but when i access a data member, only then the function seems to need *this* and that's when i get a crash.
    and at that point i don't have the point of return, where i can go an check why was the object deleted?
    Functions are not stored inside the object. They exist outside the object, in the code space. Therefore, the compiler needs to pass the object's address to the function (the this parameter).
    Since member variables are stored in the address of memory where this points to, you get a crash when trying to access them. But calling member function are perfectly valid without a crash. However, it's still undefined behavior.
    A typical approach is to always set deleted objects to NULL and check before trying to dereference or call functions if the object is not NULL.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Of course, that doesn't help you if you access the dead object through a different pointer than the one you set to NULL. Which is why you really need more careful thought about object ownership and lifetimes.
    Once you know how long an object lives and who's responsible for deleting it, you can use smart pointers to enforce these ideas.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Banned
    Join Date
    Nov 2007
    Posts
    678
    i was actually calling the function through a reference. which was passed from multiple levels of function calls.
    none of the function calls are direct. they use boost events to call each function in turn. say, on after another.
    and i can not (as yet) find where the object gets destroyed midway, all i have is a reference which
    succeeds calling the function and when that function tries to access a data member, my program crashes!

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    Easy. Track where the object still lives. Then put a breakpoint in the destructor.
    You could also set a breakpoint condition to "this = (address of object)".
    You just need to make sure the object is created first and then put the breakpoint and you should be able to find when it's destructed.
    Last edited by Elysia; 04-11-2008 at 02:54 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. please help!...find and replace
    By amy589 in forum C++ Programming
    Replies: 26
    Last Post: 10-13-2006, 06:42 PM
  3. Can't find DirectX!!!
    By Queatrix in forum C++ Programming
    Replies: 2
    Last Post: 07-24-2006, 07:50 PM
  4. Replies: 3
    Last Post: 06-09-2006, 09:53 AM
  5. Q: Recursion to find all paths of a maze
    By reti in forum C Programming
    Replies: 7
    Last Post: 11-26-2002, 08:28 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21