ZhouWeikuan/cocos2d — Issue #54: GLException: out of memory on large project
Author: gazuko
Created: 2011-12-26 18:30:00+00:00
State: open

I have a project with 17 scenes and approximately 40Mb images. On every scene is about 10-20 Sprites with animation.
If i change scenes from first to last and back - application (device - Samsung Galaxy Tab 10.1, Android 3.2) have a dumped and closed.

I set
mGLSurfaceView.setDebugFlags(CCGLSurfaceView.DEBUG_CHECK_GL_ERROR);
in Activity, and see in LogCat, that is error:
12-26 22:05:45.690: ERROR/AndroidRuntime(11013): FATAL EXCEPTION: GLThread 10
12-26 22:05:45.690: ERROR/AndroidRuntime(11013): android.opengl.GLException: out of memory
12-26 22:05:45.690: ERROR/AndroidRuntime(11013): at android.opengl.GLErrorWrapper.checkError(GLErrorWrapper.java:62)
12-26 22:05:45.690: ERROR/AndroidRuntime(11013): at android.opengl.GLErrorWrapper.glDrawArrays(GLErrorWrapper.java:258)
12-26 22:05:45.690: ERROR/AndroidRuntime(11013): at org.cocos2d.nodes.CCSprite.draw(CCSprite.java:915)
12-26 22:05:45.690: ERROR/AndroidRuntime(11013): at org.cocos2d.nodes.CCNode.visit(CCNode.java:740)
12-26 22:05:45.690: ERROR/AndroidRuntime(11013): at org.cocos2d.nodes.CCNode.visit(CCNode.java:746)
12-26 22:05:45.690: ERROR/AndroidRuntime(11013): at org.cocos2d.nodes.CCNode.visit(CCNode.java:746)
12-26 22:05:45.690: ERROR/AndroidRuntime(11013): at org.cocos2d.transitions.CCTransitionScene.draw(CCTransitionScene.java:76)
12-26 22:05:45.690: ERROR/AndroidRuntime(11013): at org.cocos2d.nodes.CCNode.visit(CCNode.java:740)
12-26 22:05:45.690: ERROR/AndroidRuntime(11013): at org.cocos2d.nodes.CCDirector.drawCCScene(CCDirector.java:716)
12-26 22:05:45.690: ERROR/AndroidRuntime(11013): at org.cocos2d.nodes.CCDirector.onDrawFrame(CCDirector.java:665)
12-26 22:05:45.690: ERROR/AndroidRuntime(11013): at org.cocos2d.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1245)
12-26 22:05:45.690: ERROR/AndroidRuntime(11013): at org.cocos2d.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1056)

I insert releasing textures in my scenes and CCTextureCache, something like this:

public void removeUnusedTextures() {
HashMap tempSet = (HashMap) _texturesRefCount.clone();
for (String key : tempSet.keySet()){
String nKey = key;
if (_texturesRefCount.get(nKey) <= 0){
WeakReference texWR = textures.get(nKey);
if (texWR != null){
CCTexture2D tex = texWR.get();
if (tex != null){
tex.releaseTexture(CCDirector.gl);
}
}
textures.remove(nKey);
_texturesRefCount.remove(nKey);
}
}
}
And call it before scene transitions do. Size of CCTextureCache.textures hashmap not exceed of 50-60 cached textures.
This is a decrease a chance to application crash and i can do more scene transitions, than before, but i still to get crash after a some time.
_texturesRefCount - is a hashmap, i add it to CCTextureCache and increase/decrease counters for my textures.

I use sources, not a jar. Also i see all issues on this subject there and in other places, and try some solutions - noone was to work correct.

What's right way to release resources between different scenes? Does cocos2d automatically texture release, or i need to release it manually?