What steps should we take -- what are the best practices -- to prevent leaks when using @property
and @synthesize
?
相关问题
- CALayer - backgroundColor flipped?
- What uses more memory in c++? An 2 ints or 2 funct
- Core Data lightweight migration crashes after App
- back button text does not change
- Achieving the equivalent of a variable-length (loc
相关文章
- 现在使用swift开发ios应用好还是swift?
- TCC __TCCAccessRequest_block_invoke
- Does JavaScript allow getters and setters?
- xcode 4 garbage collection removed?
- Unable to process app at this time due to a genera
- Notice: Undefined property - how do I avoid that m
- How can I add media attachments to my push notific
- didBeginContact:(SKPhysicsContact *)contact not in
One thing that has helped me a lot is to check your header file for property definitions with a retain type, and then make sure that there is a release for each of them in the -dealloc method.
For the various assignments to the properties during the lifetime of the object, the automatic synthesized setters should take care of it.
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.