views:

433

answers:

3

Hello i have the following error:

malloc: * error for object 0x2087000: pointer being freed was not allocated * set a breakpoint in malloc_error_break to debug

I have no idea what object that is. I don't know how to find it. Can anybody explain to me how (and where) to use malloc_history. I have set a breakpoint in malloc_error_break but i couldn't find out what object is at that address. I receive this after removing an object from an nsmutablearray and popping a view controller manually. If I comment out the line [reviewUpload.images removeObject: self.reviewUploadImg] it doesn't give me the error but of course it's not useful for me like that.

- (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];
}

I tried to print out 3 adressess but neither of them is the address that was being freed but not allocated.

I add the image from an 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];
}

I don't think i have anything wrong here: ReviewUpload has an images array which may contain ReviewUploadImage objects. In the implementation of these classes i have only the release method, I just release the members i have to release.

@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

Isn't there an easy way to get the objective-c object from an adress?!

UPDATE: If i remove either of these lines i don't get the error. I don't know what to say this is really weird.

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

UPDATE: if the image selected in "didFinishPickingMediaWithInfo" is nil no error is displayed. Maybe it has to do something with the way i create the thumbnail. Here is the method that creates it. If anybody has any idea please help.

-(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;
}
+2  A: 

When the object is removed from array it's being send a dealloc message. Take a look on your dealloc function of ReviewUploadImage class. I see you have some thumb member there. Check whether you retain it somewhere. Anyway if your app stops on breakpoint you've set, you can use the following GDB commands to see the stack:

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

hope it helps.

EDIT: I saw your code, I think you should retain the thumbImage before assigning it to a class member thumb, or use the setter, setter will do it for you automatically.

Nava Carmon
Thank you i will try these commands. The strange thing is that it isn't the address of reviewUpload or the current reviewUploadImg or the thumb from reviewUploadImg. I know this because i log these adresses in revomeImage just before the error. but neither is the "right" one, but it seems strange to me that all i can do is guess the object by address.
kudor gyozo
thumbImage is a newly created image. I wrote the function copyResizedImage. When i pass thumbImage to reviewUploadImg it is retained because of @property(nonatomic, retain) UIImage *thumb.And in the error it is not the address of thumbImage.
kudor gyozo
in removeImage function try to log self also.
Nava Carmon
no... it's not self :(
kudor gyozo
Check this thread http://stackoverflow.com/questions/295778/iphone-debugging-pointer-being-freed-was-not-allocated-errors
Nava Carmon
A: 

I did a malloc_history but i don't really understand these results. Is this showing all the allocs and deallocs for that address? Do they have to match? (because they don't, the ALLOC line is larger) The allocs and deallocs? Sorry for being this noob and askink questions in the answer.

Side question: Why doesn't the program freeze? I had similar occasions with over releasing memory and they ended in EXC_BAD_ACCESS or something similar.

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 


kudor gyozo
A: 

After one month i magically solved the error by uninstalling iphone os 4 beta sdk and installing 3.2 sdk. It doesn't give me the error anymore. Damn... i want my wasted time back :(.

kudor gyozo