2010-05-05 113 views
0

更新:我无法找到导致问题的原因我正在使用beta 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个地址,但他们都不是被释放但未被分配的地址。

我从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 

是不是有一种简单的方式来获得一个ADRESS 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; 
} 
+0

你可以把你的代码初始化reviewUploadImg吗?什么是reviewUpload?它是什么样的对象?这将有助于更好地理解您的问题 – 2010-05-05 07:41:01

+0

reviewUploadImg是ReviewUploadImage类,用于存储Review的评论图像(ReviewUpload类)。用户可以为公司或产品撰写评论,并可将图片附加到reviewUpUp.images中存储的评论中。 – 2010-05-05 07:48:06

+0

这个问题为什么这么受欢迎? – 2011-03-25 15:20:36

回答

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会自动为你做。

+0

谢谢,我会尝试这些命令。奇怪的是,它不是rev​​iewUpload的地址或当前的reviewUploadImg或来自reviewUploadImg的大拇指。我知道这是因为我在错误发生之前将这些地址记录在revomeImage中。但它们都不是“正确的”,但对我来说,我所能做的只是通过地址来猜测对象。 – 2010-05-05 08:15:35

+0

thumbImage是一个新创建的图像。我写了函数copyResizedImage。当我通过thumbImage reviewUploadImg它被保留,因为@property(nonatomic,retain)UIImage * thumb。 而在错误中,它不是thumbImage的地址。 removeImage函数中的 – 2010-05-05 08:17:39

+0

也尝试登录自己。 – 2010-05-05 08:36:43

0

我做了一个malloc_history,但我不太了解这些结果。这是显示该地址的所有alloc和deallocs?他们必须匹配吗? (因为他们不,ALLOC线更大)alloc和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 


-1

一个月以后我奇迹般地通过卸载的iPhone OS 4测试版SDK和安装3.2 SDK解决了这个错误。它不会再给我错误了。

+0

如何到这个地狱回答有帮助??? – 2012-12-13 15:14:23

+0

对不起,我没有把这个标记为接受。我不知道是什么问题 – 2012-12-17 17:41:09

+0

这个有趣的是有一个测试sdk中的错误? – 2014-03-30 22:31:02

相关问题