views:

537

answers:

2

I am trying to make NSTableDataSource compatible object and give this object to NSTableView as DataSource, however when table tries to display data, it crashes.

@interface NSArrayDataSource : NSObject{
    NSArray* internalArray;
}
-(id) initWithArray: (NSArray*) objects;
-(int)numberOfRowsInTableView:(NSTableView *)aTableView;
-(id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex;
-(void)tableView:(NSTableView *)aTableView setObjectValue:(id)anObject forTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex;
@end


@implementation NSArrayDataSource 

-(id) initWithArray: (NSArray*) objects{
    if(self = [super init])
    {
     internalArray = [[NSArray alloc] initWithArray:objects];
    }
    return self;
}

-(int)numberOfRowsInTableView:(NSTableView *)aTableView{
    return [internalArray count];
}

-(id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex{
    id obj = [internalArray objectAtIndex:rowIndex];
        // when I debug, I get same pointers with invalid data
        // each object has "name" message
        // this following line gives invalid pointer and
        // it crashes
    return [obj name];
}

-(void)tableView:(NSTableView *)aTableView setObjectValue:(id)anObject forTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex{
}

@end

Can anyone tell me what could be the problem?

Right now I have removed every release and dealloc messages from my class, so no object is removed, I have done retain message call everywhere when I transfer data.

NSArrayDataSource* nsds = [[NSArrayDataSource alloc] initWithArray: myArray];
[tableView setDataSource:nsds];

I have one doubt, does NSArray does call [retain] for every objects while creating new object or it is just storing pointer values?

Suppose my object's name is "SLProject" then when I debug, in init method I see values under debug window correctly but in tableView delegation method the internalArray's debugger displays object of type "NSKVONotifying_SLProject" and they point to invalid data, however the addresses are correct.

+1  A: 

In answer to your question about NSArray object creation, yes, each object receives a retain message when it is added to the array, and a release message when it is removed. From the Apple docs:

In general, objects that you add to an array aren’t copied; rather, each object receives a retain message before its id is added to the array. When an object is removed from an array, it’s sent a release message.

As for your object's name function, how is it implemented? If you replace [obj name] with [obj description] does this still crash your application?

Perspx
If I initialize object like this...SLProject* p = [[SLProject alloc] init];I have not specified here autorelease, so this means this object will never be deallocated right? what happens if I add it an array, and that array gets deleted, this object will remain as I havent made autorelease?
Akash Kava
Thank you for your help, its working now.
Akash Kava
In response to your first comment, it is common to release an object once it has been added to an array (the retain count will be incremented then decremented, with no net effect); this means you do not need to release the objects when the array is. Otherwise there will be a memory leak.
Perspx
+1  A: 

My first suspicion would be that the -name NSString property of the SLProject class is not being retained. It would help to know the message in the debug log that describes the crash because without that we are just guessing.

As for the "NSKVONotifying_" prefix on your class name, at runtime Cocoa will create a subclass of your object when you are using Key Value Observing. This is normal and is unlikely to be the cause of your problem.

Nathan Kinsinger
Thanks, I was reading value from xml, and I had used convenience property and that was autorelease, I did copy and it started working.Thank you.
Akash Kava