iPhone 开发:被释放的指针未分配 [英] iPhone development: pointer being freed was not allocated
问题描述
我从调试器收到了这条消息:
i got this message from the debugger:
Pixture(1257,0xa0610500) malloc: *** error for object 0x21a8000: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
所以我做了一些追踪并得到:
so i did a bit of tracing and got:
(gdb) shell malloc_history 1257 0x21a8000
ALLOC 0x2196a00-0x21a89ff [size=73728]: thread_a0610500 |start |主要 |UIApplicationMain |GSEventRun |GSEventRunModal |CFRunLoopRunInMode |CFRunLoopRunSpecific |__CFRunLoopDoObservers |CA::Transaction::observer_callback(__CFRunLoopObserver*, unsigned long, void*) |CA::事务::提交() |CA::Context::commit_transaction(CA::Transaction*) |CALayerDisplayIfNeeded |-[CALayer_display] |CABackingStoreUpdate |backing_callback(CGContext*, void*) |-[CALayer drawInContext:] |-[UIView(CALayerDelegate) drawLayer:inContext:] |-[AvatarView drawRect:] |-[AvatarView overlayPNG:] |+[UIImageUtility createMaskOf:] |UIGraphicsGetImageFromCurrentImageContext |CGBitmapContextCreateImage |create_bitmap_data_provider |malloc |malloc_zone_malloc
我真的不明白我做错了什么.这是 [UIImageUtility createMaskOf:]
函数的代码:
and i really can't understand what i am doing wrong. here's the code of the [UIImageUtility createMaskOf:]
function:
+ (UIImage *)createMaskOf:(UIImage *)source {
CGRect rect = CGRectMake(0, 0, source.size.width, source.size.height);
UIGraphicsBeginImageContext(CGSizeMake(source.size.width, source.size.height));
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, 0, source.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
UIImage *original = [self createGrayCopy:source];
CGContextRef context2 = CGBitmapContextCreate(NULL, source.size.width, source.size.height, 8, 4 * source.size.width,
CGColorSpaceCreateDeviceRGB(), kCGImageAlphaNoneSkipLast);
CGContextDrawImage(context2, CGRectMake(0, 0, source.size.width, source.size.height), original.CGImage);
CGImageRef unmasked = CGBitmapContextCreateImage(context2);
const float myMaskingColorsFrameColor[6] = { 1,256,1,256,1,256 };
CGImageRef mask = CGImageCreateWithMaskingColors(unmasked, myMaskingColorsFrameColor);
CGContextSetRGBFillColor (context, 256,256,256, 1);
CGContextFillRect(context, rect);
CGContextDrawImage(context, rect, mask);
UIImage *whiteMasked = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return whiteMasked;
}
之前调用的另一个自定义函数如下:
the other custom function called before that is the following:
- (UIImage *)overlayPNG:(SinglePart *)sp {
NSLog([sp description]);
// Rect and context setup
CGRect rect = CGRectMake(0, 0, sp.image.size.width, sp.image.size.height);
NSLog(@"%f x %f", sp.image.size.width, sp.image.size.height);
// Create an image of a color filled rectangle
UIImage *baseColor = nil;
if (sp.hasOwnColor) {
baseColor = [UIImageUtility imageWithRect:rect ofColor:sp.color];
} else {
SinglePart *facePart = [editingAvatar.face.partList objectAtIndex:0];
baseColor = [UIImageUtility imageWithRect:rect ofColor:facePart.color];
}
// Crete the mask of the layer
UIImage *mask = [UIImageUtility createMaskOf:sp.image];
mask = [UIImageUtility createGrayCopy:mask];
// Create a new context for merging the overlay and a mask of the layer
UIGraphicsBeginImageContext(CGSizeMake(sp.image.size.width, sp.image.size.height));
CGContextRef context2 = UIGraphicsGetCurrentContext();
// Adjust the coordinate system so that the origin
// is in the lower left corner of the view and the
// y axis points up
CGContextTranslateCTM(context2, 0, sp.image.size.height);
CGContextScaleCTM(context2, 1.0, -1.0);
// Create masked overlay color layer
CGImageRef MaskedImage = CGImageCreateWithMask (baseColor.CGImage, mask.CGImage);
// Draw the base color layer
CGContextDrawImage(context2, rect, MaskedImage);
// Get the result of the masking
UIImage* overlayMasked = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIGraphicsBeginImageContext(CGSizeMake(sp.image.size.width, sp.image.size.height));
CGContextRef context = UIGraphicsGetCurrentContext();
// Adjust the coordinate system so that the origin
// is in the lower left corner of the view and the
// y axis points up
CGContextTranslateCTM(context, 0, sp.image.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
// Get the result of the blending of the masked overlay and the base image
CGContextDrawImage(context, rect, overlayMasked.CGImage);
// Set the blend mode for the next drawn image
CGContextSetBlendMode(context, kCGBlendModeOverlay);
// Component image drawn
CGContextDrawImage(context, rect, sp.image.CGImage);
UIImage* blendedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGImageRelease(MaskedImage);
return blendedImage;
}
推荐答案
我遇到了同样的问题,有很多相同的症状:
I've had the same problem, with a lot of the same symptoms:
- 被释放的指针未分配错误
- 升级到 Xcode 3.2
- 图像代码中发生错误
如果我将构建目标更改为 3.1,模拟器中的错误就会消失.如果我在设备上运行代码,则不会出现错误.可能是 3.0 中的一个错误
If I change the build target to 3.1, the errors in the simulator go away. If I run the code on the device, the errors don't appear. Possibly a bug in 3.0
我的建议是将 3.1 作为您的目标进行测试,如果您愿意,您可以针对 3.0 进行构建以进行发布,而不必担心错误,因为它们不会发生在设备上.
My advice is to test with 3.1 as your target, and if you want, you can build for 3.0 for release and not worry about the errors as they don't happen on the device.
这篇关于iPhone 开发:被释放的指针未分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!