I’m writing a full featured iOS Native GameKit Plugin and I get the following memory leak when profiling my code using Instruments:
Leaked Object Size Responsible Library Responsible Frame
GKMatchRequest 16 Bytes GameCenterFoundation -[GKMatchRequest copyWithZone:]
GKMatchRequestInternal 48 Bytes GameCenterFoundation -[GKInternalRepresentation copyWithZone:]
The Stack Trace indicates that the responsible line of code for the leak is:
GKMatchmakerViewController *matchmakerViewController = [[GKMatchmakerViewController alloc] initWithMatchRequest:request];
Excerpt code, show the Game Center Real-Time Match UI:
GKMatchRequest \*request = [[GKMatchRequest alloc] init]; NSLog(@"[GameKitLibrary] gameCenterRealTimeMatchUI request.retainCount = %i", request.retainCount); // retainCount = 1 request.minPlayers = minPlayers; request.maxPlayers = maxPlayers; request.defaultNumberOfPlayers = defaultNumPlayers; if ( playerGroupEnabled == YES ) { request.playerGroup = playerGroup; } if ( playerAttributesEnabled == YES ) { request.playerAttributes = playerAttributes; } NSLog(@"[GameKitLibrary] before matchmakerViewController request.retainCount = %i", request.retainCount); // retainCount = 1 GKMatchmakerViewController \*matchmakerViewController = [[GKMatchmakerViewController alloc] initWithMatchRequest:request]; // where leak occurs NSLog(@"[GameKitLibrary] after matchmakerViewController request.retainCount = %i", request.retainCount); // retainCount = 1 [request release]; NSLog(@"[GameKitLibrary] after release request.retainCount = %i", request.retainCount); // retainCount = 1 if (matchmakerViewController != nil) { matchmakerViewController.matchmakerDelegate = gameCenterManagerDelegate; [[[UIApplication sharedApplication] keyWindow].rootViewController presentViewController:matchmakerViewController animated:YES completion:nil]; }
I’ve also tried autorelease, GKMatchRequest *request = [[[GKMatchRequest alloc] init] autorelease]; but I get the same memory leak in Instruments.
If I call [request release]; twice it crashes.
The leak doesn’t happen right away, it happens about 3 minutes after a Real-Time match is made with players still playing the match.
This plugin is my first time coding Objective-C/C++ so I can’t tell if I’m doing something wrong or this is a leak within the GameKit framework.
Any help is most appreciated,
Warren