I am trying to track down a memory leak in my cocos2D game. I have run the game using instruments to find what is causing the leaks and the bulk of the problem seems to come from this method.
-(void)setColour:(int)c {
switch (c) {
case RED:
images[SMALL_BUBBLE_IMAGE] = [[CCTexture2D alloc] initWithImage:[UIImage imageNamed:(NSString *)@"red_bubble_small.png"]];
images[SMALL_SELECTED_BUBBLE_IMAGE] = [[CCTexture2D alloc] initWithImage:[UIImage imageNamed:(NSString *)@"red_bubble_select_small.png"]];
images[LARGE_BUBBLE_IMAGE] = [[CCTexture2D alloc] initWithImage:[UIImage imageNamed:(NSString *)@"red_bubble.png"]];
images[LARGE_SELECTED_BUBBLE_IMAGE] = [[CCTexture2D alloc] initWithImage:[UIImage imageNamed:(NSString *)@"red_bubble_select.png"]];
break;
case BLUE:
images[SMALL_BUBBLE_IMAGE] = [[CCTexture2D alloc] initWithImage:[UIImage imageNamed:(NSString *)@"blue_bubble_small.png"]];
images[SMALL_SELECTED_BUBBLE_IMAGE] = [[CCTexture2D alloc] initWithImage:[UIImage imageNamed:(NSString *)@"blue_bubble_select_small.png"]];
images[LARGE_BUBBLE_IMAGE] = [[CCTexture2D alloc] initWithImage:[UIImage imageNamed:(NSString *)@"blue_bubble.png"]];
images[LARGE_SELECTED_BUBBLE_IMAGE] = [[CCTexture2D alloc] initWithImage:[UIImage imageNamed:(NSString *)@"blue_bubble_select.png"]];
break;
case GREEN:
images[SMALL_BUBBLE_IMAGE] = [[CCTexture2D alloc] initWithImage:[UIImage imageNamed:(NSString *)@"green_bubble_small.png"]];
images[SMALL_SELECTED_BUBBLE_IMAGE] = [[CCTexture2D alloc] initWithImage:[UIImage imageNamed:(NSString *)@"green_bubble_select_small.png"]];
images[LARGE_BUBBLE_IMAGE] = [[CCTexture2D alloc] initWithImage:[UIImage imageNamed:(NSString *)@"green_bubble.png"]];
images[LARGE_SELECTED_BUBBLE_IMAGE] = [[CCTexture2D alloc] initWithImage:[UIImage imageNamed:(NSString *)@"green_bubble_select.png"]];
break;
case PURPLE:
images[SMALL_BUBBLE_IMAGE] = [[CCTexture2D alloc] initWithImage:[UIImage imageNamed:(NSString *)@"purple_bubble_small.png"]];
images[SMALL_SELECTED_BUBBLE_IMAGE] = [[CCTexture2D alloc] initWithImage:[UIImage imageNamed:(NSString *)@"purple_bubble_select_small.png"]];
images[LARGE_BUBBLE_IMAGE] = [[CCTexture2D alloc] initWithImage:[UIImage imageNamed:(NSString *)@"purple_bubble.png"]];
images[LARGE_SELECTED_BUBBLE_IMAGE] = [[CCTexture2D alloc] initWithImage:[UIImage imageNamed:(NSString *)@"purple_bubble_select.png"]];
break;
case YELLOW:
images[SMALL_BUBBLE_IMAGE] = [[CCTexture2D alloc] initWithImage:[UIImage imageNamed:(NSString *)@"yellow_bubble_small.png"]];
images[SMALL_SELECTED_BUBBLE_IMAGE] = [[CCTexture2D alloc] initWithImage:[UIImage imageNamed:(NSString *)@"yellow_bubble_select_small.png"]];
images[LARGE_BUBBLE_IMAGE] = [[CCTexture2D alloc] initWithImage:[UIImage imageNamed:(NSString *)@"yellow_bubble.png"]];
images[LARGE_SELECTED_BUBBLE_IMAGE] = [[CCTexture2D alloc] initWithImage:[UIImage imageNamed:(NSString *)@"yellow_bubble_select.png"]];
break;
}
[images[SMALL_BUBBLE_IMAGE] retain];
[images[SMALL_SELECTED_BUBBLE_IMAGE] retain];
[images[LARGE_BUBBLE_IMAGE] retain];
[images[LARGE_SELECTED_BUBBLE_IMAGE] retain];
[small_bubble setTexture:images[SMALL_BUBBLE_IMAGE]];
[large_bubble setTexture:images[LARGE_BUBBLE_IMAGE]];
colour = c;
}
All this function does is change the images used by an object, each object has 4 images associated with it so I store them in an array. I had thought that any pointers I use that are not retained were released automatically, but then i figured maby this wasnt the case. The project would crash on me if I called release on the object without having first called retain on the object so thats why the lines retaining the images are there.
I call these lines in the dealloc function;
[images[SMALL_BUBBLE_IMAGE] release];
[images[SMALL_SELECTED_BUBBLE_IMAGE] release];
[images[LARGE_BUBBLE_IMAGE] release];
[images[LARGE_SELECTED_BUBBLE_IMAGE] release];
Instruments tells me the lines within the case statement are where the memory leaks are. This function is only called once in an objects lifecycle. Any ideas why this is causing memory leaks?