One Quickie


Making naked memory autoreleased (General->General)
(courtesy of Mike Ash, who fixed some errors in the previous version of this quickie)
void *tempCopyOf(void *data, NSUInteger size)
{
   return data ? [[NSData dataWithBytes:data length:size] mutableBytes] : NULL;
}
OBTW, these techniques will fail under GC. Here's how you'd write a working one under GC:
void *tempCopyOf(void *data, NSUInteger size)
{
   void *buffer = NULL;
   if( data ) {
       buffer = NSAllocateCollectable(size, 0);
       memcpy(buffer, data, size);
   }
   return buffer;
}
Unfortunately I'm not aware of a nice way that works in dual-mode code.



borkware home | products | miniblog | rants | quickies | cocoaheads
Advanced Mac OS X Programming book

webmonster@borkware.com