Walker
Walker

Reputation: 1225

When TableView's DataSource Array Changes, App Crashes

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.

Upvotes: 2

Views: 1099

Answers (4)

geekydevjoe
geekydevjoe

Reputation: 108

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!";

Upvotes: 0

Tom Dalling
Tom Dalling

Reputation: 24115

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.

Upvotes: 0

Peter Hosey
Peter Hosey

Reputation: 96333

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.

Upvotes: 5

smorgan
smorgan

Reputation: 21579

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.

Upvotes: 1

Related Questions