开发者

NSMutableArray Element lost when displaying in NSTableView is called

I was trying to display the contents of an array into a table view. The table view has single column. My class is RKCandidate which is a subclass of NSWindowController. RKCandidate is also the Owner of the 'nib' file In IB I have selected the File's Owner as the dataSource of the tableview.

There are the methods I'm using to update the tableview,

- (id) initWithClient:(TextInputController *) client
{
 self = [super initWithWindowNibName:@"RKCandidate"];
 if (self != nil) {
  _client = client;

 }
 return self;
}

- (void) dealloc
{
 [optionsArray release];
 [super dealloc];
}
- (void) updateCandidate {

 NSLog(@"updateCandidate Called.\n");

 optionsArray = [_client composedStringArray:self];
 [candidateOptions reloadData];

 NSLog(@"Retain Count of OptionsArray: %i\n",[optionsArray retainCount]);
 NSLog(@"Object Count of OptionsArray: %i\n",[optionsArray count]);
 NSLog(@"Address of Object: %i\n",[optionsArray objectAtIndex:0]);
 NSLog(@"Object: %@\n",[optionsArray objectAtIndex:0]);
 //NSString *benChar = [optionsArray objectAtIndex:0];
 //NSLog(@"Object Retain Count: %@i\n",[benChar retainCount]);
  }
#pragma mark TableView Data Source Methods
#pragma mark -

- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView {

 NSLog(@"Retain Count of OptionsArray: %i\n",[optionsArray retainCount]);
 NSLog(@"Object Count of OptionsArray: %i\n",[optionsArray count]);
 NSLog(@"Address of Object: %i\n",[optionsArray objectAtIndex:0]);
 NSLog(@"Object: %@\n",[optionsArray objectAtIndex:0]);
 //NSLog(@"Object Retain Count: %@i\n",[[optionsArray objectAtIndex:0] retainCount]);

 return [optionsArray count];
}

- (id)tableView:(NSTableView *)aTableView 
     objectValueForTableColumn:(NSTableColumn *)aTableColumn 
    row:(NSInteger)rowIndex {

 NSLog(@"Retain Count of OptionsArray: %i\n",[optionsArray retainCount]);
 NSLog(@"Object Count of OptionsArray: %i\n",[optionsArray count]);
 NSLog(@"Address of Object: %i\n",[optionsArray objectAtIndex:0]);
 NSLog(@"Object: %@\n",[optionsArray objectAtIndex:0]);
 //NSLog(@"Object Retain Count: %@i\n",[[optionsArray objectAtIndex:0] retainCount]);

 return [optionsArray objectAtIndex:rowIndex];
}

The optionsArray is updated by a simple method from client the method is as follows,

-(NSMutableArray *) composedStringArray:(id)sender {

 NSLog(@"returning composedStringArray.\n");

 return convertBufferArray;
}

convertBufferArray is a mutable array.

When the updateCandidate method is called for the first time. It works nicely. But problem starts when I call it second time after updating the convertBufferArray array inside the client.

When the updateCandidate method is called from the first time the Console log is as follows:

2010-11-20 09:03:35.385 Input Method Tester[7225:a0f] Retain Count of OptionsArray: 1
2010-11-20 09:03:35.385 Input Method Tester[7225:a0f] Object Count of OptionsArray: 1
2010-11-20 09:03:35.386 Input Method Tester[7225:a0f] Address of Object: 1372384
2010-11-20 09:03:35.386 Input Method Tester[7225:a0f] Object: ∆
2010-11-20 09:03:35.392 Input Method Tester[7225:a0f] Retain Count of OptionsArray: 1
2010-11-20 09:03:35.392 Input Method Tester[7225:a0f] Object Count of OptionsArray: 1
2010-11-20 09:03:35.393 Input Method Tester[7225:a0f] Address of Object: 1372384
2010-11-20 09:03:35.393 Input Method Tester[7225:a0f] Object: ∆
2010-11-20 09:03:35.397 Input Method Tester[7225:a0f] Retain Count of OptionsArray: 1
2010-11-20 09:03:35.397 Input Method Tester[7225:a0f] Object Count of OptionsArray: 1
2010-11-20 09:03:35.398 Input Method Tester[7225:a0f] Address of Object: 1372384
2010-11-20 09:03:35.399 Input Method Tester[7225:a0f] Object: ∆

The table view displays the content of the array.

But when I just add another letter to the Object string, some how, the string gets replaced with an empty string when the - (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex is called. The log is as follows.

2010-11-20 09:03:38.349 Input Method Tester[7225:a0f] returning composedStringArray.
2010-11-20 09:03:38.350 Input Method Tester[7225:a0f] Retain Count of OptionsArray: 1
2010-11-20 09:03:38.351 Input Method Tester[7225:a0f] Object Count of OptionsArray: 1
2010-11开发者_运维知识库-20 09:03:38.352 Input Method Tester[7225:a0f] Address of Object: 1372384
2010-11-20 09:03:38.353 Input Method Tester[7225:a0f] Object: ƍ
2010-11-20 09:03:38.354 Input Method Tester[7225:a0f] Retain Count of OptionsArray: 1
2010-11-20 09:03:38.354 Input Method Tester[7225:a0f] Object Count of OptionsArray: 1
2010-11-20 09:03:38.354 Input Method Tester[7225:a0f] Address of Object: 1372384
2010-11-20 09:03:38.355 Input Method Tester[7225:a0f] Object: ƍ
2010-11-20 09:03:38.358 Input Method Tester[7225:a0f] Retain Count of OptionsArray: 1
2010-11-20 09:03:38.358 Input Method Tester[7225:a0f] Object Count of OptionsArray: 1
2010-11-20 09:03:38.359 Input Method Tester[7225:a0f] Address of Object: 1372384
2010-11-20 09:03:38.359 Input Method Tester[7225:a0f] Object: 

As you can see in the list line of the above log the string is empty. The tableView become empty.

Is there anything wrong I'm doing? I've tried putting the release and retaining the optionsArray but the result is the same. I also tried to copy the content of the composedBufferArray into the the optionsArray. I tried putting

optionsArray = [_client composedStringArray:self];

into the init method. The result is the same for all the cases.

Please help. I couldn't solve this problem for last two days. Please help.

Sorry I couldn't post a image or a link as I'm totally new in Stack Overflow.

Regards,

Raiyan


First:

Do not call retainCount

It is useless for debugging and the returned number is often quite thoroughly misleading. For starters, an object can never return a 0 for the retain count and, thus, trying to find over-released object using retainCount is entirely futile.

Your bug looks like a memory management issue. That it isn't crashing is simply luck.

You should read the Cocoa Memory Management Guide a couple of times (until it sinks in -- when I started w/Objective-C, I found reading the language docs a few times was tremendously valuable).

Your -init method looks suspect; _client = client; will not retain _client.

You should also use "build and analyze" on your code. The llvm static analyzer will identify most memory management issues in your code.

Finally, learn to use the zombie detection instrument. See some of the answers here for more information.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜