One Quickie


Making naked memory autoreleased (Hacks->General)
From the devious mind of Rainer Brockerhoff : given a pointer + length, dynamically allocate a copy and make it autoreleased, without (directly) involving cocoa objects. One handy usage: giving a plugin a copy of some struct, but be protected against the plugin messing it up.
static void *tempCopyOf(void *data,UInt32 size) {
    void *buffer = NULL;
    if (data) {
        buffer = malloc(size);
        if (buffer) {
            bcopy(data,buffer,size);
            [NSData dataWithBytesNoCopy: buffer  length: size  freeWhenDone: YES];
        }
    }
    return (buffer);
}
You can get really fancy and assign buffer to calloc(1, size) if you want to give it "pass NULL to get a zero-filled buffer back" semantics.

Also note this doesn't play well with garbage collection - caveat nerdor. (Thanks to Ken Ferry for that note)



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

webmonster@borkware.com