TahDoodle not responsive / EXC_BAD_ACCESS


#1

Hi,

I 'm a beginner…

I entered the code below.

[code]//
// MyDocument.h
// TahDoodle2
//

#import <Cocoa/Cocoa.h>

@interface MyDocument : NSDocument
{
NSMutableArray *todoItems;
IBOutlet NSTableView *itemTableView;
}

  • (IBAction) createNewItem: (id)sender;
    @end

[/code]

[code]//
// MyDocument.m
// TahDoodle2
//

#import “MyDocument.h”

@implementation MyDocument

#pragma mark - NSDocument Overrides

  • (NSString *)windowNibName
    {

    return @“MyDocument”;
    }

#pragma mark - Actions

  • (IBAction) createNewItem: (id)sender
    {
    if (!todoItems) {
    todoItems =[NSMutableArray array];
    }

    [todoItems addObject:@“New Item”];

    [itemTableView reloadData];

    [self updateChangeCount:NSChangeDone];
    }

// test log

#pragma mark - Data Source Methods

  • (NSInteger) numberOfRowsInTableView :(NSTableView *)tv
    {
    return [todoItems count];
    }

  • (id)tableView:(NSTableView *)tableView
    objectValueForTableColumn:(NSTableColumn *)tableColumn
    row: (NSInteger)row;
    {
    return [todoItems objectAtIndex:row];
    }

  • (void) tableView:(NSTableView *)tableView
    setObjectValue:(id)object
    forTableColumn:(NSTableColumn *)tableColumn
    row:(NSInteger)row
    {
    [todoItems replaceObjectAtIndex:row
    withObject:object];
    [self updateChangeCount:NSChangeDone];
    }

  • (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError
    {

    if (!todoItems) {
    todoItems = [NSMutableArray array];
    }

    NSData *data = [NSPropertyListSerialization
    dataWithPropertyList:todoItems
    format:NSPropertyListXMLFormat_v1_0
    options: 0
    error];
    return data;

}

  • (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError **)outError
    {
    todoItems = [NSPropertyListSerialization
    propertyListWithData:data
    options:NSPropertyListMutableContainers
    format:NULL
    error];
    return (todoItems !=nil);
    }

@end[/code]
And linked everything in Interface Builder. (Button to File’s Owner, table view to FO and FO to tv)

When I run TahDoodle I get the window, the table view and the insert button. There is nothing in the table view.

When I push the insert button, TahDoodle crashes. In mydocument.m, at the bottom, I get : GDB: Program received signal “EXC_BAD_ACCESS”.

What went wrong?


#2

What are you using for memory management: Automatic or Manual Reference Counting?


#3

I don’t know.
Where can I check?


#4

The easiest way is to change:

- (IBAction) createNewItem: (id)sender
{
   if (!todoItems) {
       todoItems =[NSMutableArray array];
   }
   ...

To:

- (IBAction) createNewItem: (id)sender
{
   if (!todoItems) {
       todoItems =[[NSMutableArray array] retain];
   }
   ...

To see if Xcode complains about sending the retain message.

If it does not complain, that means you are using Manual Reference Counting.


#5

I changed the code.
Xcode didn’t complain.
So I use manual reference counting.

Besides that, TahDoodle works now.

Thank you, ibex10 !