malloc in obj-c/cocoa
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.