Search code examples
cocos2d-xreal-timegame-centerinvitegkmatchmaker

EXE_BAD_BADACCESS trying to access GKMatch?


The Problem In Brief

Trying to play with a friend in real-time using gamecenter causes a crash during gameplay

Project Details

  • I am using gamecenter with cocos2dx
  • I am using the SANDBOX mode: Both accounts being tested are in sandbox mode
  • 2 Ipads are used for testing (IOS 8.3)
  • I am using GKMatchMaker to match players
  • Real-Time matches with 'Random' aka 'Play Now' works with no issues
  • Real-Time matches with 'Invite a Friend' matches successfully but crashes after accessing GKMatch object
  • Crash happens when I send data using reliable or unreliable method (Only on Invite)

My Investigation

  • Accessing GKMatch during the match creates EXC_BAD_ACCESS
  • The assigned value to match variable is not null or invalid when accessed in the following function:

-(void)matchmakerViewController: (GKMatchmakerViewController *) viewController didFindMatch:(GKMatch *)match

  • I may be doing the whole process incorrectly

My Process For Handling Invites

  1. Match maker from device A sends notification about invite to device B
  2. Device B opens up and redirects to match making room
  3. Device B calls following function:

{

-(void)player:(GKPlayer *)player didAcceptInvite:(GKInvite *)invite {

 self->inviteStarted((int)invite.playerGroup);

 NSLog(@"didAcceptInvite");

 //Called when another player accepts a match invite from the local player.
 NSLog(@"didAcceptInvite was called: Player: %@  accepted our invitation", player);

 GKMatchmakerViewController *mmvc = [[[GKMatchmakerViewController alloc] initWithInvite:invite] autorelease];

 mmvc.matchmakerDelegate = self;

 [[AppController getViewController] presentViewController:mmvc animated:YES completion:nil];

  }

}

  1. Both Devices call following function:

{

-(void)matchmakerViewController:(GKMatchmakerViewController *)viewController didFindMatch:(GKMatch *)match
{

if (match != nil)
{

    [[AppController getViewController] dismissViewControllerAnimated:YES completion:nil];

    NSLog(@"%@", match);

    self->myMatch = match;

    //NSLog(@"Match found count!! %lu", (unsigned long)[self->myMatch retainCount]);

    match.delegate = self;

    if (!self->matchStarted && myMatch.expectedPlayerCount == 0)
    {
        self->matchStarted = YES;

        opponent=[[myMatch players] objectAtIndex:0];

        self->matchBegan();

        [self loadOppPhoto];
    }
}
}

}

  1. Device A send data to Device B (CRASH HAPPENS HERE)

{ -(void) sendDataToOppUnreliable:(NSString*)str

{


NSError *error=nil;

NSData *packet = [str dataUsingEncoding:NSUTF8StringEncoding];
//NSLog(@"this: %@", self);

//NSLog(@"Match found count!! %lu", (unsigned long)[self->myMatch retainCount]);

//NSLog(@"match: %@", self->myMatch);//EXC_BAD_ACCESS 

[myMatch sendDataToAllPlayers: packet withDataMode: 

GKMatchSendDataUnreliable error:&error];//EXC_BAD_ACCESS

if (error!=nil)
{
    NSLog(@"Error in sending data: %li", (long)[error code]);
}
}

}

Thanks for the help :)


Solution

    • GKMatch property was set to (assign) and was needed to change to (retain)
    • GKMatch object needed to be initialized to nil at start so it could be used