Skip to content

Commit 27385c6

Browse files
authored
Merge pull request #40 from SDWebImage/fix_copy_data_buffer_threads
Fix ICC profile, buffer and AVIF encoding memory leak
2 parents 99e6c22 + d5b9708 commit 27385c6

File tree

3 files changed

+18
-16
lines changed

3 files changed

+18
-16
lines changed

SDWebImageAVIFCoder/Classes/ColorSpace.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ void SDAVIFCalcColorSpaceMono(avifImage * avif, CGColorSpaceRef* ref, BOOL* shou
215215
}
216216
if(avif->icc.data && avif->icc.size) {
217217
if(@available(macOS 10.12, iOS 10.0, tvOS 10.0, *)) {
218-
CFDataRef iccData = CFDataCreateWithBytesNoCopy(kCFAllocatorDefault, avif->icc.data, avif->icc.size,kCFAllocatorNull);
218+
CFDataRef iccData = CFDataCreate(kCFAllocatorDefault, avif->icc.data, avif->icc.size);
219219
*ref = CGColorSpaceCreateWithICCData(iccData);
220220
CFRelease(iccData);
221221
*shouldRelease = TRUE;
@@ -313,7 +313,7 @@ void SDAVIFCalcColorSpaceRGB(avifImage * avif, CGColorSpaceRef* ref, BOOL* shoul
313313
}
314314
if(avif->icc.data && avif->icc.size) {
315315
if(@available(macOS 10.12, iOS 10.0, tvOS 10.0, *)) {
316-
CFDataRef iccData = CFDataCreateWithBytesNoCopy(kCFAllocatorDefault, avif->icc.data, avif->icc.size,kCFAllocatorNull);
316+
CFDataRef iccData = CFDataCreate(kCFAllocatorDefault, avif->icc.data, avif->icc.size);
317317
*ref = CGColorSpaceCreateWithICCData(iccData);
318318
CFRelease(iccData);
319319
*shouldRelease = TRUE;

SDWebImageAVIFCoder/Classes/Conversion.m

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -704,11 +704,11 @@ static CGImageRef CreateCGImage8(avifImage * avif) {
704704
}
705705

706706
end_all:
707-
free(resultBufferData);
708-
free(argbBufferData);
709-
free(dummyCbData);
710-
free(dummyCrData);
711-
free(scaledAlphaBufferData);
707+
if (resultBufferData) free(resultBufferData);
708+
if (argbBufferData) free(argbBufferData);
709+
if (dummyCbData) free(dummyCbData);
710+
if (dummyCrData) free(dummyCrData);
711+
if (scaledAlphaBufferData) free(scaledAlphaBufferData);
712712
return result;
713713
}
714714

SDWebImageAVIFCoder/Classes/SDImageAVIFCoder.m

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -210,12 +210,14 @@ - (nullable NSData *)encodedDataWithImage:(nullable UIImage *)image format:(SDIm
210210
vImage_Buffer src;
211211
v_error = vImageBuffer_InitWithCGImage(&src, &srcFormat, NULL, imageRef, kvImageNoFlags);
212212
if (v_error != kvImageNoError) {
213+
vImageConverter_Release(convertor);
213214
return nil;
214215
}
215216
vImage_Buffer dest;
216-
vImageBuffer_Init(&dest, height, width, hasAlpha ? 32 : 24, kvImageNoFlags);
217-
if (!dest.data) {
218-
free(src.data);
217+
v_error = vImageBuffer_Init(&dest, height, width, hasAlpha ? 32 : 24, kvImageNoFlags);
218+
if (v_error != kvImageNoError) {
219+
if (src.data) free(src.data);
220+
vImageConverter_Release(convertor);
219221
return nil;
220222
}
221223

@@ -224,15 +226,15 @@ - (nullable NSData *)encodedDataWithImage:(nullable UIImage *)image format:(SDIm
224226
free(src.data);
225227
vImageConverter_Release(convertor);
226228
if (v_error != kvImageNoError) {
227-
free(dest.data);
229+
if(dest.data) free(dest.data);
228230
return nil;
229231
}
230232

231233
avifPixelFormat avifFormat = AVIF_PIXEL_FORMAT_YUV444;
232234

233235
avifImage *avif = avifImageCreate((int)width, (int)height, 8, avifFormat);
234236
if (!avif) {
235-
free(dest.data);
237+
if (dest.data) free(dest.data);
236238
return nil;
237239
}
238240
avifRGBImage rgb = {
@@ -245,7 +247,6 @@ - (nullable NSData *)encodedDataWithImage:(nullable UIImage *)image format:(SDIm
245247
};
246248
avifImageRGBToYUV(avif, &rgb);
247249
free(dest.data);
248-
dest.data = NULL;
249250

250251
NSData *iccProfile = (__bridge_transfer NSData *)CGColorSpaceCopyICCProfile([SDImageCoderHelper colorSpaceGetDeviceRGB]);
251252

@@ -264,14 +265,15 @@ - (nullable NSData *)encodedDataWithImage:(nullable UIImage *)image format:(SDIm
264265
encoder->maxThreads = 2;
265266
avifResult result = avifEncoderWrite(encoder, avif, &raw);
266267

268+
avifImageDestroy(avif);
269+
avifEncoderDestroy(encoder);
267270
if (result != AVIF_RESULT_OK) {
268-
avifEncoderDestroy(encoder);
271+
if (raw.data) avifRWDataFree(&raw);
269272
return nil;
270273
}
271274

272275
NSData *imageData = [NSData dataWithBytes:raw.data length:raw.size];
273-
free(raw.data);
274-
avifEncoderDestroy(encoder);
276+
avifRWDataFree(&raw);
275277

276278
return imageData;
277279
}

0 commit comments

Comments
 (0)