@@ -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