调用 initWithBitmapData 时未分配正在释放的 Malloc 指针错误
Malloc pointer being freed was not allocated error when calling initWithBitmapData
当我通过调用此例程创建 CIImage 时,标题中出现 malloc 错误。当我直接调用 initWithBitmapData(不在 createCIUimageFromData 例程中)时,它工作正常。
我在 iOS 中看到了可能相关的错误参考,但我不能确定,而且我肯定比 Apple 的代码更怀疑我的代码!
我的猜测是我的附加重定向以某种方式搞砸了事情,但是拥有单独的例程比在我需要的任何地方嵌入代码更干净。
谢谢。
失败:
- (CIImage *) createCIimageFromData : (unsigned char *)pData width : (int32_t)width height : (int32_t)height
{
/*
Once we have the raw data, we convert it into a CIImage.
The following code does the required work.
*/
NSLog(@"entering createciimage\n");
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceGray();
NSData *_pixelsData = [NSData dataWithBytesNoCopy:pData length:(sizeof(unsigned char)*width*height) freeWhenDone:YES ];
CIImage *_dataCIImage = [[CIImage alloc] initWithBitmapData:_pixelsData bytesPerRow:(width*sizeof(unsigned char)) size:CGSizeMake(width,height) format:kCIFormatR8 colorSpace:colorSpaceRef];
CGColorSpaceRelease(colorSpaceRef);
/*
newImage is the final image
Do remember to release the allocated parts.
*/
NSLog(@"done ciimage\n");
return _dataCIImage;
}
作品:
void prepData(unsigned char *pData, // source-destination
int strideSrc, // stride
int width,
int height,
double amount,
int deltaLimit,
id owner)
{
//[owner createCIimageFromData:pData width:width height:height]; // <-- commented out
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceGray();
NSData *_pixelsData = [NSData dataWithBytesNoCopy:pData length:(sizeof(unsigned char)*width*height) freeWhenDone:YES ];
CIImage *_dataCIImage = [[CIImage alloc] initWithBitmapData:_pixelsData bytesPerRow:(width*sizeof(unsigned char)) size:CGSizeMake(width,height) format:kCIFormatR8 colorSpace:colorSpaceRef];
CGColorSpaceRelease(colorSpaceRef);
// . . .
}
显然问题是在 NSData
对象试图释放数据时引起的。为避免此问题,请使用 freeWhenDone:NO
,然后在完成 CIImage
.
后释放数据
当我通过调用此例程创建 CIImage 时,标题中出现 malloc 错误。当我直接调用 initWithBitmapData(不在 createCIUimageFromData 例程中)时,它工作正常。
我在 iOS 中看到了可能相关的错误参考,但我不能确定,而且我肯定比 Apple 的代码更怀疑我的代码!
我的猜测是我的附加重定向以某种方式搞砸了事情,但是拥有单独的例程比在我需要的任何地方嵌入代码更干净。
谢谢。
失败:
- (CIImage *) createCIimageFromData : (unsigned char *)pData width : (int32_t)width height : (int32_t)height
{
/*
Once we have the raw data, we convert it into a CIImage.
The following code does the required work.
*/
NSLog(@"entering createciimage\n");
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceGray();
NSData *_pixelsData = [NSData dataWithBytesNoCopy:pData length:(sizeof(unsigned char)*width*height) freeWhenDone:YES ];
CIImage *_dataCIImage = [[CIImage alloc] initWithBitmapData:_pixelsData bytesPerRow:(width*sizeof(unsigned char)) size:CGSizeMake(width,height) format:kCIFormatR8 colorSpace:colorSpaceRef];
CGColorSpaceRelease(colorSpaceRef);
/*
newImage is the final image
Do remember to release the allocated parts.
*/
NSLog(@"done ciimage\n");
return _dataCIImage;
}
作品:
void prepData(unsigned char *pData, // source-destination
int strideSrc, // stride
int width,
int height,
double amount,
int deltaLimit,
id owner)
{
//[owner createCIimageFromData:pData width:width height:height]; // <-- commented out
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceGray();
NSData *_pixelsData = [NSData dataWithBytesNoCopy:pData length:(sizeof(unsigned char)*width*height) freeWhenDone:YES ];
CIImage *_dataCIImage = [[CIImage alloc] initWithBitmapData:_pixelsData bytesPerRow:(width*sizeof(unsigned char)) size:CGSizeMake(width,height) format:kCIFormatR8 colorSpace:colorSpaceRef];
CGColorSpaceRelease(colorSpaceRef);
// . . .
}
显然问题是在 NSData
对象试图释放数据时引起的。为避免此问题,请使用 freeWhenDone:NO
,然后在完成 CIImage
.