views:

294

answers:

5

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.

+1  A: 

Consider running Xcode 3.2's Build and Analyze (Under the Build menu). This can be very helpful in finding reference counting issues.

If that doesn't help, run the Leaks tool in Instruments (Run->Run With Performance Tool->Leaks).

nall
+3  A: 

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.

Georg
A: 

Trying to release it a second time after setting your variable pointer to nil won't help - it won't do anything. Also don't call dealloc. While technically not against the law your

- (GKSession *) peerPickerController:(GKPeerPickerController *)picker sessionForConnectionType:(GKPeerPickerConnectionType)type

method indicates a scrappyness of style that could lead to this kind of leak. It both creates a new GKSession and sets it to an instance variable and returns a reference to it. It doesn't use either of the arguments. Either create the instance variable OR autorelease it and return it. Where is the code calling this method? Is the caller retaining the returned value?

have been using instruments and it is within instruments that it appears. Have used build and analyze and nothing appears, there are no hints to anything.Also, all the retain counts have hit 0, so i put the dealloc in to make this happen. still there is an error when instruments checks for another leak.
Adam Libonatti-Roche
you should still tidy it up. Setup your inSession property to retain then - (GKSession *)sessionForConnectionType:(GKPeerPickerConnectionType)type { return [[GKSession alloc] initWithSessionID:gameSessionID displayName:nil] autorelease];}// to assignself.inSession = [whichObject? sessionForConnectionType:type];-- --// to releaseself.inSession = nil;Do you have zombies turned on?
i had the original code from apple which returned a session as an autorelease, but that was never used either and was not released when the peer picker cancelled. that started all the problems.
Adam Libonatti-Roche
is NSZombieEnabled ?
Doesn't Instruments show you which objects leak?
Georg
how would NSZombie be enabled? instruments show that it is the peer picker code that leaks.
Adam Libonatti-Roche
1) go back to the apple version of the code. It is definitely less buggy - i dont get what you mean by "but that was never used either…"2) Using NSZombieEnabled makes objects appear to leak in instruments - make sure to turn it off before leak hunting.3) The apple code requires that you have declared your 'inSession' property correctly. Are you happy that you have done that correctly?4) If you have done all that and Leaks says 'inSession' is leaking t is possible that the session is being cached and you shouldn't worry about it.
A: 

In that second bit of code, you [inSession release] twice, and THEN dealloc it.

I'm sorry, but this is just about 200 different types of wrong.

Never call dealloc The system does that for you.

Don't call release on the same object twice in the same method. You never know if the object still exists after the first call. I'm surprised that you are not getting over-release exceptions, muchless a leak.

For a quick explanation of the use of retain and release, see this question.

For a more detailed explanation (and very worthwhile) read the Cocoa documentaton on the subject.

mmc
i understand the count principle behind retain and release, which is why this is confusing me so much right now and am in the points of desperation.why if the count is 0, for everything that i use, is instruments finding a leak with inSession. or more to the point, the original apple code which never allowed the user to manually release the variable.
Adam Libonatti-Roche
A: 

Just a comment about using [x retainCount] to "help" in identifying memory issues. As I learned and tried this out on several occasions - this might NOT reflect the correct situation. So do not rely on this value - don't use this - it will most probably cause confusion.

Secondly, it happens to me all the time that I use Apple code (which of course works in the example) and then "out of a blue" it causes problems in my code (showing up in LEAKS Instrument)- without having "abused it" of course. Sometimes I spend hours over hours and find the weirdest reasons for the strange behaviour. For example, just adding in UINavigationControllerDelegate as a protocol to respond to, the code might suddenly leak - without having changed any other line of code!