views:

338

answers:

4

I have an array of 5 items that is also used as the content for a tableview. In the nib is a button that changes the content of the array to 5 different items. When I click that button, however, the app crashes with an EXC_BAD_ACCESS. I set a breakpoint on objc_exception _throw and had my suspicions confirmed. The bad method is

- (id)tableView:(NSTableView *)wordsTableView
objectValueForTableColumn:(NSTableColumn *)column
      row:(int)rowIndex
{
    return [[currentCard words] objectAtIndex:rowIndex];
}

currentCard is an instance of the GameCard class, and its array, words, is the array in question. On first launch, it works fine, but if I try to change it, crash.

----------EDIT----------

In AppController's awakeFromNib: I have this

currentCard = [[GameCard alloc] init];

And in the button's IBAction, I have this:

[currentCard release];
currentCard = [[GameCard alloc] init];

With zombies enabled, when I click the button, I get this from GDB:

2009-06-22 18:55:03.368 25WordsMax[19761:813] *** -[CFArray objectAtIndex:]: message sent to deallocated instance 0x14ba00

referring to the data source method. I've been trying to track down the memory bug for hours, but am missing it.

I got so frustrated I commented out every retain & release (no autoreleases) in the code and still get 2009-06-22 19:41:58.564 25WordsMax[21765:813] *** -[CFArray objectAtIndex:]: message sent to deallocated instance 0x14c330 when I hit the button.

And what is calling the datasource method? I'm not calling reloadData. If in my datasource method, I return @"A Word" for each row, everything runs fine. In GDB, I can even see my NSLogs printing the contents of the new array, all without a hitch. It's only when the datasource method as seen in the question gets called that any problems happen.

+1  A: 

Sounds like you have a reference counting error somewhere, which isn't going to be debuggable from just that snippet of code. Try running with NSZombieEnabled=1, and then looking for retain/release bugs where you manage whatever object you get zombie warnings about.

smorgan
I enabled zombies, and got this:2009-06-22 18:55:03.368 25WordsMax[19761:813] *** -[CFArray objectAtIndex:]: message sent to deallocated instance 0x14ba00Which seems to stem fromreturn [[currentCard words] objectAtIndex:rowIndex];What's weird is that I thought you had to call reloadData on a tableView before it would call it's data source method again, and yet my reloadData call is commented out.I've been trying to track down this memory bug for a few hours, but can't find anything. Any more ideas?Thanks
Walker Argendeli
A: 

NSZombie is telling you that the object returned from [currentCard words] is getting released somewhere, then you're using it again. Check all of your assignments to that variable and make sure that you're not setting it to an autoreleased object. Then check all release and autorelease messages sent to it.

Tom Dalling
No, it's telling him that the array got released somewhere, and then he used it again.
Peter Hosey
Ah yes. My mistake.
Tom Dalling
+4  A: 

2009-06-22 18:55:03.368 25WordsMax[19761:813] *** -[CFArray objectAtIndex:]: message sent to deallocated instance 0x14ba00

Read that carefully. The receiver was an instance of CFArray, and the message selector was objectAtIndex:.

The deallocated instance was an array, not a game card.

Your game card owns the array, so it must retain it in some way. This means you must either:

  1. Create the array using alloc and init (and not autorelease it).
  2. Retain it.
  3. Make its own copy. (Preferable if you're receiving an array from another object, as in your setter, since the array you receive may be mutable, and you don't want the other object mutating “your” array.)

If you synthesize your accessors, declare the property as either @property(retain) or @property(copy). Note that this won't work if you want a mutable copy; in that case, you'll have to implement the setter yourself.

Peter Hosey
Walker Argendeli
And what is calling the datasource method? I'm not calling reloadData. If in my datasource method, I return @"A Word" for each row, everything runs fine. In GDB, I can even see my NSLogs printing the contents of the new array, all without a hitch. It's only when the datasource method as seen in the question gets called that any problems happen.
Walker Argendeli
The message actually comes from Cocoa, not from GDB. “I got so frustrated I commented out every retain…and still get [the message]….” Well, yeah. If you don't retain it, it's going to die when the autorelease pool drains.
Peter Hosey
I figured out the problem. words, the NSMutableArray, had been "initialized" with+ arrayWithCapacity:with no retain. I changed it to an alloc init using– initWithCapacity:
Walker Argendeli
Thank you for posting your solution, that is exactly what I was doing.
Tony Eichelberger
A: 

It may help to see the GameCard Init and the GameCard dealloc method.

Also to verify that the method you think is the problem is the problem I would check for nil

if( [currentCard words] != nil )
    return [[currentCard words] objectAtIndex:rowIndex];

else
    return @"Blah!";
geekydevjoe