正在释放的指针未分配

时间:2010-05-05 07:35:16

标签: iphone objective-c pointers malloc

更新:我找不到导致我使用测试版SDK的问题的原因,当使用最终的SDK时,问题就消失了。我不知道自己做错了什么。抱歉。我想删除它但不能删除。

您好我有以下错误:

malloc: *对象0x2087000的错误:未分配被释放的指针 * 在malloc_error_break中设置断点以进行调试

我不知道那是什么对象。我不知道怎么找到它。任何人都可以向我解释如何(以及在​​何处)使用malloc_history。我在malloc_error_break中设置了一个断点,但我找不到该地址的对象。从nsmutablearray中删除对象并手动弹出视图控制器后,我收到此消息。如果我注释掉[reviewUpload.images removeObject:self.reviewUploadImg]这一行,它就不会给我错误,但当然这对我来说没什么用。

- (void) removeImage
{
    debugLog(@"reviewUploadImg %@", self.reviewUploadImg);
    debugLog(@"reviewUpload %@", self.reviewUpload);
    debugLog(@"reviewUploadImg thumb %@", self.reviewUploadImg.thumb);
    [reviewUpload.images removeObject: self.reviewUploadImg];
    [self.navigationController popViewControllerAnimated:TRUE];
}

我试图打印出3个adressess,但它们都不是被释放但未分配的地址。

我从UIImagePickerController添加图片。

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{

    UIImage *pickedImage = (UIImage *)[info objectForKey: UIImagePickerControllerEditedImage];
    if (!pickedImage)
        pickedImage = (UIImage *)[info objectForKey: UIImagePickerControllerOriginalImage];

    if (!reviewUpload.images)
        reviewUpload.images = [[NSMutableArray alloc] initWithCapacity: 5];

    //UIImage *thumbImage = [pickedImage copyResizedImage: CGSizeMake(120, 120)];
    UIImage *thumbImage = [pickedImage copyResizedImage:CGSizeMake(120, 120) withPaddingColor: [UIColor lightGrayColor]];

    ReviewUploadImage *rui = [[ReviewUploadImage alloc] init];
    rui.thumb = thumbImage;
    [thumbImage release];

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat: @"yyyyMMddHHmmssSS"];

    NSDate *date = [NSDate date];
    NSString *tmpFileName = [dateFormatter stringFromDate: date];

    [dateFormatter release];

    NSString *path = [NSTemporaryDirectory() stringByAppendingPathComponent: tmpFileName];

    NSData *imageData = [[NSData alloc] initWithData: UIImageJPEGRepresentation(pickedImage, 0.90)];
    [imageData writeToFile: path atomically: TRUE];
    [imageData release];

    rui.path = path;
    [reviewUpload.images addObject: rui];
    debugLog(@"rui rc %i", rui.retainCount);
    [rui release];

    [self dismissModalViewControllerAnimated:TRUE];
}

我认为这里没有任何错误:ReviewUpload有一个可能包含ReviewUploadImage对象的图像数组。在这些类的实现中,我只有发布方法,我只是发布了我必须发布的成员。

@interface ReviewUpload : NSObject 
{
    NSString        *title;
    NSString        *tags;
    NSString        *text;
    NSInteger       rating;
    NSMutableArray  *images;

    NSInteger       idCompany;
    NSInteger       idProduct;

}

@property(nonatomic, copy)      NSString *title;
@property(nonatomic, copy)      NSString *tags;
@property(nonatomic, copy)      NSString *text;
@property(nonatomic, assign)    NSInteger rating;
@property(nonatomic, retain)    NSMutableArray *images;

@property(nonatomic, assign)    NSInteger idCompany;
@property(nonatomic, assign)    NSInteger idProduct;

@end
@interface ReviewUploadImage : NSObject 
{
    UIImage *thumb;
    NSString    *path;
}

@property(nonatomic, retain)    UIImage *thumb;
@property(nonatomic, copy)      NSString    *path;

@end

是否有一种简单的方法可以从地址中获取objective-c对象?!

更新: 如果我删除这些行中的任何一行,我不会得到错误。我不知道该怎么说这真的很奇怪。

    [reviewUpload.images removeObject: self.reviewUploadImg];
    [self.navigationController popViewControllerAnimated:TRUE];

更新:如果在“didFinishPickingMediaWithInfo”中选择的图像为零,则不显示错误。也许它必须与我创建缩略图的方式做一些事情。这是创建它的方法。如果有人有任何想法,请帮助。

-(UIImage *) copyResizedImage:(CGSize) thumbSize withPaddingColor: (UIColor *) bgColor
{
    CGFloat resizedProp = thumbSize.width / thumbSize.height;
    CGFloat imageProp   = self.size.width / self.size.height;

    CGSize newSize;
    CGFloat factor;

    if (imageProp > resizedProp) //image is wider
        factor = thumbSize.width  / self.size.width;
    else
        factor = thumbSize.height / self.size.height;

    newSize.width  = self.size.width * factor;
    newSize.height = self.size.height * factor;

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    UIImage *resizedImage;

    UIGraphicsBeginImageContext(thumbSize);
    CGContextRef drwContextRef = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(drwContextRef, bgColor.CGColor);
    CGContextFillRect(drwContextRef, CGRectMake(0, 0, thumbSize.width, thumbSize.height));
    CGRect imageRect = CGRectMake(
        (thumbSize.width - newSize.width) / 2.0,
        (thumbSize.height - newSize.height) /2.0, newSize.width, newSize.height);
    [self drawInRect:imageRect];
    resizedImage = UIGraphicsGetImageFromCurrentImageContext(); // this is autoreleased and i dont need it
    UIGraphicsEndImageContext();

    [resizedImage retain];
    [pool release];
    return resizedImage;
}

3 个答案:

答案 0 :(得分:4)

当从数组中删除对象时,它将发送一个dealloc消息。看一下ReviewUploadImage类的dealloc函数。我看到你有一些拇指成员。检查一下你是否保留它。无论如何,如果您的应用程序在您设置的断点处停止,您可以使用以下GDB命令查看堆栈:

backtrace -- Print backtrace of all stack frames
bt -- Print backtrace of all stack frames
down -- Select and print stack frame called by this one
frame -- Select and print a stack frame
return -- Make selected stack frame return to its caller
select-frame -- Select a stack frame without printing anything
up -- Select and print stack frame that called this one
希望它有所帮助。

编辑:我看到你的代码,我认为你应该在将它分配给类成员拇指之前保留thumbImage,或者使用setter,setter会自动为你做。

答案 1 :(得分:0)

我做了一个malloc_history,但我真的不明白这些结果。这是否显示该地址的所有allocs和deallocs?他们必须匹配吗? (因为他们没有,ALLOC线更大)allocs和deallocs?很抱歉在答案中出现了这个noob和askink问题。

附带问题:为什么程序不冻结?我有类似的过度释放内存的情况,他们以EXC_BAD_ACCESS或类似的方式结束。

TestWebService(740,0xa067b4e0) malloc: *** error for object 0x20e5000: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
(gdb) shell malloc_history 207 0x20e5000
malloc_history cannot examine process 207 because the process does not exist.
(gdb) shell malloc_history 740 0x20e5000
malloc_history Report Version:  2.0
Process:         TestWebService [740]
Path:            /Users/kudorgyozo/Library/Application Support/iPhone Simulator/3.0/Applications/6AF403F5-6856-44B9-A76E-45F58355535A/TestWebService.app/TestWebService
Load Address:    0x1000
Identifier:      TestWebService
Version:         ??? (???)
Code Type:       X86 (Native)
Parent Process:  gdb-i386-apple-darwin [742]

Date/Time:       2010-05-06 12:02:23.167 +0300
OS Version:      Mac OS X 10.6.3 (10D573)
Report Version:  6

ALLOC 0x20e3800-0x20e512f [size=6448]: thread_a067b4e0 |start | main | UIApplicationMain | GSEventRun | GSEventRunModal | CFRunLoopRunInMode | CFRunLoopRunSpecific | __NSFireDelayedPerform | -[UITableView _userSelectRowAtIndexPath:] | -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] | -[PLAlbumView tableView:didSelectRowAtIndexPath:] | -[PLUIAlbumViewController albumView:selectedPhoto:] | PLNotifyImagePickerOfImageAvailability | -[UIImagePickerController _imagePickerDidCompleteWithInfo:] | -[WriteReviewImagesController imagePickerController:didFinishPickingMediaWithInfo:] | -[UIImage(Thumbnails) copyResizedImage:withPaddingColor:] | -[UIImage drawInRect:] | -[UIImage drawInRect:blendMode:alpha:] | CGContextDrawImage | ripc_DrawImage | ripc_AcquireImage | CGSImageDataLock | img_data_lock | img_interpolate_read | img_decode_read | CGAccessSessionGetChunks | getBytes_cb | getBandProcJPG | _cg_jpeg_start_decompress | _cg_jinit_master_decompress | start_input_pass | start_pass_huff_decoder | _cg_jpeg_make_d_derived_tbl | alloc_small | malloc | malloc_zone_malloc 
----
FREE  0x20e3800-0x20e512f [size=6448]: thread_a067b4e0 |start | main | UIApplicationMain | GSEventRun | GSEventRunModal | CFRunLoopRunInMode | CFRunLoopRunSpecific | __NSFireDelayedPerform | -[UITableView _userSelectRowAtIndexPath:] | -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] | -[PLAlbumView tableView:didSelectRowAtIndexPath:] | -[PLUIAlbumViewController albumView:selectedPhoto:] | PLNotifyImagePickerOfImageAvailability | -[UIImagePickerController _imagePickerDidCompleteWithInfo:] | -[WriteReviewImagesController imagePickerController:didFinishPickingMediaWithInfo:] | -[UIImage(Thumbnails) copyResizedImage:withPaddingColor:] | -[UIImage drawInRect:] | -[UIImage drawInRect:blendMode:alpha:] | CGContextDrawImage | ripc_DrawImage | ripc_AcquireImage | CGSImageDataLock | img_data_lock | img_interpolate_read | img_decode_read | CGAccessSessionGetChunks | getBytes_cb | getBandProcJPG | _cg_jpeg_destroy | self_destruct | free_pool | free 


答案 2 :(得分:-1)

一个月后,我通过卸载iphone os 4 beta sdk并安装3.2 sdk神奇地解决了这个错误。它不再给我错误了。