Memory management Cocos2d-RemovePriteFrames和removeAllTextures不能立即工作

Memory management Cocos2d-RemovePriteFrames和removeAllTextures不能立即工作,memory-management,cocos2d-iphone,crash,textures,Memory Management,Cocos2d Iphone,Crash,Textures,编辑: 我在前一个场景的dealloc和cleanup方法中添加了以下代码(我从中调用ReplaceSecene,但它没有任何效果。即使在创建InstructionScene后的1/2/5秒后,内存仍然包含上一个场景中的资源。强制删除这些资源的唯一方法是在创建场景0.1f秒后将其从新场景中删除(通过回调)。这有点奇怪。 以下是以前的场景清理和daelloc方法的代码: -(void) cleanup { CCLOG(@""); CCLOG(@""); CCLOG(@"Pl

编辑: 我在前一个场景的dealloc和cleanup方法中添加了以下代码(我从中调用ReplaceSecene,但它没有任何效果。即使在创建InstructionScene后的1/2/5秒后,内存仍然包含上一个场景中的资源。强制删除这些资源的唯一方法是在创建场景0.1f秒后将其从新场景中删除(通过回调)。这有点奇怪。

以下是以前的场景清理和daelloc方法的代码:

-(void) cleanup
{
    CCLOG(@"");
    CCLOG(@"");
    CCLOG(@"PlanetSelection Menu cleanup");
    CCLOG(@"");
    [super cleanup];
    [planetLayer removeAllChildrenWithCleanup:YES];
    [self removeAllChildrenWithCleanup: YES];

    [[CCSpriteFrameCache sharedSpriteFrameCache] removeSpriteFramesFromFile:textureFileName];
    [[CCTextureCache sharedTextureCache] removeUnusedTextures];
    [[CCSpriteFrameCache sharedSpriteFrameCache] removeUnusedSpriteFrames];
    [CCAnimationCache purgeSharedAnimationCache];
}


-(void) dealloc
{
    CCLOG(@"Dealloc gets caled");
    [CCAnimationCache purgeSharedAnimationCache];
    [[CCDirector sharedDirector] purgeCachedData];
    [[CCSpriteFrameCache sharedSpriteFrameCache] removeUnusedSpriteFrames];
    [[CCSpriteFrameCache sharedSpriteFrameCache] removeSpriteFramesFromFile:textureFileName];
}

原始问题:

我在游戏中有几个场景,到目前为止,我在每个场景的开头都使用了以下代码来删除以前存储的纹理。但是有一种情况是这样做不起作用的:当我用一个新场景(我们称之为a)替换一个场景(我们称之为B)时,没有精灵,只有一些从字体图像表创建的标签

[[CCDirector sharedDirector] replaceScene: [InstructionsScene sceneWithLevelName:FIRST_LEVEL]];
新对象创建得太快,因为以下调用似乎没有任何效果:

-(id) initWithLevelName:(LevelName)name
{
    if ((self = [super init]))
    {
        //Remove stuff from previous scene
        [[CCSpriteFrameCache sharedSpriteFrameCache] removeSpriteFrames];
        [[CCSpriteFrameCache sharedSpriteFrameCache] removeUnusedSpriteFrames]; //Not really necessary


        //Use these
        [[CCTextureCache sharedTextureCache] removeUnusedTextures]; //Not really needed
        [[CCTextureCache sharedTextureCache] removeAllTextures];
        [[CCDirector sharedDirector] purgeCachedData];
        [CCAnimationCache purgeSharedAnimationCache];
        ....
     }
}
在调用“替换场景”方法时,两个CCLayer(CCScane)对象同时处于活动状态。但是不会删除上一个场景中的纹理。如果在指令场景中添加并使用精灵表,则相同的代码可以完美工作。对此的一个调整是使用选择器回调,删除0.1f之后的所有纹理。,但这不是非常优雅和平滑:

 [self runAction:[CCSequence actionOne:[CCDelayTime actionWithDuration:0.1f] two:[CCCallFunc actionWithTarget:self selector:@selector(removeStuffFromPreviousScene)]]];
这是一个已知问题吗?它可能导致潜在的崩溃。

我将代码粘贴到此处,以确保您可以试用:

//
//  InstructionsScene.h
//
//  Created by mm24 on 09/09/13.
//  Copyright 2013 mm24. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "cocos2d.h"
#import "CommonEnumerations.h"

@interface InstructionsScene : CCLayer {

    LevelName levelName;
    float startTime;

    CCLabelBMFont * levelNameTitle;
    CCLabelBMFont * levelSubtitle;

    CCLabelBMFont * instructionsHeader;
    CCLabelBMFont * instructions;
}

+(id)sceneWithLevelName:(LevelName)name;
@end


//
//  InstructionsScene.m
//
//  Created by mm24 on 09/09/13.
//  Copyright 2013 mm24. All rights reserved.
//

#import "InstructionsScene.h"
#import "ShooterScene.h"
#import "AppDelegate.h"
#import "mach/mach.h"


@implementation InstructionsScene

+(id)sceneWithLevelName:(LevelName)name
{
    CCScene * scene = [CCScene node];
    InstructionsScene * layer = [[self alloc] initWithLevelName:name];

    [scene addChild:layer];
    return scene;
}


-(id) initWithLevelName:(LevelName)name
{
    if ((self = [super init]))
    {
        //Remove stuff from previous scene
        [[CCSpriteFrameCache sharedSpriteFrameCache] removeSpriteFrames];
        [[CCSpriteFrameCache sharedSpriteFrameCache] removeUnusedSpriteFrames];


        //Use these
        [[CCTextureCache sharedTextureCache] removeUnusedTextures];
        [[CCTextureCache sharedTextureCache] removeAllTextures];
        [[CCDirector sharedDirector] purgeCachedData];
        [CCAnimationCache purgeSharedAnimationCache];

        //Try out and use it. Not compulsory
        [self removeAllChildrenWithCleanup: YES];

        CCLOG(@"init with level name");
        levelName = name;
        startTime = 10.0f;

        levelNameTitle = [CCLabelBMFont labelWithString:@"Title" fntFile:@"bitmapFontTest.fnt"];
        levelNameTitle.position = CGPointMake(160.0f, 420.0f);
        levelNameTitle.anchorPoint = CGPointMake(0.5f, 0.5f);
        levelNameTitle.scale = 1.3f;
        [self addChild:levelNameTitle z:1] ;

        levelSubtitle = [CCLabelBMFont labelWithString:@"Subtitle" fntFile:@"bitmapFontTest.fnt"];
        levelSubtitle.position = CGPointMake(160.0f, 400.0f);
        levelSubtitle.anchorPoint = CGPointMake(0.5f, 0.5f);
        levelSubtitle.scale = 0.7f;
        [self addChild:levelSubtitle z:1] ;

        instructionsHeader   = [CCLabelBMFont labelWithString:@" Instructions " fntFile:@"bitmapFontTest.fnt"];
        instructionsHeader.position = CGPointMake(160.0f, 240.0f);
        instructionsHeader.anchorPoint = CGPointMake(0.5f, 0.5f);
        instructionsHeader.scale = 0.7f;
        [self addChild:instructionsHeader z:1] ;

        instructions  = [CCLabelBMFont labelWithString:@"Press any key" fntFile:@"bitmapFontTest.fnt"];
        instructions.position = CGPointMake(160.0f, 200.0f);
        instructions.anchorPoint = CGPointMake(0.5f, 0.5f);
        instructions.scale = 0.7f;
        [self addChild:instructions z:1] ;

        [[CCDirector sharedDirector] resume];


     //   [self runAction:[CCSequence actionOne:[CCDelayTime actionWithDuration:0.1f] two:[CCCallFunc actionWithTarget:self selector:@selector(removeStuffFromPreviousScene)]]];
        [self runAction:[CCSequence actionOne:[CCDelayTime actionWithDuration:1.0f] two:[CCCallFunc actionWithTarget:self selector:@selector(report_memory)]]];
        [self runAction:[CCSequence actionOne:[CCDelayTime actionWithDuration:2.0f] two:[CCCallFunc actionWithTarget:self selector:@selector(report_memory)]]];
        [self runAction:[CCSequence actionOne:[CCDelayTime actionWithDuration:5.0f] two:[CCCallFunc actionWithTarget:self selector:@selector(report_memory)]]];
        [self callBackReplace];


    }
    return self;
}

-(void) removeStuffFromPreviousScene
{
    //Use these
    [[CCSpriteFrameCache sharedSpriteFrameCache] removeSpriteFrames];
    [[CCSpriteFrameCache sharedSpriteFrameCache] removeUnusedSpriteFrames];


    //Use these
    [[CCTextureCache sharedTextureCache] removeUnusedTextures];
    [[CCTextureCache sharedTextureCache] removeAllTextures];
    [[CCDirector sharedDirector] purgeCachedData];
    [CCAnimationCache purgeSharedAnimationCache];

}

-(void) report_memory {
    CCLOG(@"");
    CCLOG(@"");
    CCLOG(@"InstructionScene info:");
    CCLOG(@"");

    [[CCTextureCache sharedTextureCache] dumpCachedTextureInfo];
    struct task_basic_info info;

    mach_msg_type_number_t size = sizeof(info);
    kern_return_t kerr = task_info(mach_task_self(),
                                   TASK_BASIC_INFO,
                                   (task_info_t)&info,
                                   &size);
    if( kerr == KERN_SUCCESS ) {
        NSLog(@"Memory in use (in bytes): %u", info.resident_size);
    } else {
        NSLog(@"Error with task_info(): %s", mach_error_string(kerr));
    }
}

-(void) nextSuggestionPressed
{
    [self stopAllActions];
    [self callBackReplace];
}

-(void) callBackReplace 
{
    [self runAction:[CCSequence actions:
                     [CCDelayTime actionWithDuration:startTime] ,
                     [CCCallFunc actionWithTarget:self selector:@selector(replaceWithShooterScene)],
                      nil]
     ];
}


-(void) replaceWithShooterScene
{
      [[CCDirector sharedDirector] replaceScene:[ShooterScene sceneWithLevelName:levelName]];
}


@end
//
//说明现场
//
//由mm24于2013年9月9日创建。
//版权所有2013 mm24。保留所有权利。
//
#进口
#导入“cocos2d.h”
#导入“CommonEnumerations.h”
@界面说明场景:CCLayer{
LevelName LevelName;
浮动起始时间;
CCLabelBMFont*levelNameTitle;
CCLABELMFONT*级别字幕;
CCLabelBMFont*指令标题;
CCLABELMFONT*说明;
}
+(id)场景级别名称:(级别名称)名称;
@结束
//
//指令现场
//
//由mm24于2013年9月9日创建。
//版权所有2013 mm24。保留所有权利。
//
#导入“InstructionScene.h”
#导入“shootersene.h”
#导入“AppDelegate.h”
#输入“马赫/马赫小时”
@现场实施说明
+(id)场景级别名称:(级别名称)名称
{
CCScene*scene=[CCScene节点];
指令场景*层=[[self alloc]initWithLevelName:name];
[场景添加子对象:层];
返回场景;
}
-(id)initWithLevelName:(LevelName)名称
{
if((self=[super init]))
{
//从上一个场景中删除内容
[[CCSpriteFrameCache SharedPriteFrameCache]移除PriteFrames];
[[CCSpriteFrameCache sharedSpriteFrameCache]移除未使用的SpriteFrames];
//使用这些
[[CCTextureCache sharedTextureCache]移除未使用的纹理];
[[CCTextureCache sharedTextureCache]removeAllTextures];
[[CCDirector sharedDirector]purgeCachedData];
[CCAnimationCache purgeSharedAnimationCache];
//尝试并使用它。不是强制性的
[自行清除所有儿童:是];
CCLOG(@“具有级别名称的init”);
levelName=名称;
起始时间=10.0f;
levelNameTitle=[CCLabelBMFont labelWithString:@“Title”fntFile:@“bitmapFontTest.fnt”];
levelNameTitle.position=CGPointMake(160.0f,420.0f);
levelNameTitle.anchorPoint=CGPointMake(0.5f,0.5f);
levelNameTitle.scale=1.3f;
[self-addChild:levelNameTitle z:1];
levelSubtitle=[CCLabelBMFont labelWithString:@“Subtitle”fntFile:@“bitmapFontTest.fnt”];
levelSubtitle.position=CGPointMake(160.0f,400.0f);
levelSubtitle.anchorPoint=CGPointMake(0.5f,0.5f);
levelSubtitle.scale=0.7f;
[self-addChild:levelSubtitle z:1];
指令标题=[CCLabelBMFont labelWithString:@“指令”fntFile:@“bitmapFontTest.fnt”];
指令Header.position=CGPointMake(160.0f,240.0f);
指令Header.anchorPoint=CGPointMake(0.5f,0.5f);
指令收割台。刻度=0.7f;
[自添加子项:指令标题z:1];
说明=[CCLabelBMFont labelWithString:@“按任意键”fntFile:@“bitmapFontTest.fnt”];
说明.位置=CGPointMake(160.0f,200.0f);
instructions.AncorPoint=CGPointMake(0.5f,0.5f);
说明:比例=0.7f;
[自添加子项:说明z:1];
[[CCDirector sharedDirector]简历];
//[自运行操作:[CCSequence actionOne:[CCDelayTime actionWithDuration:0.1f]两个:[CCCallFunc actionWithTarget:自选择器:@selector(removeStuffFromPreviousScene)];
[自运行操作:[CCSequence actionOne:[CCDelayTime actionWithDuration:1.0f]两个:[CCCallFunc actionWithTarget:自选择器:@selector(报告内存)];
[self runAction:[CCSequence actionOne:[CCDelayTime actionWithDuration:2.0f]two:[CCCallFunc actionWithTarget:self selector:@selector(report_memory)];
[自运行操作:[CCSequence actionOne:[CCDelayTime actionWithDuration:5.0f]两个:[CCCallFunc actionWithTarget:自选择器:@selector(报告内存)];
[自回调替换];
}
回归自我;
}
-(无效)从以前的场景中移除
{
//使用这些
[[CCSpriteFrameCache SharedPriteFrameCache]移除PriteFrames];
[[CCSpriteFrameCache sharedSpriteFrameCache]移除未使用的SpriteFrames];
//使用这些
[[CCTextureCache sharedTextureCache]移除未使用的纹理];
[[CCTextureCache sharedTextureCache]removeAllTextures];
[[CCDirector sharedDirector]purgeCachedData];
[CCAnimationCache purgeSharedAnimationCache];
}
-(无效)报告内存{
CCLOG(@“);
CCLOG(@“);
CCLOG(@“指令场景信息:”);
CCLOG(@“);
[[CCTextureCache sharedTextureCache]dumpCachedTextureInfo];