0

I am working with cocos2d. At the first Default.png load as first splash, then splash1.png load as second splash. I see in Instruments, that memory don't free when I replace scene. How can I unload images from memory? Thanks!

#import "cocos2d.h"
#import "MainMenu.h"

@interface Splash : CCLayer {

    NSMutableArray *m_pSplashes;
    int m_nCurrentSplash;
    CGSize m_szWinSize;
    CCSequence *m_pSequence, *m_pSequenceDefault;
    CCCallFunc *m_pCall;
    CCSprite *m_pSplashDefault, *splash;
    id m_pFadein, m_pDelay, m_pFadeout;
}

@property(nonatomic, retain) CCSequence *m_pSequence;

+(id) scene;
-(void) showNext: (id) sender;

@end

Implementation file

#import "Splash.h"

@implementation Splash

@synthesize m_pSequence;

+(id) scene {

    CCScene *scene = [CCScene node];
    Splash *layer = [Splash node];
    [scene addChild: layer];
    return scene;
}   

-(id) init {    
    if( (self=[super init]) ) {     
        m_szWinSize = [[CCDirector sharedDirector] winSize];        
        m_pFadein = [CCFadeIn actionWithDuration:2];
        m_pDelay = [CCDelayTime actionWithDuration:2];
        m_pFadeout = [CCFadeOut actionWithDuration:2];
        m_pCall = [CCCallFunc actionWithTarget:self selector:@selector(showNext:)];

        m_nCurrentSplash = 0;
        m_pSplashes = [[NSMutableArray alloc] init];

        m_pSequenceDefault = [CCSequence actions:m_pFadeout, m_pCall, nil];

        [m_pSplashes addObject:@"splash1.png"];

        m_pSplashDefault = [[[CCSprite alloc] initWithFile:@"Default.png"] autorelease];
        [m_pSplashDefault setRotation:-90];
        [m_pSplashDefault setPosition:ccp(m_szWinSize.width/2, m_szWinSize.height/2)];
        [self addChild:m_pSplashDefault];
        [m_pSplashDefault runAction:m_pSequenceDefault];
        [m_pFadein retain];
        [m_pDelay retain];
        [m_pFadeout retain];
        [m_pCall retain];
    }
    return self;
}


-(void) showNext: (id) sender { 
    if ( m_nCurrentSplash >= [m_pSplashes count] )
    {   
        CCScene *scene = [CCScene node];
        id child = [MainMenu node];
        [scene addChild:child]; 
        [[CCDirector sharedDirector] replaceScene: [CCFadeTransition transitionWithDuration:1 scene:scene]];
        [m_pCall release];
    }
    else
    {
        splash = [[[CCSprite alloc] initWithFile:[m_pSplashes objectAtIndex:m_nCurrentSplash]] autorelease];
        [splash setPosition:ccp(m_szWinSize.width/2, m_szWinSize.height/2)];
        splash.tag = 1;
        [self addChild:splash];
        m_nCurrentSplash ++;
        m_pSequence = [CCSequence actions:m_pFadein, m_pDelay, m_pFadeout, m_pCall, nil];
        [splash runAction:m_pSequence];
    }
}


-(void) dealloc {
    NSLog ( @"dealloc" );
    [m_pSplashes release];
    [m_pFadein release];
    [m_pDelay release];
    [m_pFadeout release];
    [self removeAllChildrenWithCleanup:YES];
    [super dealloc];
}   

@end
Sebastian
  • 7,670
  • 5
  • 38
  • 50
Sveta
  • 1,270
  • 3
  • 16
  • 33

1 Answers1

0

Either you manually handle by using

[[CCTextureCache sharedTextureCache]removeTexture:(CCTexture2D *)tex]

or:

[[CCTextureCache sharedTextureCache] removeUnusedTextures];

if you are sure the texture is no longer being use.

danielbeard
  • 9,120
  • 3
  • 44
  • 58
Lim Gim Hong
  • 306
  • 1
  • 3