Search code examples
iphoneobjective-cmemory-leaksp2pdealloc

Trouble Finding a Memory Leak


Hey everyone, i am having trouble finding a memory leak. all off my retain counts = 0 when i dealloc them but still I am flagging up a leak from the following bit of code:

- (GKSession *) peerPickerController:(GKPeerPickerController *)picker sessionForConnectionType:(GKPeerPickerConnectionType)type {
inSession = [[GKSession alloc] initWithSessionID:gameSessionID displayName:nil sessionMode:GKSessionModePeer];
printf( "insession alloc on Start: %i\n", [inSession retainCount] );
return inSession;

}

On cancelling the peer picker, so if you don't find anybody to connect to, i run this code to get rid of everything to do with the peer picker.

- (void)peerPickerControllerDidCancel:(GKPeerPickerController *)picker { 
picker.delegate = nil;
mpicker.delegate = nil;
inSession.delegate = nil;
gameSession.delegate = nil;

if(inSession != nil) {

    [self invalidateSession:inSession];
    [inSession release];
    inSession = nil;

}

[picker release];

picker = nil;
mpicker = nil;



[inSession release];


if(self.gameSession != nil) {
    [self invalidateSession:self.gameSession];
    [self.gameSession release];
    self.gameSession = nil;
}

[self.gameSession release];
self.gameLabel.hidden = NO;
self.gameState = pongStateStartGame;


[gameSession release];
[inSession release];

[inSession dealloc];
[gameSession dealloc];



[mpicker dealloc];

}

Somewhere, the code is leaking and i can't figure out for the life of me where. Any help with this would be amazingly appreciated.


Solution

  • Use Instruments to find your leaks.

    The problem is you haven't yet understood Cocoa's memory management.

    [inSession dealloc];
    [gameSession dealloc];
    [mpicker dealloc];
    

    You should never have to call -dealloc yourself. NSObject calls this when the reference count reaches 0.

    Try to learn the correct way to manage the memory.