If I have the below property:
@property (nonatomic, retain) MyObject *theObject;
then if I want to create a new MyObject, do I:
self.theObject = [[MyObject alloc] init];
or:
self.theObject = [[[MyObject alloc] init] autorelease];
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You have to use autorelease. If you don’t, the object will have a release count of two (one from
alloc, one from theretainby the setter), so when the property is unset, it’ll be leaked.This only applies to strong or retained and copy properties. Assigned and weak properties should just be assigned an alloc-initted object, since they don’t alter its reference count.