ZhouWeikuan / cocos2d

cocos2d for android, based on cocos2d-android-0.82, and now ported from cocos2d-iphone 0.99.4. The googlecode address is here: http://code.google.com/p/cocos2d-android-1/ . There are several demos to watch.
610 stars 291 forks source link

GLException: out of memory on large project #54

Open gazuko opened 12 years ago

gazuko commented 12 years ago

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<String, Integer> tempSet = (HashMap<String, Integer>) _texturesRefCount.clone();
    for (String key : tempSet.keySet()){
        String nKey = key;
    if (_texturesRefCount.get(nKey) <= 0){
            WeakReference<CCTexture2D> 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?

gazuko commented 12 years ago

Problem is resolved, if i call CCTextureCache.removeAllTextures every time before new scene build. But this is not good way, really?