2010-05-05 86 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

相關問題