cocos ios下内存警报的坑!!!! - pxqwxl/myLearnPoject GitHub Wiki

在ios下内存警报时 cocos会对纹理进行释放从而减少内存 在cocos工程里 appcontroller类中存在这样一行代码

  • (void)applicationDidReceiveMemoryWarning:(UIApplication )application { / Free up as much memory as possible by purging cached data objects that can be recreated (or reloaded from disk) later. */ cocos2d::Director::getInstance()->purgeCachedData(); }

这行代码当ios出现内存报警时会被调用 调用了director中的purgeCachedData方法

void Director::purgeCachedData(void) { FontFNT::purgeCachedData(); FontAtlasCache::purgeCachedData();

if (s_SharedDirector->getOpenGLView())
{
    SpriteFrameCache::getInstance()->removeUnusedSpriteFrames();
    _textureCache->removeUnusedTextures();

    // Note: some tests such as ActionsTest are leaking refcounted textures
    // There should be no test textures left in the cache
    log("%s\n", _textureCache->getCachedTextureInfo().c_str());
}
FileUtils::getInstance()->purgeCachedEntries();

} 这个方法会对fnt atlas 和spriteframe进行释放 所以没有手动retain并且未被引用的部分资源会被释放 这时去查找该spriteframe会导致spriterframe找不到从而崩溃