What steps should we take — what are the best practices — to prevent leaks when using @property and @synthesize?
What steps should we take — what are the best practices — to prevent
Share
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.
Be aware of your standard things that give you back retained objects, methods with alloc, copy or new. When you invoke these along with your property you can inadvertently create a leak.
In your interface you have
And in your implementation you have
Then later on you use the the property
your object now has a retain count of 2. one from using self.someArray = and one from the alloc. self.someArray = invokes your setter which is the same as – (void)setSomeArray:(NSArray)someArray; which is created for you with the synthesize. This is going to contain a retain because of the retain keyword you used in the @property declaration.
I tend to avoid this one of two ways.
either with using the autoreleased intializer
or
or use a temp variable
all of these methods will leave you with your self.someArray object having a retain count of one which you can take care of in the dealloc.