• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: malloc during selectAll -> crash
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: malloc during selectAll -> crash


  • Subject: Re: malloc during selectAll -> crash
  • From: Ryan Britton <email@hidden>
  • Date: Thu, 11 May 2006 07:31:09 -0700


On May 11, 2006, at 7:16 AM, Lorenzo wrote:

////////////////////// NSMutableArray *items = [NSMutableArray array]; NSIndexSet *selectedRows = [self selectedRowIndexes]; unsigned int i, totItems = [selectedRows count]; if(totItems == 0) return items ;

unsigned int *buf = malloc(totItems);

You're allocating a buffer here of totItems bytes. An int is not a single byte so you're bound to get a memory access error when you attempt to access something outside your malloc'd memory. Try replacing this with something like unsigned int *buf = (unsigned int *) malloc(totlItems * sizeof(unsigned int));


It's possible for malloc to return NULL if it's unable to allocate the memory so you might check that too.


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 ;

_______________________________________________ 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
  • Follow-Ups:
    • Re: malloc during selectAll -> crash
      • From: Jim Correia <email@hidden>
    • Re: malloc during selectAll -> crash
      • From: Lorenzo <email@hidden>
References: 
 >malloc during selectAll -> crash (From: Lorenzo <email@hidden>)

  • Prev by Date: Re: malloc during selectAll -> crash
  • Next by Date: NSNumber math?
  • Previous by thread: Re: malloc during selectAll -> crash
  • Next by thread: Re: malloc during selectAll -> crash
  • Index(es):
    • Date
    • Thread