Ok……
I’m implementing a simple OpenGL ES application on the iPhone and I recently added in Pinch Media Analytics. Doing this helped uncover a memory management problem, and I’m not entirely sure how to deal with it.
In a perfect world, my application – which loads PNGs and .CAF files in didFinishLoading will launch, load all of it’s resources, and run just fine.
HOWEVER, if my program gets a crash (which happened as I was integrating the Pinch Media libraries) or if I run Safari and open a bunch of pages and then launch my game, the game will crash back to the menu because it is out of memory.
This problem will persist until I do a hard reset of the system.
The default answer that you sort of get online is to implement the didReceiveMemoryWarning method as listed below….
- (void)didReceiveMemoryWarning
{
// default behavior is to release the view if it doesn't have a superview.
// remember to clean up anything outside of this view's scope, such as
// data cached in the class instance and other global data.
[super didReceiveMemoryWarning];
}
But this doesn’t really help as it’s the other programs that are holding onto memory not mine. I don’t want to release my own view do I? Is there a good description of how to handle this situation and / or what happens in the didReceiveMemoryWarning event?
If you only have a single view, then the only thing you can do really is to release any data you are not using, and lazy load them later.
If you have more than a single view, then they might be released if they are not visible. If this happens, the corresponding controller will be sent
setView:withnil. I handle this happening by immediately releasing allIBOutletvariables so they are set properly when the view is loaded from itsxibagain.This is the approach I take in a normal, non-OpenGL ES application which has >6 views, and works consistently even when I am 4 levels deep in a navigation view and all previous controllers have their views set to
nil– there is no crash when I navigate backwards, though there is a delay as views are reloaded.If you haven’t found it already, in the simulator there is a menu item to simulate a memory warning, which is easier than forcing the condition to occur in a real device. That said, it does not replace testing the same scenario in a real device – just makes testing easier.