2

I'm a bit confused about object lifetime in C++. Let's say I have the following code. First I create the pointer MyObject (line 1). Then I create an object and point the pointer at it (line 2). Then I modify the object, and point the pointer at the resultant object (line 3). Finally, I delete the object, so that I avoid a memory leak (line 4).

MyClass * MyObject;
MyObject= new MyClass(x, y); 
*MyObject= MyObject-> ModifyObject(z);
delete MyObject;

Did the original object from line 2 simply get modified in line 3? (Which means that the above code is safe). Or was a second object created in line 3, meaning that the first object from line 2 never gets deleted, creating a memory leak?

EDIT: Here's a sample of what ModifyObject(z) might look like

MyClass MyClass::ModifyObject(int z) {
    int a = z;
    int b = z;
    return MyClass(a, b);
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
MindSeeker
  • 560
  • 1
  • 6
  • 18
  • 1
    *"Then I modify the object, and point the pointer at the resultant object (line 3)."* -- No, line 3 does not do that. In line 3, you dereference the pointer, which means you are calling the assignment operator on the object. The pointer is pointing at the exact same place. – Benjamin Lindley Jun 13 '14 at 03:10

2 Answers2

5

Did the original object from line 2 simply get modified in line 3? (Which means that the above code is safe). Or was a second object created in line 3

Actually, both. But it's not a leak. And it all depends on that unusual implementation of ModifyObject

This code

MyClass MyClass::ModifyObject(z) {
    int a = z;
    int b = z;
    return MyClass(a, b);
}

doesn't modify the instance it was called on. Instead it creates a separate temporary MyClass instance as its return value. (So really the function name is misleading.)

Using that function in the line *MyObject = MyObject->ModifyObject(z); the temporary object is returned from ModifyObject. Then all of its data is copied the original instance referred to by *MyObject via the assignment operator. Then the temporary is destroyed automatically.

So after that, there is still only one object that was ever allocated via new and your MyObject pointer is still pointing to it. So the delete statement correctly deallocates it.

TheUndeadFish
  • 8,058
  • 1
  • 23
  • 17
1

Ignoring the syntax errors . . .

All your ModifyObject code is doing is creating a new object and returning it. It's not modifying the object Pointed to by MyObject.

Assuming you meant

MyClass MyClass::ModifyObject(int z) {
    int a = z;
    int b = z;
    return MyClass(a, b);
}

that is equivalent to

MyClass MyClass::ModifyObject(int z) {
    return MyClass(z, z);
}

which could just as easily be

*MyObject = MyClass (z, z) ;

Which, if you have defined an assignment operator for MyClass, class that. Otherwise, it does a binary copy (often a very bad thing).

Yes, the original object got modified through assignment in line 3.

user3344003
  • 20,574
  • 3
  • 26
  • 62
  • Sorry, not the best example, in reality the ModifyObject() takes some existing variables owned by the object into account in the calculations for the new a and b (rather than just setting them to z). I just made the example that way for simplicity. Thanks for your help! – MindSeeker Jun 13 '14 at 04:03
  • 2
    Then what you are doing is using the object to create a new object and overwrite itself. What you would normally do is have void ModifyObject and change whatever there. Don't create a new object and don't do the assignment afterwards. – user3344003 Jun 13 '14 at 04:05
  • Wow, don't know why I didn't think of that before. Thank you! – MindSeeker Jun 13 '14 at 05:22