Question

Recently I have come across a problem in an app that I am developing. The app is crashing with EXC_BAD_ACCESS. This doesn't make sense because auto-reference-counting is turned on.

In the app, I have a button that is linked to an IBAction that displays a GKPeerPickerController.

-(IBAction)showPicker:(id)sender
{
picker = [[GKPeerPickerController alloc ] init];
picker.delegate = self;
picker.connectionTypesMask = GKPeerPickerConnectionTypeNearby;

[picker show];
}

This doesn't make sense because if I try to manage the memory with calls such as release, it will give me an error saying that ARC disables that call. So as far as I know there is nothing I can do about it. When it crashes, the EXC_BAD_ACCESS is on the line that allocates and initializes the GKPeerPickerController.

Was it helpful?

Solution

Does this only happen the second time you try to launch the GKPeerPicker?

EXC_BAD_ACCESS is thrown when your app tries to access a memory location which it doesn't 'own'. This can happen in numerous different ways, even with ARC, which makes it a hard crash to diagnose.

Check out this question for e.g. EXC_BAD_ACCESS (SIGSEGV) crash - using NSZombies could be a way to track what's going on.

However, a little more understanding of what's going on might help you to understand this crash and fix it.

The first question is - how is it possible to get EXC_BAD_ACCESS when we're merely assigning a newly allocated object? Well - the 'magic of ARC' is coming in to play here... That simple assignment statement is assigning a new object to an instance variable. The compiler will see that and say - Ah... that ivar might already have an object assigned to it, in which case I'd better release it... So it will add some code for you to check for nil and release the ivar, before it assigns the new value.

So - I find it unlikely that the alloc/init is causing your problem, and more likely that it's whatever is currently stored in your picker ivar... What happens if you make picker a local variable instead of an ivar?

-(IBAction)showPicker:(id)sender
{
     GKPeerPickerController *localPicker = [[GKPeerPickerController alloc ] init];
     localPicker.delegate = self;
     localPicker.connectionTypesMask = GKPeerPickerConnectionTypeNearby;

     [localPicker show];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top