开发者

Few questions on iphone memory management and cocos2d

I'm working on my first app and have a few questions on memory management.

First Question:

I'm working on an intro scene that looks like this

#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

Have I released everything correctly, and a开发者_运维技巧voided leaks? I ran it in instruments multiple times and it found no leaks and used about 2mb of memory, is that to much or the amount to be expected? Also does the dealloc method get called when the scene is replaced?

Question 2:

My main menu is set up like this

#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

Once again am I doing everything correctly? The layer ControlLayer I'm adding to this scene contains a directional pad sprite that the user uses to navigate the menu. In instruments It also confirms that their is no memory leaks, and it uses 4.79 mb of memory. Once again is that a reasonable amount? I will most likely switch to using AtlasSprite and AtlastSpriteManager to conserve memory.

I'm new to cocos2d, so if you see I'm doing anything wrong point it out! I'd rather fix bad habits in the early stages. And if you have any future tips for memory management please share.


Don't release logo, label, or background. You didn't alloc/copy/new/retain them, so you don't own them and must not release them.

I assume the controllerLayer property has the retain attribute? If not, you probably mean to do so.

In general I'd suggest two things going forward.

  1. Read and understand the Cocoa Memory Management Fundamentals
  2. Run the Clang analyzer on your code. This is available in Xcode 3.2 via Build->Build and Analyze. It will help detect these memory issues.

Also check out this SO question.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜