Reputation: 45
So I am a bit confused on when objects are autoreleased. I understand so far that if I am not the "owner", it will do so. But in which cases would I not be the owner? When I create an object using a convenience method? I don't understand where all these convenience methods are coming from and how would you create them.
Upvotes: 2
Views: 104
Reputation: 21986
You generally use alloc
+ an initializer to create objects that will not be autoreleased. Instead you use static methods to get autoreleased instances. Example:
NSString* string1;
NSString* string2;
@autoreleasepool{
string1= [NSString stringWithString: @"Hello"];
string2= [[NSString alloc] initWithString: @"Hello"];
}
// string1 isn't alive, string2 is alive
You must also pay attention to singletons. In case of singletons, they're not autoreleased but you don't own them. Often you understand from the name of the method is it returns a singleton or not (eg: something like sharedInstance
or mainThread
).
Upvotes: 1