Kzrbill
Kzrbill

Reputation: 1448

iPhone memory leak issue in ZipArchive lib

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.

Upvotes: 0

Views: 690

Answers (2)

Bad Boy
Bad Boy

Reputation: 628

i have faced the same problem and Solved by reading some of the blogs . it seems to simple
close your ZipArchive Object before relesing Object
its look like
[ZipObj UnzipCloseFile];

Upvotes: 2

Dave DeLong
Dave DeLong

Reputation: 243156

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.

Upvotes: 0

Related Questions