我很难找到内存泄漏。我正在使用 cocos2d。这是两个类的数据区:
@interface Dungeon : CCLayerColor {
DungeonLevel *aDungeonLevel;
Player *thePlayer;
// list of all monster file names
NSMutableArray *monsterNames;
// array of how many monsters there are of each monster level
NSMutableArray *monsterLevels;
MessageView *theMessageView;
DungeonDisplay *theDisplay;
bool processing;
int currentDungeonLevel;
}
@interface DungeonDisplay : CCLayerColor {
NSMutableArray *displayGrid;
NSMutableArray *displayGrid2;
NSMutableArray *displayGrid3;
NSMutableArray *displayGrid4;
NSMutableArray *dungeonMatrix;
NSMutableArray *monsterSprites;
Dungeon *theDungeon;
int xdelt;
int ydelt;
CGPoint lowerLeft;
Player *thePlayer;
CCSprite *playerSprite;
CCSprite *mSprite1;
ButtonsLayer *buttonArea;
double previousTime;
double currentTime;
double touchTimePrev;
bool touchFlag;
bool processing;
bool processing2;
bool animating;
bool flipSprite;
bool doIdleAnimation;
bool isAttacking;
int firstIteration;
CGPoint dungeonOriginalPosition;
CGPoint playerOriginalPosition;
CGPoint mSprite1Original;
CGPoint buttonOriginal;
CCTimer *myTimer;
// List of Messages
NSMutableArray *messages;
int messageIndex;
// player transparency level
int transparency;
// indicates that walls need to become transparent
bool needTransparency;
int pXInc;
int pYInc;
int tempx;
int tempy;
// debugging variables
CCLabelTTF *debugLabel1;
CCLabelTTF *debugLabel2;
// the Map
MiniMap *aMap;
}
好的,现在 Dungeon 对象通过与另一个对象 DungeonLevel 交互来创建 DungeonDisplay 对象(我认为这与找出 DungeonDisplay 未释放的原因特别相关)。这是创建“单例”DungeonDisplay 对象的所有代码:
-(void) displayDungeon
{
if (!theDisplay) {
theDisplay = [[DungeonDisplay alloc]init];
[self addChild:theDisplay z:101];
[theDisplay letTheDungeon:self];
}
else {
[thePlayer placePC:thePlayer.pCLocation];
[theDisplay displayStructure];
}
theDisplay.visible = true;
aDungeonLevel.visible = NO;
}
出于某种原因,在 addChild(一个 cocos 方法)之后,保留计数跳到 4(从 1)。“letTheDungeon”对保留计数没有影响(如预期的那样)。