2

我最近将现有的 Cocos2D 项目从 0.8 版迁移到 2.0 版并启用了 ARC。

我这样做的方式是使用 Apple 的空应用程序模板,然后添加来自 Cocos2d 2.x 模板的代码,因为它有重大变化。之后,我添加了游戏中的代码并对已弃用的代码和 ARC 问题进行了必要的更改。

由于游戏正在运行,但不如预期,我没有动画并且游戏占用了整个 CPU 资源。从控制台我看到一切都在创建后立即被释放。我的旧代码不是原因,因为它甚至发生在我的任何场景被推送之前。

在此处输入图像描述

编辑 我还再次重复了整个过程并从 Cocos2D 模板项目中制作了一个支持 ARC 的版本,但那里也一样。这可能是正常的事情吗?

4

1 回答 1

1

That's not normal, although common problem when converting to ARC. ARC will release objects out of scope, whereas under MRC an alloc/init object would stay in memory (and leak). Check where you may need to keep a strong reference.

Here's an example that worked before converting to ARC:

-(void) someMethod
{
    id object = [[MyObject alloc] init];
}

Under MRC, object stays in memory (leaks) after someMethod returns. Under ARC, ARC cleans up the object when the method returns. The simplest fix is to turn object into an ivar (aka instance variable, member of class).

Also check singletons. Depending on how its implemented, the Singleton class might dealloc right away. For example if the static instance is declared __weak or __unsafe_unretained.

You should also run the Xcode Analyzer (Build -> Analyze) to get pointers for potential issues.

于 2012-10-01T11:28:28.313 回答