gpt4 book ai didi

ios - iOS 应用程序中 Objective-C 代码的内存泄漏

转载 作者:行者123 更新时间:2023-11-28 23:57:47 25 4
gpt4 key购买 nike

我的代码正在消耗内存。我添加了这个函数,它似乎是所有问题的原因,因为当我不调用它时,我就不会用完。

它是 Objective-C 中用于裁剪图像的函数。如何释放拍卖中使用的内存,以便在函数结束时退出之前清除所有内容。

-(void) crop: (CVImageBufferRef)sampleBuffer
{
int cropX0, cropY0, cropHeight, cropWidth, outWidth, outHeight;

cropHeight = 720;
cropWidth = 1280;
cropX0 = 0;
cropY0 = 0;

outWidth = 1280;
outHeight = 720;

CVPixelBufferLockBaseAddress(sampleBuffer,0);
void *baseAddress = CVPixelBufferGetBaseAddress(sampleBuffer);
size_t bytesPerRow = CVPixelBufferGetBytesPerRow(sampleBuffer);

vImage_Buffer inBuff;
inBuff.height = cropHeight;
inBuff.width = cropWidth;
inBuff.rowBytes = bytesPerRow;

int startpos = cropY0*bytesPerRow+4*cropX0;
inBuff.data = baseAddress+startpos;

unsigned char *outImg= (unsigned char*)malloc(4*outWidth*outHeight);
vImage_Buffer outBuff = {outImg, outHeight, outWidth, 4*outWidth};

vImage_Error err = vImageScale_ARGB8888(&inBuff, &outBuff, NULL, 0);
if (err != kvImageNoError)
{
NSLog(@" error %ld", err);
}
else
{
NSLog(@"Success");
}


CVPixelBufferRef pixelBuffer = NULL;
OSStatus result = CVPixelBufferCreateWithBytes(kCFAllocatorDefault,
inBuff.width,
inBuff.height,
kCVPixelFormatType_32BGRA,
outImg,
bytesPerRow,
NULL,
NULL,
NULL,
&pixelBuffer);


CVPixelBufferUnlockBaseAddress(sampleBuffer,0);

}

最佳答案

免费(outImg);最后丢失,因为您没有释放分配的内存。这是嵌入式编程的一个好习惯,也是在这里,因为你有 const size 像素尺寸来使用一个 const 矩阵,你可以在函数的顶部声明并初始化为零。

关于ios - iOS 应用程序中 Objective-C 代码的内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50627060/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com