我很困惑一个奇怪的事情....我有一个无符号字符数组....我使用calloc分配它,并记录一些字节数据...但是当我释放这个无符号字符并再次分配,我发现它保留了前一次分配的内存中的相同地址。我明白为什么......但我不明白为什么我第二次尝试写入的数据没有被写入......写入了第一次写入的数据......有人可以解释我吗? ??????无符号字符分配和免费问题
unsigned char *rawData = (unsigned char*) calloc(height * width * 4, sizeof(unsigned char));
这是我如何分配它.... 其实我的问题是,由于这种分配,其中发生一次,每2秒我有内存泄漏......但是,当我尝试释放的分配的内存部门发生上述事情.... :(
请,如果有人能帮助我....我会很高兴... 这里是代码...
- (unsigned char*) createBitmapContext:(UIImage*)anImage{
CGImageRef imageRef = [anImage CGImage];
NSUInteger width = CGImageGetWidth(imageRef);
NSUInteger height = CGImageGetHeight(imageRef);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char *rawData = (unsigned char*) calloc(height * width * 4, sizeof(unsigned char));
bytesPerPixel = 4;
bytesPerRow = bytesPerPixel * width;
NSUInteger bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(rawData, width, height,
bitsPerComponent, bytesPerRow, colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
CGContextRelease(context);
imageRef=nil;
return rawData; }
在这个代码中没有这个部分我在那里免费(rawData),因为我不能释放它在这个方法里面,我试图在调用这个方法后全局定义rawData并释放它...但没有什么有趣的...。 ...我会很高兴...
您将需要提供一个完整的代码片段,显示分配和缓冲区写入,以及如何验证内容。以上是不足以回答你的问题。 – gavinb
@gavinb完成...任何有趣的想法? – Garnik