Texture flushed when coming back from background

On my game, some people experience what seems to be a texture cache being flushed. It does not happen very often and seems to happen when people exit the game and return later. I use 3.17

See the image below:

For Android, on my applicationDidEnterBackground() I do nothing (not even pausing or stopping animation since it was causing issues with ads). Same when coming back, I do nothing.

Any idea?

Are you using a texture map or are they all individual images that’s moving in and out of cache could it be phone freeing up memory space while in background? I have not had this but I use Texture map one big image. ohh edit is it you need to say sprite->retain();
to keep it then you have to sprite->release();

I use texturepacker and I have a few big 1024x1024 textures loaded with:

SpriteFrameCache::getInstance()->addSpriteFramesWithFile.

Then I use:

initWithSpriteFrameName();

to assign them to Sprites. Nothing fancy.

My game is tiled base and looking at the screenshot, I can see the tiles but their textures are pointing to a different texture it seems.

I use same thing but I call createWithSpriteFrameName();

 /** Add you SpriteSheet Name Here **/
SpriteFrameCache::getInstance()->addSpriteFramesWithFile("SpriteSheet01.plist");

//Create sprite
auto sprite = Sprite::createWithSpriteFrameName(“mySpriteName.png”);
addChild( sprite, 1);

are you loading a few SpriteSheets at the same time rather then one big one ?

I don’t think createWithSpriteFrameName or initWithSpriteFrameName changes anything. Just 2 different ways to initialize the sprites

I do load multiple big sprite sheet textures (like 4-5 between 1024x1024 and 2048x2048).

This is working fine for 99% of the people and only fails for 1% when they exit the game for a while and come back.

sound like there out cache or something Texture packer has a get upset if you go over 2048x 2048 size so I wonder if its phone needing resource when they go background. What happens if you do a cocos2dx ->retain(); thought that was meant to stop the flushing? dose it report “Texture flushed” because in Cocos2d-x is not an error message, but rather a notification that the graphics hardware has run out of memory. I not experienced this myself hopefully some one can help.

For android, check this thread to see if anything in there may help:

https://discuss.cocos2d-x.org/t/restart-application-instead-of-reload-all-resources-when-opengl-es-context-lost-on-android/45074

Thanks. This is probably what is happening. But I see that the solution they propose is to close the app if the context is lost. But I do not see how they achieve it. The online line of code mentioned is already in the current version of 3.17.2.

In that case, you can work around this issue by reloading everything. How you handle that is entirely up to you, but a trick I personally use is to add a dummy cocos2d::Scene at the start of the scene stack, and when you detect that you need to reload, you use popToRootScene or popToSceneStackLevel to get back to it, and then do a pushScene instantly with the first real scene that will load the required resources.

The reason for this is that everything in the popped scenes should be unloaded and those scenes freed, so effectively you’re starting all over again without having dangling resources in memory.

Thanks. But how do you “detect that you need to reload” ? Is there a flag somewhere telling you “Your textures are unloaded” or something?

How you detect the need to reload resources is up to you. You could add an event listener for EVENT_COME_TO_BACKGROUND, and every time you receive it, check if the texture is cached via TextureCache::getTextureForKey(). That event is used if CC_ENABLE_CACHE_TEXTURE_DATA is enabled, which seems to be the case for Android.

Ok, thanks. But I suppose I do not need the event listener. This should do the trick:

applicationWillEnterForeground() in the AppDelegate class