views:

30

answers:

2

For example:

MyClass *obj1 = [[MyClass alloc] init];
MyClass *obj2 = [obj1 retain];

and release it with

[obj2 release];
[obj2 release];

Is it legal to do this? Will the object will be released properly?

A: 

You can check easily enough by testing if obj1 == obj2.

Matt Kane
+1  A: 

Yep, it'll work fine. You're calling release on the same object, no matter which pointer variable you use to do so.

Looks a bit weird though.

And, a common idiom would be

[obj1 release], obj1 = nil;
[obj2 release], obj2 = nil;

which clearly wouldn't work if you used obj2 in both lines.

Graham Perks