Firstly, sorry if this is a lame question. After years of C# and Java coding I've been a little spoilt when it comes to handling memory, and I'm still learning Obj-C.
I have a series of scenes, that transition to each other and then loop. Each scene has a large PNG as a background. After each scene, the app's memory usage goes up by 5-10mb, until it finally crashes.
I've been looking to fix this, but have read in several places on this forum that cocos2d automatically deallocates children, so not sure why I am getting this issue.
Here's my code (mostly taken from sample code), any ideas?
(@interface Landscape2Scene : CSScene)
@implementation Landscape2Scene
-(id) init
{
if( (self=[super init])) {
CCSprite *background = [CCSprite spriteWithFile:@"RestaurantExternal1Background1024x768.png"];
background.position = CGPointMake(0, 0);
background.anchorPoint = CGPointZero;
[self addChild:background];
}
return self;
}
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
CCScene *s = [RestaurantTelevisionScene node];
[[CCDirector sharedDirector] replaceScene:[CCPageTurnTransition transitionWithDuration:0.5f scene:s]];
}
- (void) dealloc
{
[super dealloc];
}
@end
------
(@interface CSScene : CCLayer)
@implementation CSScene
+(id) scene
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];
// 'layer' is an autorelease object.
CSScene *layer = [self node];
// add layer as a child to scene
[scene addChild: layer];
// return the scene
return scene;
}
-(id) init
{
if( (self=[super init])) {
self.isTouchEnabled = YES;
self.isAccelerometerEnabled = YES;
CGSize wins = [[CCDirector sharedDirector] winSize];
cpInitChipmunk();
cpBody *staticBody = cpBodyNew(INFINITY, INFINITY);
space = cpSpaceNew();
cpSpaceResizeStaticHash(space, 400.0f, 40);
cpSpaceResizeActiveHash(space, 100, 600);
space->gravity = ccp(0, 0);
space->elasticIterations = space->iterations;
cpShape *shape;
// bottom
shape = cpSegmentShapeNew(staticBody, ccp(0,0), ccp(wins.width,0), 0.0f);
shape->e = 1.0f; shape->u = 1.0f;
cpSpaceAddStaticShape(space, shape);
// top
shape = cpSegmentShapeNew(staticBody, ccp(0,wins.height), ccp(wins.width,wins.height), 0.0f);
shape->e = 1.0f; shape->u = 1.0f;
cpSpaceAddStaticShape(space, shape);
// left
shape = cpSegmentShapeNew(staticBody, ccp(0,0), ccp(0,wins.height), 0.0f);
shape->e = 1.0f; shape->u = 1.0f;
cpSpaceAddStaticShape(space, shape);
// right
shape = cpSegmentShapeNew(staticBody, ccp(wins.width,0), ccp(wins.width,wins.height), 0.0f);
shape->e = 1.0f; shape->u = 1.0f;
cpSpaceAddStaticShape(space, shape);
}
return self;
}
- (void) dealloc
{
[[CCTextureCache sharedTextureCache] removeUnusedTextures];
[super dealloc];
}
@end