我有一个结构定义为:
struct VImage
{
uint8_t *imageData;
int dimensions[3];
...
}
我正在分析我的代码以查找泄漏和其他问题。考虑以下代码:
for (int i = 0; i < 100; i++)
{
// after we have the image, we use the cheap rgb function to get rgb data
struct VImage *vImage = [SomeClass someFunction:someArgument];
free(vImage);
}
这会使我的代码崩溃。我想知道为什么会这样。病情i < 10
但不会使应用程序崩溃。仪器上的分析并不表明存在任何泄漏;然而分配激增并且我使用了大量的实时内存;所以我想知道以这种方式释放结构是否正确,或者 free 是否是一个异步函数,它将释放某人的内存,但不仅仅是当我要求它释放内存时。
编辑:所以我被要求提供此 someFunction
的详细信息方法,我会的。我认为该方法没有内存泄漏,但尽管如此,我还是将其放在这里:
+ (struct VImage *)grayScaleImageDataFromImage:(UIImage *)image
{
int imageWidth = image.size.width, imageHeight = image.size.height;
uint32_t *rgbImageData = (uint32_t *) malloc(imageWidth * imageHeight * sizeof(uint32_t));
memset(rgbImageData, 0, imageWidth * imageHeight * sizeof(uint32_t));
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
// create context with RGBA pixels
CGContextRef context = CGBitmapContextCreate(rgbImageData, imageWidth, imageHeight, 8, imageWidth * sizeof(uint32_t), colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipLast);
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
CGContextSetShouldAntialias(context, NO);
// paint to fill pixels array
CGContextDrawImage(context, CGRectMake(0, 0, imageWidth, imageHeight), [image CGImage]);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
// now convert to grayscale using CCIR 601 <=> Y' = 0.299 R' + 0.587 G' + 0.114 B'.
uint8_t *grayScaleImageData = (uint8_t *) malloc(imageWidth * imageHeight);
for(int y = 0; y < imageHeight; y++)
{
for(int x = 0; x < imageWidth; x++)
{
uint32_t rgbPixel = rgbImageData[y*imageWidth+x];
grayScaleImageData[y*imageWidth + x] = 0.299*((rgbPixel>>24)&255) + 0.587*((rgbPixel>>16)&255) + 0.114*((rgbPixel>>8)&255);
}
}
struct VImage *vImage = createGrayScaleDescription(grayScaleImageData, imageHeight, imageWidth);
free(rgbImageData);
return vImage;
}
struct VImage *createGrayScaleDescription(uint8_t *grayScaleImage, int height, int width)
{
struct VImage *vImage = malloc(sizeof(struct VImage));
vImage->grayScaleImage = grayScaleImage;
vImage->numberOfDimensions = 2;
vImage->dimensions[0] = height, vImage->dimensions[1] = width, vImage->dimensions[2] = 1;
vImage->isGrayscale = true;
return vImage;
}
所以我所做的就是通过一些计算使用 RGB 数据创建灰度图像。不过,我认为这里没有任何问题。
最佳答案
一方面,您正在泄漏grayScaleImageData
指向的内存:
在grayScaleImageDataFromImage中你会这样做
uint8_t *grayScaleImageData = (uint8_t *) malloc(imageWidth * imageHeight);
这很好,然后在createGrayScaleDescription
中将vImage->grayScaleImage
设置为指向该内存,这也很好。
但是在 1 到 100 循环中,您释放了 vImage,而没有首先释放 vImage->grayScaleImage
指向的内存。这是内存泄漏。
我认为这不会导致您的应用程序崩溃,但这是您需要修复的问题。我没有什么其他的想法,但我会仔细看看。
编辑:仔细检查后,您似乎超出了分配的缓冲区
grayScaleImageData
。
你用以下方式分配它:
uint8_t *grayScaleImageData = (uint8_t *) malloc(imageWidth * imageHeight);
但是你用以下内容填充该缓冲区:
uint32_t rgbPixel = rgbImageData[y*imageWidth+x];
grayScaleImageData[y*imageWidth + x] = 0.299*((rgbPixel>>24)&255) + 0.587*((rgbPixel>>16)&255) + 0.114*((rgbPixel>>8)&255);
您已分配 (imageWidth * imageHeight) 8 位 uint,但随后用 32 位 uint 填充它。
也许这就是导致你崩溃的原因?也许尝试将 malloc 更改为
uint32_t *grayScaleImageData = (uint32_t *) malloc(imageWidth * imageHeight * sizeof (uint32_t));
就像上面对 rgbImageData 所做的那样?或者将位移计算的结果转换为 uint8_t,然后将其设置为grayScaleImageData:
uint32_t rgbPixel = rgbImageData[y*imageWidth+x];
grayScaleImageData[y*imageWidth + x] = (uint8_t)(0.299*((rgbPixel>>24)&255) + 0.587*((rgbPixel>>16)&255) + 0.114*((rgbPixel>>8)&255));
关于objective-c - 结构内存分配(泄漏和崩溃,iPhone 上的分析),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21996394/