iPhone/iPad Memory Management
I was facing some, seemingly intermittent, crashes of an application I am currently writing, execution would abruptly terminate without any indication of what went wrong.
Seems my understanding of object allocation and releasing was not entirely correct, more specifically I was making wrong assumptions about the iPhone SDK and the internal implementation of its types such as NSArray and NSString.
I was able to fix the problem by not releasing my instances everywhere and just following these rules of thumb:
- release every object you have allocated, retained or copied (alloc, retain, copy)
doing so will reduce chances on having memory leaks - do not release instances such as returned from NSString.stringWithFormat:
since there's no alloc involved you are not the owner of the instance, the previous is equivalent to [[[NSString alloc] initWithFormat:] autorelease] - adding objects to NSMutableArray will have the array retain these objects, subsequently releasing the array will also release the objects
Releasing an instance which has been marked for autorelease will almost immediately result in a crash. So don't release anything unless you have ownership of an instance referenced after an alloc, retain or copy.



1 Comments:
Nice Blog !
I Like This Very Much.
Methods of Modern Farming
By
Suporna Sarkar, at 8:14 AM
Post a Comment
<< Home