Skip navigation.
 
mlRe: Is there any compression library for Cocoa?
FROM : Trygve Inda
DATE : Wed Apr 30 11:24:16 2008

> There's a handy NSData-Compression class in Dustin Mierau's NetSocket
> 0.9 sample code available here:
>
> http://blackholemedia.com/code/
>
> Here's a snippet from the header file.
>
> @interface NSData (Compression)
> - (NSData*)compressedData;
> - (NSData*)compressedDataWithLevel:(int)inLevel;
> - (NSData*)uncompressedData;
> @end
>
> @interface NSMutableData (Compression)
> - (BOOL)compress;
> - (BOOL)uncompress;
> @end


This code (from above) appears to leak:

- (NSData*)compressedDataWithLevel:(int)inLevel
{
    Bytef*    data = (Bytef*)[self bytes];
    uLongf    originalLength = [self length];
    uLongf    bufferLength = ( [self length] * 1.1 ) + 16;
    Bytef*    buffer;
    int        err;
   
    if( inLevel > NSDataCompressionBest )
        inLevel = NSDataCompressionBest;
   
    if( inLevel < Z_DEFAULT_COMPRESSION )
        inLevel = NSDataCompressionNone;
   
    buffer = (Bytef*)malloc( (uInt)bufferLength );
    err = compress2( buffer, &bufferLength, (const Bytef*)data, [self
length], inLevel );
    if( err != Z_OK )
    {
        free( buffer );
        return nil;
    }
   
    bcopy( buffer, buffer + sizeof( uLongf ), bufferLength );
    bcopy( (void*)&originalLength, buffer, sizeof( uLongf ) );
   
    return [NSData dataWithBytesNoCopy:buffer length:bufferLength + sizeof(
uLongf )];
}


If the compression is successful, and I later release the (compressed)
NSData, buffer is still around, right?

Is there an update to this code?

How could I add buffer to a pool of sorts to be released when the object is
released as it is just malloc'd.

Thanks,

Trygve

Related mailsAuthorDate
mlIs there any compression library for Cocoa? Allen Dang Nov 29, 17:55
mlRe: Is there any compression library for Cocoa? Christopher Hickma… Nov 29, 18:13
mlRe: Is there any compression library for Cocoa? Andreas Monitzer Nov 29, 19:38
mlRe: Is there any compression library for Cocoa? Allen Dang Nov 30, 16:41
mlRe: Is there any compression library for Cocoa? David Riggle Nov 30, 18:46
mlRe: Is there any compression library for Cocoa? Trygve Inda Apr 30, 11:24
mlRe: Is there any compression library for Cocoa? Luc Heinrich Apr 30, 11:28
mlRe: Is there any compression library for Cocoa? matt.gough Apr 30, 12:21
mlRe: Is there any compression library for Cocoa? Trygve Inda Apr 30, 12:35
mlRe: Is there any compression library for Cocoa? matt.gough Apr 30, 12:39
mlRe: Is there any compression library for Cocoa? Trygve Inda Apr 30, 13:40
mlRe: Is there any compression library for Cocoa? Christopher Nebel Apr 30, 18:04