SDWebImage / SDWebImageAVIFCoder

A SDWebImage coder plugin to support AVIF(AV1 Image File Format) image
MIT License
55 stars 14 forks source link

SDWebImageAVIFCoder/Classes/Conversion.m CreateCGImage8 方法是否有内存泄漏? #60

Open JsonIOS opened 11 months ago

JsonIOS commented 11 months ago

from chatgpt4: 在正常执行路径上,resultBufferData 不会造成内存泄漏。在函数的末尾,end_all 标签处,有一行代码释放了 resultBufferData

if (resultBufferData) free(resultBufferData);

然而,在某些错误处理路径上,可能存在 resultBufferData 的内存泄漏。在以下情况下,resultBufferData 可能已经分配了内存,但在跳转到错误处理部分时没有释放:

  1. end_no_alpha_monoend_no_alpha_color 标签之后,代码跳转到 end_no_alpha。在这两个标签后,resultBufferData 已经设置为 NULL,因此在 end_all 标签中不会再次释放。要解决这个问题,可以在跳转到 end_no_alpha 之前添加一行代码来释放 resultBufferData
free(resultBufferData);

在其他情况下,代码在 end_all 标签中正确释放了分配的内存。在进行更改后,您应该使用内存分析工具(如 Xcode 中的 Instruments)来检查代码,以确保没有内存泄漏。

dreampiggy commented 11 months ago

You're correct. Maybe need check and fix

JsonIOS commented 11 months ago

Could you please let me know when the memory leak will be fixed?

JsonIOS commented 11 months ago
image

maybe I am wrong, 'resultBufferData' is designed to release memory with image's life cycle

JsonIOS commented 11 months ago

copy from pic: question: CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, result->data, result->rowBytes * result->height, FreeImageData); CGImageRef imageRef = CGImageCreate(result->width, result->height, bitsPerComponent, bitsPerPixel, rowBytes, colorSpace, bitmapInfo, provider, NULL, NO, renderingIntent);

provider 会被持有吗?如果 free(result->data) 会对 imageRef 有影响吗?

answer: 当您使用 CGDataProviderCreateWithData 创建一个 CGDataProviderRef 并将其传递给 CGImageCreate 函数时,CGImageRef 会持有(retain)这个 CGDataProviderRef。这意味着 CGDataProviderRef 的引用计数会增加。只要 CGImageRef 存在,与其关联的 CGDataProviderRef 就不会被释放。

在您的代码中,当创建 CGImageRef 后,您调用了 CGDataProviderRelease(provider)。这会减少 CGDataProviderRef 的引用计数。由于 CGImageRef 仍然持有 CGDataProviderRef,因此 CGDataProviderRef 不会被释放,FreeImageData 函数也不会被调用。

如果您在创建 CGImageRef 后手动调用 free(result->data),这将对 CGImageRef 产生影响。因为 CGImageRef 依赖于 CGDataProviderRef 提供的数据,而 result->data 是这些数据的来源。如果提前释放了 result->data,CGImageRef 将无法访问有效的图像数据,这可能导致未定义行为或程序崩溃。

因此,您不应手动调用 free(result->data)。相反,您应该依赖于 FreeImageData 函数来在适当的时机释放这些数据。当 CGImageRef 的引用计数变为 0 时,它会释放与其关联的 CGDataProviderRef,从而触发 FreeImageData 函数的调用。要确保这一点,请在使用完 CGImageRef 后调用 CGImageRelease 函数。