1

In the code below, the destructor of class B is called in Case 1 but not in Case 2, when the execution returns back to main from fn(). I don't understand this difference, since both are on heap memory when A is created with new. Could you please explain?

class B {
public:
    B() {
        printf(" [B] COntsructor");
    }
    ~B() {
        printf(" [B] Destructor");
    }
};

class A {
public:
    A() { 
        printf(" [A] COntsructor");
    }
    ~A() { 
        printf(" [A] Destructor");
    }

    B Query() { return b; }    /// Case 1
    B* Query() { return &b; }  /// Case 2

    B b;
};

void fn()
{
    A *a = new A();
    B b = a->Query();  // case 1
    B* b = a->Query(); // case 2
    return;
}

int _tmain(int argc, _TCHAR* argv[])
{
    fn();
    return 0;
}
5
  • 1
    you have two case 1, case 2 in code, confusing...
    – billz
    Jan 7, 2013 at 6:43
  • When you return by value, you have to construct a value to return. Jan 7, 2013 at 6:44
  • @billz i think each case involves commenting out the other line of the pair.
    – Karthik T
    Jan 7, 2013 at 6:45
  • You never delete object a of type A - you have a memory leak in your function fn. Jan 7, 2013 at 6:45
  • @billz - no, they're different. The labels are case-sensitive. Jan 7, 2013 at 12:52

4 Answers 4

3

Case 1: When you return b by value the local variable b is constructed using the copy constructor. All but one copy is optimized away by Return Value Optimization. The destruction of the local variable b triggers the destructor.

Case 2: When you return &b all you are returning is a pointer to b and so there is no destruction necessary.

Edit: The code shows the destructor being called without a corresponding call to the constructor. This is because the function return copy is happening through the copy constructor.

Edit2: @ZarShardan is correct - the "Many copies" I refered to would likely not exist thanks to Return Value Optimization.

6
  • i intially thought the same that only a local copy is getting destroyed , but the Constructor of B is called only once and then the Destructor of B is called. What about the member variable of A on heap? Jan 7, 2013 at 6:45
  • @user1862187 the member variable is the one being returned, when it is returned by value, a copy is made of it on the stack, as the return value of the function a->query. It is the destruction of this copy that calls the destructor.
    – Karthik T
    Jan 7, 2013 at 6:46
  • 1
    Ahh...copy constructor is called. Jan 7, 2013 at 6:52
  • i am sorry did i do anything? Jan 7, 2013 at 7:10
  • I have one downvote to this question, I doubt its you, unless the down arrow is highlighted for you
    – Karthik T
    Jan 7, 2013 at 7:11
1
  • a will never get deleted, so no destructors for it or its members.
  • a->b will be constructed using the default constructor, so that's the constructor call you see.
  • B b = a->Query(); will be created using the copy constructor, which doesn't print anything.
  • At the end of fn, the local b will go out of scope, so that's your destructor call.

Things might become clearer if you add debug code for the copy constructor, if you have all debug code print the value of this, and if you eventually delete a to see those destructor calls as well.

0
0

I don't see you creating a new instance of B in the second case, thus the pointer that A holds as a member would be cleaned up with A's destructor and since a constructor in B was never invoked a destructor won't be invoked either.

-1

Object a is never deleted, so neither its own destructor nor its member object(s) destructor(s) is(are) called.

Try adding

delete a; 

in the end of your function fn()

or better use std::unique_ptr instead of a bare pointer. That's better design in case your Query member function throws an exception (RAII stuff)

2
  • 1
    while a valid suggestion, not an answer to the question and should be a comment.
    – Karthik T
    Jan 7, 2013 at 6:50
  • Why downvote? I explained why the destructors aren't called: object a (hence all its members as well) is constructed on the heap and never deleted. I also pointed out this is a rather serious (in C++ world) bug - a memory leak, and then edited to add a better way to handle this. While maybe not a very comprehensive answer it is still correct and adds value to the subject. Either way doesn't deserve a downvote. Jan 7, 2013 at 9:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.