内存泄漏:收到内存警告。等级= 1& 2在cocos2d中

时间:2011-04-07 11:37:24

标签: memory-leaks cocos2d-iphone

我是cocos2d的新手,面临记忆问题 我得到记忆警告等级1& 2.

  1. 我的游戏中有两个场景。

  2. 在第一个场景(mainmenu)中我有按钮(点击它会用第二个替换场景,即(StartMoving))

  3. 在StartMoving.h文件中我有初始化的精灵,数组(我在dealloc方法中取消分配)

  4. 在我的StartMoving的init方法中,我初始化了背景Image。(在dealloc方法中重新定义)并转到Allreset方法。

  5. 这是我的Allreset方法的代码。

  6. - (无效)AllReset
    {
    而(X == 5)
        {
            X = 0;
            [[CCDirector sharedDirector] replaceScene:[mainmenu scene]]; //当X = 5

    时替换场景
    }  
    
    CCLOG(@"%@: %@",NSStringFromSelector(_cmd),self);  
    CCSprite *tra=[CCSprite spriteWithFile:@"tra.png"];             
        tra.position =ccp(800,115);                                     
        [self addChild:tra z:0 tag:2];                                  
            [tra1 insertObject:tra atIndex:0];      //tra1 is NSMutablearray which is realeased in dealloc                       
    
    NSLog(@"tra is loaded....");  
    
    //[[SimpleAudioEngine sharedEngine] playEffect:@"police.wav"];      
    
    NSArray *name14 =[NSArray arrayWithObjects:@"app1.plist",@"app2.plist",@"app3.plist",@"app4.plist",@"app5.plist", nil];                                         
    
    NSArray *name24 =[NSArray arrayWithObjects:@"app1.png",@"app2.png",@"app3.png",    @"app4.png",@"app5.png",nil];                                            
    
    NSArray *name34 =[NSArray arrayWithObjects:@"a",@"b",@"c",@"d",@"e",nil];  
    
    NSString *file14=[name14 objectAtIndex:x];      
    NSString *file24=[name24 objectAtIndex:x];  
    NSString *file34=[name34 objectAtIndex:x];  
    
    int frame[]={3, 7, 11, 4, 5};             
    float Fdelay[]={0.15, 0.2, 0.3, 0.1,0.5};           
    
    CCSpriteFrameCache *frameCache4 =[CCSpriteFrameCache sharedSpriteFrameCache];  
    [frameCache4    addSpriteFramesWithFile:file14];  
    CCSpriteBatchNode *danceSheet4 = [CCSpriteBatchNode batchNodeWithFile:file24];  
    [self addChild:danceSheet4];  
    
    CCSprite *sprite4 = [CCSprite node];  
    sprite4.position = ccp(395,155);  
    [tra addChild:sprite4 z:1 tag:14];  
    
    NSMutableArray *animFrames4 = [NSMutableArray arrayWithCapacity:frame[x]];  
    for(int i = 1; i <= frame[x]; i++) {  
        NSString *namef4=[NSString stringWithFormat:@"%@%i.png",file34,i];  
        CCSpriteFrame *frame4 = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:namef4];  
        [animFrames4 addObject:frame4];  
    }  
    
    CCAnimation *anim4 = [CCAnimation animationWithFrames:animFrames4 delay:Fdelay[x]];  
    CCAnimate *animN4 = [CCAnimate actionWithAnimation:anim4];  
    CCRepeatForever *repeat4 = [CCRepeatForever actionWithAction:animN4];    
    [sprite4 runAction:repeat4];   
        id move= [CCMoveTo actionWithDuration:5.5f position:CGPointMake(240,115)];            
    id easeout=[CCEaseOut actionWithAction:move rate:1.5f];  
    id menudisp=[CCCallFuncN actionWithTarget:self selector:@selector(menudisplay:)];  
    [train1 runAction:[CCSequence actions:easeout,menudisp, nil]];   
    

    }

    1. 我这里只显示了一个动画,但同一个精灵上有7个动画。

    2. 当tra在给定点停止时,将显示菜单标签。当用户点击标签时,它开始朝同一方向移动。走出屏幕。一旦整个精灵走出屏幕,就会调用一个方法来清理tra的记忆(我想只有tra的孩子才会被清理。如果我错了,请纠正我。)

    3. 清理内存后,它将再次使用增加的x值调用Allreset方法。

    4. ` - (无效)Incrementingx
      {     X ++;
          [self Allreset];
      }

      `

      我还在appdelegate.m的applicationDidReceiveMemoryWarning方法中使用了以下内容

      [[CCSpriteFrameCache sharedSpriteFrameCache] removeUnusedSpriteFrames];

          [[CCTextureCache sharedTextureCache] removeUnusedTextures];
          // [[CCDirector sharedDirector] purgeCachedData]; //注意这是评论

      应用程序在模拟器上运行完全正常但在设备上:(
      请帮帮我:(

3 个答案:

答案 0 :(得分:4)

在我使用大量精灵表和精灵表动画时,我遇到了类似的问题。使用简单的动画。同时调用不同的场景或在场景之间切换。这些都是我所做的。

  1. 在dealloc中添加以下代码,而不是在delegate.m
  2. //IF you have particular spritesheets to be removed! Don't use these if you haven't any
    [[CCSpriteFrameCache sharedSpriteFrameCache]removeSpriteFramesFromFile:@"ufoRotateThird2.plist"];
    [[CCSpriteFrameCache sharedSpriteFrameCache]removeSpriteFramesFromFile:@"ufoRotateSecond1.plist"];
    [[CCSpriteFrameCache sharedSpriteFrameCache]removeSpriteFramesFromFile:@"explode.plist"];
    [[CCSpriteFrameCache sharedSpriteFrameCache]removeSpriteFramesFromFile:@"explodeR.plist"];
    
    //Use these
    [[CCSpriteFrameCache sharedSpriteFrameCache] removeSpriteFrames];
    [[CCSpriteFrameCache sharedSpriteFrameCache] removeUnusedSpriteFrames];
    
    
    //Use these
    [[CCTextureCache sharedTextureCache] removeUnusedTextures];
    [[CCTextureCache sharedTextureCache] removeAllTextures];
    [[CCDirector sharedDirector] purgeCachedData];
    
    //Try out and use it. Not compulsory
    [self removeAllChildrenWithCleanup: YES];
    
    1. 在init中,首先取消选中所有选择器。
    2. 场景切换时取消预定所有选择器

答案 1 :(得分:1)

之前我遇到过这个记忆警告。我只是在iOS设备上,而不是在模拟器上,我发现它是因为运行背景太多了(iOS 4.x)。只需关闭这些应用程序,警告就会消失。

答案 2 :(得分:1)

而是删除dealloc.Better放入onExit。因为当你想在下一个场景中添加精灵帧时,dealloc有时会引起问题。

-(void) onExit{
    [[CCSpriteFrameCache sharedSpriteFrameCache] removeSpriteFrames];
    [[CCSpriteFrameCache sharedSpriteFrameCache] removeUnusedSpriteFrames];
    [super onExit];
}