@clain: I'm getting the same leaks with cocos 0.7.3 for iPhone 3.0. (I don't want to upgrade to 0.8 because I wrote some custom particle code that I don't want to recode to work with 0.8's new particle system).
When I run my code through "ObjectAlloc" and "Leaks" in Instruments my game leaks each time I call replaceScene. Dealloc is called when I call replaceScene, however the memory in ObjectAlloc never decreases like it does when I run sample cocos2d demos like SceneTest and SpritesTest through the same Instruments.
At first I thought the memory was leaking because my object's weren't being deallocated. I read that dealloc is only called when [self retainCount] is equal to zero. A call to [self retainCount] immediately before calling replaceScene shows that the scene has a retainCount of 5, however calls to dealloc are called each time I call replaceScene. I don't get it.
The Leaked objects being reported are listed here:
main.m
#import <UIKit/UIKit.h>
int main(int argc, char *argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, @"SimpleGameAppDelegate"); //this line
[pool release];
return retVal;
}
CocosNode.m
+(id) node
{
return [[[self alloc] init] autorelease]; //this line
}
FirstScene.m in the init method of a scene
Sprite * bg = [Sprite spriteWithFile:@"splash_screen.png"]; //this line
Sprite.m - two leaks from this file
#pragma mark Sprite - image file
+ (id) spriteWithFile:(NSString*) filename
{
return [[[self alloc] initWithFile:filename] autorelease]; //this line
}
- (id) initWithFile:(NSString*) filename
{
self = [super init];
if( self ) {
// texture is retained
self.texture = [[TextureMgr sharedTextureMgr] addImage: filename]; //this line
CGSize s = self.texture.contentSize;
transformAnchor = ccp(s.width/2, s.height/2);
_autoCenterFrames = NO;
// lazy alloc
animations = nil;
}
return self;
}
TextureMgr.m
-(Texture2D*) addImage: (NSString*) path
{
NSAssert(path != nil, @"TextureMgr: fileimage MUST not be nill");
Texture2D * tex;
if( (tex=[textures objectForKey: path] ) ) {
return tex;
}
// Split up directory and filename
NSString *fullpath = [FileUtils fullPathFromRelativePath: path ];
// all images are handled by UIImage except PVR extension that is handled by our own handler
if ( [[path lowercaseString] hasSuffix:@".pvr"] )
return [self addPVRTCImage:fullpath];
tex = [ [Texture2D alloc] initWithImage: [UIImage imageWithContentsOfFile: fullpath ] ]; //this line
[textures setObject: tex forKey:path];
return [tex autorelease];
}
After calling runScene in my AppDelegate, adding a background image, a timer, and a sound to the scene, I get my first spike in leaked memory. At this point I'm not sure what code to edit.
What's causing my game to leak memory?