简体   繁体   English

C和Objective-C - 正确释放无符号字符指针的方法

[英]C and Objective-C - Correct way to free an unsigned char pointer

in my app I create an unsigned char pointer using this function: 在我的应用程序中,我使用此函数创建一个unsigned char指针:

- (unsigned char*)getRawData
{
// First get the image into your data buffer
CGImageRef image = [self CGImage];
NSUInteger width = CGImageGetWidth(image);
NSUInteger height = CGImageGetHeight(image);

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

unsigned char *rawData = malloc(height * width * 4);
NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * width;
NSUInteger bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(rawData, width, height, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);

CGContextSetBlendMode(context, kCGBlendModeCopy);

CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, (CGFloat)width, (CGFloat)height), image);
CGContextRelease(context);

// Now your rawData contains the image data in the RGBA8888 pixel format.

return rawData;
}

And in another class I assign a property to that pointer like so: self.bitmapData = [image getRawData]; 在另一个类中,我将一个属性分配给该指针,如下所示:self.bitmapData = [image getRawData];

Where in this process can I free that malloc'd memory? 在这个过程中我可以释放那个malloc内存吗? When I try to free the property in dealloc, it gives me an exc_bad_access error. 当我尝试在dealloc中释放属性时,它会给我一个exc_bad_access错误。 I feel like I'm missing a fundamental c or objective-c concept here. 我觉得我在这里缺少一个基本的c或objective-c概念。 All help is appreciated. 所有帮助表示赞赏。

There is a good discussion about the safety of using malloc/free in objective-c here . 有一个关于使用malloc /免费在Objective-C的安全商量好了这里

As long as you correctly free() the memory that you malloc(), there should be no issue. 只要你正确地释放()你malloc()的内存, 应该没有问题。

I personally think that using NSMutableData or NSMutableArray is just easier. 我个人认为使用NSMutableData或NSMutableArray更容易。 If you don't need ultimate performance, I would not use the C malloc/free statements directly. 如果您不需要最终性能,我不会直接使用C malloc / free语句。

One way around this sort of issue is to use NSMutableData, so you can replace 解决此类问题的一种方法是使用NSMutableData,因此您可以替换

unsigned char *rawData = malloc(height * width * 4);

with

myData = [[NSMutableData alloc] initWithCapacity:height * width * 4];
unsigned char *rawData = myData.mutableBytes;

you can then release myData in your deallocator. 然后,您可以在解除分配器中释放myData。

alternativly you can do 另外你可以这样做

myData = [NSMutableData dataWithCapacity:height * width * 4];

This will then mean your myData is kept around the the duration of the event loop, you can of cause even change the return type of getRawData method to return NSMUtableData or NSData, and that way it can be retained by other parts of your code, the only time I return raw bytes in my code is if I know it will be available for the life of the object that returns it, that way if I need to hold onto the data I can retain the owner class. 这将意味着你的myData保持在事件循环的持续时间周围,你甚至可以更改getRawData方法的返回类型以返回NSMUtableData或NSData,这样它可以被代码的其他部分保留,只有当我在代码中返回原始字节时,如果我知道它将在返回它的对象的生命周期中可用,那样如果我需要保留数据,我可以保留所有者类。

Apple will often use the Apple经常会使用

myData = [[NSMutableData alloc] initWithCapacity:height * width * 4];
unsigned char *rawData = myData.mutableBytes;

pattern and then document that if you need the bytes beyond the current autorelease pool cycle you will then have to copy it. 模式,然后记录,如果您需要超出当前自动释放池周期的字节,您将必须复制它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM