【发布时间】:2013-09-04 13:40:00
【问题描述】:
我正在使用 ARC 和 Cocos2d 2.0 作为静态库(它不使用 ARC 并且被编译为单独的目标)。我翻译了一个旧项目(没有 ARC),我想知道以这种方式声明属性是否有一些潜在的保留周期问题:
@interface PlayerData : NSObject <NSCoding> {
}
//Is ok to save this as if the game gets paused you may want to save this.
@property (readwrite, nonatomic) int numberOfHits;
@property (readwrite, nonatomic) bool everBeenHitInCurrentLevel;
@property (readwrite, nonatomic) int hitsForOneHearth;
我注意到我的各种场景会随着时间的推移而积累起来。此外,我在 CCLayer 方法(MyScene : CCLayer)的 release 方法中添加了一个 CCLOG 调用,它永远不会被调用。这就是我创建场景的方式(我使用“[CCDirector sharedDirector] replaceScene”方法替换它
+ (id) sceneWithLevelName:(LevelName)name
{
CCScene *scene = [CCScene node];
ShooterScene * shooterLayer = [[self alloc] initWithId:name];
[scene addChild:shooterLayer];
return scene;
}
编辑:当我意识到我愚蠢地没有包含一个带有对象的示例并且只使用原始数据类型时,我将在我的场景中粘贴一些元素的 sn-ps:CharacterSelection、ShooterScene 和 PlanetSelectionMenu:
//ShooterScene
@interface ShooterScene : CCLayer {
HudLayer * hudLayer;
....
}
@property(readwrite, nonatomic) CCSpriteBatchNode* backgroundAndEnemiesBatchNode;
@property(readwrite, nonatomic) ShipEntity* playerShip;
... etc..
Please note that I do not declare member variables for properties like playerShip and backgroundAndEnemiesBatchNode beause, as far as I can understand, should suffice the property declaration (but please correct me if I am wrong or if the approach may cause issues).
//CharacterSelectionScene
@interface CharacterSelectionScene : CCLayer {
int currentlySelectedCharacterSpriteTag;
CCSprite * lights;
CCLayer * spritesLayer;
...
}
//PlanetSelectionMenu
#import <Foundation/Foundation.h>
#import "cocos2d.h"
@interface PlanetSelectionMenu : CCLayer {
CCLayer * backgroundLayer; // Added background images here
CCLayer * alwaysPresentItems;
}
+ (id) scene;
@end
请注意,每次我从 PlanetSelectionMenu 转到 CharacterSelectionScene - 反之亦然 - 记忆都会增加。但是在这种情况下,我没有使用任何属性,而是“只是”将对象(CCSprites)添加到图层和批处理节点。
编辑 2:这是我在通过 Menu->CharacterSelection->PlanetSelectionScene 等运行时在分配中看到的内容。似乎 LiveBytes 平均为 4MB,因为当时我只看到一个场景,所以我假设没有保留周期。那我为什么会收到那些讨厌的 LOW 内存信息呢?
【问题讨论】:
-
标准 C 语言数字数据类型(int、bool、double 等)无法创建保留循环,因为它们不指向任何内容。如果您期望一个对象被释放,而事实并非如此,那么您可能有另一个对象具有指向它的强指针。哪些对象具有指向
MyScene的强指针?你认为他们应该解除分配吗? -
@AaronBrager 好点。我添加了一个更好的例子,包括对象而不是原始数据类型。我指的是 PlanetSelectionMenu 的 CCLayer(实际上是一个场景 - 选择了误导性名称)和 CharacterSelectionScene,因为当调用 replaceScene 时,dealloc 方法不会打印我添加的 CCLOG 语句。
标签: ios objective-c cocos2d-iphone automatic-ref-counting