ohmprakash
ohmprakash

Reputation: 123

What will happen to this object

I have created one firstObject with the help of secondObject. Now I am released that secondObject what will happen to the firstObject. Here is my code.

SecondObject *secondObject = [[SecondObject alloc]init];
FirstObject *firstObject = [[FirstObject alloc]initWithSecondObject:secondObject];
[secondObject doSomethings];
[firstObject doSomeThings];
[secondObject release];

Upvotes: 0

Views: 71

Answers (2)

Caleb
Caleb

Reputation: 125007

If firstObject needs to keep a reference to secondObject beyond it's initWithSecondObject: method, it should retain secondObject. That will prevent secondObject from being deallocated even though you're releasing it in the code above. When you release an object, you're saying: "I'm done using this." But the object will only be deallocated if no other object is currently using it (as determined by the retain count, not that you should be looking at that).

Upvotes: 1

user1118321
user1118321

Reputation: 26375

Unless secondObject has a reference to firstObject which it releases, nothing will happen to firstObject.

Upvotes: 2

Related Questions