CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Oct 2007
    Posts
    34

    Passing By Reference then Deleting the Passed Object

    Why does the following code not break after delete bar? It seems like the object foo would have a reference to a deleted object and crash in printBar(). Is bar not really gone after the "delete bar" line?

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    class Bar
    {
    public:
        Bar() {}
        void print() {
            cerr << "Bar::print(), Hey there!!!!" << endl;
        }
    
    private:
    
    };
    
    class Foo
    {
    public:
        Foo(Bar& b) : myBar(b) {}
    
        void printBar() {
            myBar.print();
        }
    
    private:
        Bar& myBar;
    };
    
    
    int main(int argc, char *argv[])
    {
    
        Bar* bar = new Bar();
        Foo* foo = new Foo(*bar);
    
        delete bar;
        bar->print();
        
        for (int i = 0; i < 10; ++i) {
            foo->printBar();
        }
    
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    I tried searching but all search results turn up more general information about references.

    Thanks.

  2. #2
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Passing By Reference then Deleting the Passed Object

    Because this is undefined behavior. It might work, it might not. It might cause a bug to manifest itself several thousand lines of code later. You never know.

    Incidentally, your Bar :: print function doesn't really do anything with the object itself, so this would work too:
    Code:
    Bar *pBar = NULL;
    pBar->print();
    Viggy

  3. #3
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Lightbulb Re: Passing By Reference then Deleting the Passed Object

    You are experiencing what is called a dangling pointer/reference which results in undefined behavior. The moment that you delete the bar object all pointers to that object are pointing to memory that was returned to the heap. Since you make the call on the dangling pointer right after the delete it may seem that the code works fine and produces reasonable results. On some systems you might see the program crash. The behavior is undefined so there is no way to predict what will happen. It is up to you the programmer to prevent this undefined behavior.

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Passing By Reference then Deleting the Passed Object

    Quote Originally Posted by MrViggy View Post
    Incidentally, your Bar :: print function doesn't really do anything with the object itself, so this would work too:
    Code:
    Bar *pBar = NULL;
    pBar->print();
    Viggy
    On most compilers, yes, that would work, since you're never touching the (invalid) this pointer. It should not be taken to mean that it *must* work, of course.

  5. #5
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Passing By Reference then Deleting the Passed Object

    Quote Originally Posted by Lindley View Post
    On most compilers, yes, that would work, since you're never touching the (invalid) this pointer. It should not be taken to mean that it *must* work, of course.
    Agreed. I didn't mean to imply that it must work for all cases. Sorry about that!

    Viggy

  6. #6
    Join Date
    May 2009
    Posts
    2,413

    Re: Passing By Reference then Deleting the Passed Object

    Code with undefined behaviour is neither working nor non-working. I would rather describe it as dead man walking.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured