views:

50

answers:

2

I'm gratefully using the ZipArchive library but it seems there is a memory leak. I don't know how to fix this - it's written in C and I'm unfamiliar with it. My question is - is it possible to autorelease the line s=(unz_s*)ALLOC(sizeof(unz_s)); like you would in Objective-C in this scenario?

extern unzFile ZEXPORT unzOpen2 (path, pzlib_filefunc_def)
        const char *path;
        zlib_filefunc_def* pzlib_filefunc_def;
    {

        // ...

        s=(unz_s*)ALLOC(sizeof(unz_s));
        *s=us;
        unzGoToFirstFile((unzFile)s);
        return (unzFile)s;
    }

Here is a screen grab of the location of the leak for clarity:

http://ziparchive.googlecode.com/issues/attachment?aid=-5463964192517894688&name=Screen+shot+2010-08-20+at+8.12.58+PM.png&token=8c66aa58a4826b99ba157903fbae83bb&inline=1

Can anybody could shed some light on how I might fix this? Thanks.

A: 

No, you cannot autorelease it. Autoreleasing is only available for Objective-C objects. So you have 2 options:

  1. Free the memory yourself using free().
  2. Wrap the s thing into an NSData using dataWithBytesNoCopy:length:, which will take ownership of the allocated data and free it when the NSData object is deallocated.

Option 2 would look something like this:

unz_s * s = unzOpen2(...);
NSData * boxedS = [NSData dataWithBytesNoCopy:s length:sizeof(unz_s)];

Then when boxedS gets destroyed, it will free s as well.

Dave DeLong
Thankyou, though it's still leaking despite an attempted fix. The ALLOCated product of unzOpen2 is assigned directly to a private class member instance variable like so within the utilising class:_unzFile = unzOpen( (const char*)[zipFile UTF8String]);So in the class's dealloc method I'm releasing it like this:if (_unzFile != NULL){ free(_unzFile); } I didn't want to go down the route of containing the C object within an NSData* object as there are many references to the instance variable _unzFile: I'd be getting further out of my depth with the languange fusion here!
Kzrbill
A: 

Dear Kzrbill i am going through this too' but wanna ask you! Are you using the complete Zip Archive Library? or the simpler one?

Ahmad Kayyali