iPhone Crashes in the Nowhere Land
On Sunday I started to have a closer look at the memory management of the application and fully implemented the dealloc methods. Subsequent testing revealed a nasty bug where the application just died when I moved from one part of the application do another but I had not idea why it was crashing and especially where.
Well, at least I had a hunch where it was happening XCode did not point near there. Eventually I started to set breakpoints in all the dealloc methods that I could think of would be called. Eventually I saw that one property was already released and my release made it crash. So I just tried a few things and eventually found the resolution in calling the super class’ dealloc method at the end. So far I did not find any clear instruction if to call it first or last but it seems to be that way that it needs to be called last.
There are a few other things I have to keep in mind when dealing with memory management:
- Use properties when dealing with references because it does handle re-assignments by itself
- With properties every assignments to a property must be done with self like this: self.myProperty = …. This way we make sure that the setter method is called and subsequently the object is retained (if marked as such)
- Called [super dealloc] as last statement in the object’s dealloc method (if provided)
- Every object creation with alloc or new must be autoreleased when assigned to retaining property
Well, no I can go back and chase some more bugs.
-Andy
Comments are closed.