cocos2d and memory warnings
I have been suffering from sudden crashes in one app I am currently developing for the iPad using cocos2d. To make things worse, those crashes were intermittent, depending on some timing constraints.
The first hint I had was that the crash happened after receiving a memory warning. So, I thought I was loading too many textures in cocos2d and that the system decided to kill my app to recover memory. That would have been a problem, because I was already using texture maps to reduce my memory footprint and I had carefully calculated the maximum amount of textures that I could have. Still, I was crashing. The only way I could see to go beyond, i.e., reducing further the texture amount, would have put me in serious troubles with the other contributors to the project who have had to remake the graphics.
But there was the timing factor to tell me that it was not only a matter of memory consumption.
It turns out that the problem had to do with how cocos2d manages its texture cache in presence of a memory warning, and the way this interact with pre-caching of large texture. The point is that when you are pre-caching some texture so that they are readily available when you need them, e.g., using CCTextureCache addImageAsync, the textures go into the cache but they are still not used by any sprite. They will not become used until you create a sprite referencing them, e.g., by calling CCSprite spriteWithSpriteFrameName. In that timeframe, your app is in a critical situation: if a memory warning comes in, cocos2d will empty its texture cache; the textures you were simply pre-caching for later use will be removed; and when you finally try to use them in a sprite, your app will crash.
I could fix this problem by calling
[[CCDirector sharedDirector] purgeCachedData];
before attempting to pre-cache the textures. This could work or not work for you depending on the size of the textures you are trying to pre-cache. If it does not, there is another approach, which is slightly more draconian. In fact, the reason why cocos2d empty its cache when a memory warning is received lays with how the cocos2d template application delegate handles applicationDidReceiveMemoryWarning. If you inspect this method, you will find a call to purgeCachedData.
You can remove that call to fix your problem, but then you should not forget to call it from time to time, say, e.g., each time you load a new scene, or implement a more complex controller-base cache purging scheme, otherwise your cache will surely kill your app.
iPhone or Android?
I am not really interested in the fight between iPhone’s and Android’s aficionados, I believe that they are two different platforms generating products with different specifications and targets. Anyway, I would like to share the following bit by Nick Farina (via Daring Fireball) on a difference in how iPhone and Android handle animations:
Adding more SDKs to your iPhone simulator
Great tip from here.
Drive: are we human or ... ?
An impressive piece of visual communication about what motivates our behaviour and the importance of autonomy, mastery, and purpose. This is based on a speech of David Pink’s, so it is spelled in highly “economic-ist” terms, but it is easy to see the deep meaning of those words.