关于iphone内存管理和cocos2d的几个问题

时间:2009-10-17 00:57:53

标签: iphone objective-c memory-management cocos2d-iphone

我正在处理我的第一个应用程序,并对内存管理有一些问题。

第一个问题:

我正在制作一个看起来像这样的介绍场景

#import "Intro_Scene.h"
#import "Main_Menu.h"
#import "Label.h"


@implementation Intro_Scene
@synthesize logo,label;

-(id) init
{
 self = [super init];

 if(self != nil)
 {
  //Load logo image and set position
  logo = [Sprite spriteWithFile:@"AVlogo_1.png"];
  logo.position = ccp(-50, 0);
  logo.scale = 1.8f;
  [self addChild: logo];

  //Creates 3 actions for the logo sprite
  id action0 = [MoveTo actionWithDuration:0 position:ccp(160,270)];
  id action1 = [FadeIn actionWithDuration:3];
  id action2 = [FadeOut actionWithDuration:3];

  //Logo runs the actions
  [logo runAction: [Sequence actions:action0,action1, action2, nil]];

  //Schedules the changeScene method to switch scenes to main menu within 6 seconds of loading.
  [self schedule: @selector(changeScene) interval:6.0f];

  //Creates a label and positions it, Alternative Visuals
  label = [Label labelWithString:@"Alternative Visuals" fontName:@"Verdana" fontSize:22];
  label.position = ccp(160, 120);
  [self addChild:label];

 }

 return self;
}

//Method called after intro has run its actions, after 6 seconds it switches scenes.
-(void)changeScene
{
 [self removeChild:logo cleanup:YES];
 [self removeChild:label cleanup:YES]; 

 Main_Menu *mainMenu = [Main_Menu node];
 [[Director sharedDirector] replaceScene: mainMenu];
}

-(void)dealloc
{
 [[TextureMgr sharedTextureMgr] removeUnusedTextures];
 [label release];
 [logo release];
 [super dealloc];
}
@end

我是否正确释放了所有内容,并避免了泄漏?我在仪器中多次运行它并且发现没有泄漏并且使用了大约2mb的内存,这是多少或预期的数量?还是在替换场景时调用dealloc方法吗?

问题2:

我的主菜单设置如下

#import "Main_Menu.h"
#import "Sprite.h"
#import "cocos2d.h"

@implementation Main_Menu
@synthesize background, controlLayer;
-(id) init
{
 self = [super init];
 if(self != nil)
 {
 //Create the default background for main menu not including directional pad and highlight box
 background = [Sprite spriteWithFile:@"Main_Menu_bg.png"];
 background.position = ccp(160,240);
 [self addChild:background];

  //Adds the control later class to the main menu, control layer class displays and controls the directional pad and selector. 
  ControlLayer *layer = [[ControlLayer alloc] init];
  self.controlLayer = layer;
  [layer release];
  [self addChild: controlLayer];
 }

 return self;
}

-(void) dealloc
{
    [seld removeChild:background cleanup:YES];
    [[TextureMgr sharedTextureMgr] removeUnusedTextures];
 [background release];
 [controlLayer release];
 [super dealloc];
}


@end

我再一次正确地做了一切吗?我添加到此场景的图层ControlLayer包含用户用来导航菜单的方向键盘精灵。在仪器中它还确认它们没有内存泄漏,它使用4.79 mb的内存。再一次是合理的数额?我很可能会转而使用AtlasSprite和AtlastSpriteManager来节省内存。

我是cocos2d的新手,所以如果你看到我做错了什么就指出来了!我宁愿在早期阶段修复坏习惯。如果您有任何未来的内存管理技巧,请分享。

1 个答案:

答案 0 :(得分:3)

不要发布徽标,标签或背景。您没有分配/复制/新建/保留它们,因此您不拥有它们并且不得释放它们。

我假设controllerLayer属性具有retain属性?如果没有,你可能意味着这样做。

总的来说,我建议前进两件事。

  1. 阅读并理解Cocoa Memory Management Fundamentals
  2. 在代码上运行Clang analyzer。这可以在Xcode 3.2中通过Build-> Build and Analyze获得。它将有助于检测这些内存问题。
  3. 另请查看this SO question.