From a83768a9c4bae19a15003503bb52e33cd04c9c05 Mon Sep 17 00:00:00 2001 From: brandons Date: Wed, 20 May 2015 11:03:00 -0700 Subject: [PATCH] Fix memory issues per https://github.com/seanooi/iOS-WebP/issues/16 --- iOS-WebP/UIImage+WebP.m | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/iOS-WebP/UIImage+WebP.m b/iOS-WebP/UIImage+WebP.m index 6aac49e..12b2482 100644 --- a/iOS-WebP/UIImage+WebP.m +++ b/iOS-WebP/UIImage+WebP.m @@ -11,10 +11,12 @@ // This gets called when the UIImage gets collected and frees the underlying image. static void free_image_data(void *info, const void *data, size_t size) { - if(info != NULL) - WebPFreeDecBuffer(&(((WebPDecoderConfig *)info)->output)); - else - free((void *)data); + if(info != NULL) { + WebPFreeDecBuffer(&(((WebPDecoderConfig *) info)->output)); + free(info); + } else { + free((void *) data); + } } @implementation UIImage (WebP) @@ -124,8 +126,8 @@ + (UIImage *)imageWithWebPData:(NSData *)imgData error:(NSError **)error return nil; } - WebPDecoderConfig config; - if(!WebPInitDecoderConfig(&config)) { + WebPDecoderConfig * config = malloc(sizeof(WebPDecoderConfig)); + if(!WebPInitDecoderConfig(config)) { NSMutableDictionary *errorDetail = [NSMutableDictionary dictionary]; [errorDetail setValue:@"Failed to initialize structure. Version mismatch." forKey:NSLocalizedDescriptionKey]; if(error != NULL) @@ -133,13 +135,13 @@ + (UIImage *)imageWithWebPData:(NSData *)imgData error:(NSError **)error return nil; } - config.options.no_fancy_upsampling = 1; - config.options.bypass_filtering = 1; - config.options.use_threads = 1; - config.output.colorspace = MODE_RGBA; + config->options.no_fancy_upsampling = 1; + config->options.bypass_filtering = 1; + config->options.use_threads = 1; + config->output.colorspace = MODE_RGBA; // Decode the WebP image data into a RGBA value array - VP8StatusCode decodeStatus = WebPDecode([imgData bytes], [imgData length], &config); + VP8StatusCode decodeStatus = WebPDecode([imgData bytes], [imgData length], config); if (decodeStatus != VP8_STATUS_OK) { NSString *errorString = [self statusForVP8Code:decodeStatus]; @@ -152,7 +154,7 @@ + (UIImage *)imageWithWebPData:(NSData *)imgData error:(NSError **)error // Construct UIImage from the decoded RGBA value array uint8_t *data = WebPDecodeRGBA([imgData bytes], [imgData length], &width, &height); - CGDataProviderRef provider = CGDataProviderCreateWithData(&config, data, config.options.scaled_width * config.options.scaled_height * 4, free_image_data); + CGDataProviderRef provider = CGDataProviderCreateWithData(config, data, config->options.scaled_width * config->options.scaled_height * 4, free_image_data); CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB(); CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault |kCGImageAlphaLast; @@ -165,7 +167,6 @@ + (UIImage *)imageWithWebPData:(NSData *)imgData error:(NSError **)error CGImageRelease(imageRef); CGColorSpaceRelease(colorSpaceRef); CGDataProviderRelease(provider); - WebPFreeDecBuffer(&config.output); return result; }