我很困惑一個奇怪的事情....我有一個無符號字符數組....我使用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