• 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
malloc in obj-c/cocoa
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

malloc in obj-c/cocoa


  • Subject: malloc in obj-c/cocoa
  • From: Ben Dougall <email@hidden>
  • Date: Fri, 6 Feb 2004 21:14:03 +0000

is it ok to use malloc in objective-c code? i did a bit of c code that uses malloc, got it working fine, copied and pasted it into my cocoa app, and the malloc part is playing havoc. what extra issues are there with using malloc in obj-c, if any that is? where can i read about that?

i'm malloc'ing little bits of memory, passing the pointers round and stuff. getting this error:

*** malloc[22753]: error for object 0x7dbd0: Incorrect checksum for freed object - object was probably modified after being freed; break at szone_error

and then exits due to signal 10 (SIGBUS).

this is the backtrace from gdb:

0 free_list_remove
1 small_malloc_from_region_no_lock
2 szone_calloc
3 malloc_zone_calloc
4 object_getIndexedIvars
5 NSAllocateObject
6 +[NSMutableString allocWithZone]
7 +[NSMutableString allocWithZone]
8 -[MyObject myMethod] // (entries above this one are in grey)
...

the method that's alloc'ing an NSMutableString there, has a pointer passed to it when called, that points to some memory i previously malloc'ed. but that actual NSMutableString creation does not actually make use of that pointer. it's almost as if my previous malloc is messing with cocoa's use of malloc? or am i making that up?

i typedef'd a pointer type like so:

typedef u_int8_t *PositionPtr;

(just to be absolutely sure - that makes PositionPtr stand for a u_int8_t pointer, right? (just checking))

then the malloc that occurs before the above backtrace takes place in a c function whose code is part of a .m file (the same file that the above method's backtrace is from):

PositionPtr posp;
posp = (PositionPtr) malloc( i ));
while( i >= 0 ) {
*(posp + i) = *(Buffer + i);
i--;
}

then posp gets return to the method that called the c function. then that pointer gets passed to the method where the SIGBUS crash occurs.

i've got no idea what's going on. any ideas? there's been other problems i've come across as i've tried to get it to work, but the above error is the most common one. are there extra issues to using malloc in obj-c, to plain c?

thanks, ben.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

  • Follow-Ups:
    • Re: malloc in obj-c/cocoa
      • From: Jens Bauer <email@hidden>
    • Re: malloc in obj-c/cocoa
      • From: Pete Yandell <email@hidden>
    • Re: malloc in obj-c/cocoa
      • From: Fritz Anderson <email@hidden>
    • Re: malloc in obj-c/cocoa
      • From: Casey Fleser <email@hidden>
    • Re: malloc in obj-c/cocoa
      • From: John Randolph <email@hidden>
  • Prev by Date: Question about "JPEG writer requires compacted components"
  • Next by Date: Re: Optimisation [inlining] of message calls (was Re: Multiple problems caused by changing NSThreadsleepUntilDate interval)
  • Previous by thread: Question about "JPEG writer requires compacted components"
  • Next by thread: Re: malloc in obj-c/cocoa
  • Index(es):
    • Date
    • Thread