1

I'm working on a nice app as per usual and then this happens! You spin the wheel of fortune in my app, and when the spinning animation is finished, the app is supposed to randomize a number, and display the card matched as the number index.

The cards are stored in a NSMutableArray, working perfectly when called the first time. Eg. when the app randomizes number "1" card number 1 is called and displayed without problem! But once the randomizer hits "1" again, the app crashes on the line specified below.

Here's the code, PLEASE help me :)

NSNumber *cardNumber;
    cardNumber = [NSNumber numberWithUnsignedInt:arc4random()%[appDelegate.cardArray count]];

    NSLog(@"%@", cardNumber);

    SpinCard *selectedCard = [appDelegate.cardArray objectAtIndex:[cardNumber doubleValue]];

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5];
    [snurrKnapp setAlpha:100];
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:cardView cache:YES];
    [UIView commitAnimations];
    [snurrKnapp setEnabled:YES];
    if(![cardTitle.text isEqualToString:selectedCard.cardTitle]) { //Crashes here
        [cardTitle setText:selectedCard.cardTitle]; //If I remove the if-case it crashes here
        [cardContent setText:selectedCard.cardContent];
    }
    [selectedCard release];

Here's the error I get:

2011-04-21 23:10:54.296 Snurra Flaskan[947:707] 0
2011-04-21 23:10:58.794 Snurra Flaskan[947:707] 2
2011-04-21 23:11:02.691 Snurra Flaskan[947:707] 1
2011-04-21 23:11:08.977 Snurra Flaskan[947:707] 2
warning: Unable to read symbols for /Developer/Platforms/iPhoneOS.platform/DeviceSupport/4.3.1 (8G4)/Symbols/Developer/usr/lib/libXcodeDebuggerSupport.dylib (file not found).
(gdb) 
Hjalmar
  • 1,921
  • 2
  • 16
  • 19
  • 1
    Seems like the cardArray is getting released, that might be the cause, could you post a snippet of the code where you alloc the array? – Fran Sevillano Apr 21 '11 at 21:23
  • Where do you initialize the cardTitle variable? My guess it's that the cardTitle variable somehow got released when it got out of some scope, causing the errors. You could try setting NSZombieEnabled=YES in your scheme (argument tab) to see if there are pointers to objects that have been released, see: http://stackoverflow.com/questions/2190227/how-do-i-set-nszombieenabled-in-xcode-4/4917557#4917557 – Wolfgang Schreurs Apr 21 '11 at 21:25
  • One trick for tracking down overrelease errors like this is to give your objects extra retains you think are not needed and see if the crash goes away. Another is to [enable zombies](http://stackoverflow.com/questions/2190227/how-do-i-set-nszombieenabled-in-xcode-4). – William Jockusch Apr 21 '11 at 21:31
  • Found it! Caused by the release of the selectedCard.. wierd? – Hjalmar Apr 21 '11 at 21:44

1 Answers1

5

I'm very new in Obj-C, but here is what I think is happening.

You should not call [selectedCard release];

Because objectAtIndex does not allocate a new SpinCard for selectedCard. You should not release it. That explains the behaviour for the second use of that SpinCard: you can't use it, it has already been released.

moala
  • 5,094
  • 9
  • 45
  • 66
  • Yep, that should be correct. Only methods with *alloc*, *copy* or *retain* in the name should be released manually using either *release* or *autorelease*. – Wolfgang Schreurs Apr 21 '11 at 21:48