场景多的 Cocos2D 项目没有正确释放内存 [英] Cocos2D project with many scenes does not release memory properly

查看:23
本文介绍了场景多的 Cocos2D 项目没有正确释放内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个很大的问题,我不太明白为什么会发生.是这样的:

I've got a great problem and I don't understand very well why occurs. This is the case:

  • 在 Cocos2D 中有一个很棒的项目,包含 10 个场景.每个场景都是一本书的一页,上面有巨大的精灵.它使用 Kobold2D 1.0.2 实现.
  • 每个页面都有一个单例类中的公共对象,通过 LayerColor 放置一个公共菜单.
  • 精灵是 PVR.CCZ RGBA4444 和 iPad 内存中的 TexturePacker每个加载的 spritesheet 大约 16-20Mb.
  • 我使用 CCTransitionTurnPage 替换场景,以替换下一个.
  • 在每个页面(类)的init方法中,加载纹理和FrameFile.
  • 在每个页面(类)的onExit方法中,卸载纹理和frameFile.我使用了 dumpCachedTextureInfo 并说我可以完美地从内存中加载和卸载纹理.
  • 当然,我会从子对象中删除所有对象.我所有的精灵都是在 .h 的接口部分声明的公共变量,因为我需要在类的每个方法中访问它们.
  • 我的项目是在 ARC 项目中使用 Kobold2D 集成制作的(但您知道附加的 Kobold2D 项目由于兼容性问题而未启用 ARC)

事实上,当我开始这个项目时,一切看起来都很完美,但我制作的每个场景(页面)的内存都在增加.Page1: 30Mb., Page2: 40, Page 3: 54, Page 4: 65... 在 7 或 8 个场景之后,Instruments、Xcode 或 iPad 本身挂起应用程序而没有任何消息(除了具有最终低内存警告的 Instruments).

The fact is when I start the project, everything seems perfect but memory is increasing for every scenes (page) I made. Page1: 30Mb., Page2: 40, Page 3: 54, Page 4: 65... After 7 or 8 scenes, Instruments, Xcode or the iPad itself hangs the application without any message (except Instruments with a final Low memory Warning).

为什么每个场景都没有释放内存?也许是因为 ARC 和没有 super dealloc 变量.为什么纹理看起来完美卸载但似乎没有卸载,因为内存在没有控制的情况下增长直到崩溃?

Why there's no memory releasing after each scenes? Maybe it's because ARC and the no super dealloc variable. Why textures appears unload perfectly but seems like there's no unload because memory is growing without control until crash?

推荐答案

我在内存保留方面遇到了类似的问题,并且仪器中没有出现泄漏.在我将以下内容写入每个场景的 .m 文件之前,我什至无法调用 -(void) dealloc:

I was having a similar problem with memory retention and no leaks showing up in instruments. I couldn't get -(void) dealloc to even get called until I wrote the following into every scene's .m file:

-(void) onExit {
    //unschedule selectors to get dealloc to fire off
    [self unscheduleAllSelectors];
    //remove all textures to free up additional memory. Textures get retained even if the sprite gets released and it doesn't show as a leak. This was my big memory saver
    [[CCTextureCache sharedTextureCache] removeAllTextures];
    [super onExit];
}

在实现这个之后,我的内存在每次调用 replaceScene: 后被释放.希望对你有所帮助.

After implementing this, my memory was released after every replaceScene: was called. Hope this is of some use to you.

这篇关于场景多的 Cocos2D 项目没有正确释放内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆