PDA

View Full Version : Strange memory leak




Soulstorm
Sep 19, 2009, 07:35 AM
I have this piece of code:

- (void)processAndSaveImageWithID:(NSString *)imageID
{
NSString *imagesDirectory = ...
NSString *mediumImagesDir = ...

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

NSLog(@"processing Image with ID");

UIImage *img = [[UIImage alloc]initWithContentsOfFile:[imagesDirectory stringByAppendingPathComponent:imageID]];
UIImage *mediumImage = [img scaledImage:428];
[img release];

[sharedGlobals saveImageAsJPGFile:mediumImage withQuality:0.6f withFileID:imageID toDirectory:mediumImagesDir];

UIImage *shrinkedImage = [mediumImage scaledImage:100];

[sharedGlobals saveImageAsJPGFile:shrinkedImage withQuality:0.6f withFileID:imageID toDirectory:sharedGlobals.applicationThumbnailsDirectory]; //THIS CAUSES THE ERROR!!!


self.imageView.image = shrinkedImage;
[self applyChangesGloballyWithFileID:imageID];
[self.imageNameTextField setEnabled:YES];

imageNameTextField.userInteractionEnabled = YES;
imageNameTextField.hidden = NO;
takePictureButton.userInteractionEnabled = NO;

[internalPool drain];

}

And here is my code for the "saveImageAsJPGFile" function:
- (void)saveImageAsJPGFile:(UIImage *)image withQuality:(CGFloat)quality withFileID:(NSString *)fileID toDirectory:(NSString *)dir
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
NSLog(@"saving image to %@", dir);
[UIImageJPEGRepresentation(image, quality) writeToFile:[dir stringByAppendingPathComponent:fileID] atomically:NO];
[pool drain];
}



Although static analysis does not give me any errors, the folowing appears on the console log:
009-09-19 14:22:45.325 iMe[6058:5607] saving image to /Users/soulstorm/Library/Application Support/iPhone Simulator/User/Applications/2B74F5DD-A743-484D-92EB-5C9449600C44/Documents/thumbnails
iMe(6058,0xb0103000) malloc: *** error for object 0x188e000: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug

However, I can't say why this is happening. Putting nslogs after each variable created and each process done shows that before the [internalPool drain] every variable has a retain count of 1.

Can anyone help me on this?



Darkroom
Sep 19, 2009, 10:37 AM
However, I can't say why this is happening. Putting nslogs after each variable created and each process done shows that before the [internalPool drain] every variable has a retain count of 1.

Can anyone help me on this?

maybe it's referring to *pool from saveImageAsJPGFile:withQuality:withFileID:toDirectory:?

PhoneyDeveloper
Sep 19, 2009, 03:33 PM
It's possible this is an Apple bug. I've seen some mention of this on the Apple forum. What version of the OS are you testing this with? Can you try it with different versions? I think maybe the apple bug appeared in OS 3.1.

Soulstorm
Sep 20, 2009, 12:55 PM
It's possible this is an Apple bug. I've seen some mention of this on the Apple forum. What version of the OS are you testing this with? Can you try it with different versions? I think maybe the apple bug appeared in OS 3.1.

Although I'm building against 3.0, the bug appeared after updating my iPhone SDK to 3.1.

I have also some other things to point out, like the UIImagePickerController complaining about a plist file, when it first starts up (although this doesn't affect performance and usability at all).