malloc during selectAll -> crash
malloc during selectAll -> crash
- Subject: malloc during selectAll -> crash
- From: Lorenzo <email@hidden>
- Date: Thu, 11 May 2006 16:16:08 +0200
I have verified that calling
unsigned int *buf = malloc(totItems);
during a [outlineView selectAll:sender], I get a crash, soon, or later on,
when I select any different window. The problem has gone away replacing the
malloc call with
unsigned int buf[totItems];
But since in some class variable case I "must" use malloc, and I get a crash
too, may you please help me to understand how to avoid this problem at
calling malloc?
The crash:
I have an outlineView. When I call selectAll automatically Cocoa invokes
outlineViewSelectionDidChange and, in this method I collect the items
currently selected into an NSMutableArray. As I have seen in some sample
code, I get the selected items this way.
//////////////////////
NSMutableArray *items = [NSMutableArray array];
NSIndexSet *selectedRows = [self selectedRowIndexes];
unsigned int i, totItems = [selectedRows count];
if(totItems == 0) return items ;
unsigned int *buf = malloc(totItems);
NSRange range = NSMakeRange([selectedRows firstIndex],
[selectedRows lastIndex] + 1);
[selectedRows getIndexes:buf maxCount:totItems inIndexRange:&range];
for(i = 0; i < totItems; i++){
id item = [self itemAtRow:buf[i]];
if(item) [items addObject:item];
}
if(buf){
free(buf);
buf = nil;
}
return items ;
//////////////////////
Best Regards
--
Lorenzo
email: email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden