crash on Surface Re-Create

crash on surface re-Create
a lost opengl context on android destroys all glTextures.
cocos2dx should reload or recreate all lost textures. this don’t look possible yet with the current framework.

currently cocos2d crashes when screen changes orientation
or when the game comes back to the foreground after being paused

currently cocos2d crashes when screen changes orientation

Did you mean helloworld? It is ok now.

The question that opengl context would be lost after being paused is a big question. I have not any good solution from now on.

I have viewed the source code of cocos2d-android-1, it reload all the resources when re-create opengl context. I don’t think it is a good method. I will research good method.

in my other games, using my own engine (very simple engine), i found no other solution but had to reload the textures.
i hope you find a good solution. take a look at love2d engine

pretty easy to hack cxtexturecache to reload.

void CCTextureCache::reloadAllTextures()
{
    std::vector keys = m_pTextures->allKeys();
    std::vector::iterator it;
    for (it = keys.begin(); it != keys.end(); it++)
    {
        CCTexture2D *texture = m_pTextures->objectForKey(*it);
        // prevents overloading the autorelease pool
        UIImage * image = new UIImage();
        if(image->initWithContentsOfFile((*it), kImageFormatPNG)) // jpeg? :P
        {
            texture->initWithImage(image);
            CCX_SAFE_DELETE(image);// image->release(); 
        } else {
            delete image;
        }
    }
}

android.opengl.GLSurfaceView.Renderer

??
EGL Context Lost
There are situations where the EGL rendering context will be lost. This typically happens when device wakes up after going to sleep. When the EGL context is lost, all OpenGL resources (such as textures) that are associated with that context will be automatically deleted. In order to keep rendering correctly, a renderer must recreate any lost resources that it still needs. The onSurfaceCreated(GL10, EGLConfig) method is a convenient place to do this.
??

i think we all have no choice but to reload textures. for dynamic textures like the atlas - we will have to find the best cross platform solution

i’ve hacked android version to behave properly on lost/recreated opengl context
(note:hacked/dirty coding)

https://github.com/icedman/cocos2d-x

solution:

  1. save full path of textures that are loaded from a file. trapping is done the CCTextureCache
  2. save *data from textures that are created initWithData. trapping is done in CCTexture2D::initWithData
  3. textures not loaded from CCTextureCache will use step #2

fixes:
glBufferData is placed on ::draw() & ::drawQuads. buffer data is also lost when context is lost

tested on emulator 2.0, nexus 2.2

Thank you for your contributions. It is a good start point for me to resolve the problem.

Any progress here? :slight_smile:

I can’t use the hacked version with the VolatileTexture because I need the memory. Wouldn’t it be a better idea to keep the VolatileTexture from doubling the texture data until the app is about to pause?

(and of course, the double data should then be deleted again when resuming the app, right after reuploading to GL)

You can take a look at this issue: #374. It have been fixed.
We cache the texture info instead of cache the data now. And the change will be published next version.

Also, you can get the edge version of engine from https://github.com/cocos2d/cocos2d-x.
Hope it’s helpful!

Woops, didn’t see that one. Looks to be a great solution, thanks!

I’ll be updating to this bleeding edge now :slight_smile: