Re: Monster memory leak and I can't figure out why
Re: Monster memory leak and I can't figure out why
- Subject: Re: Monster memory leak and I can't figure out why
- From: "email@hidden" <email@hidden>
- Date: Tue, 1 Jun 2010 19:20:54 +0100
On 1 Jun 2010, at 02:41, Ken Tozier wrote:
> Hi
>
> I'm trying to write a thumbnailer class that takes a path to a photo and creates a thumbnail at a user specified size. The code creates the thumbnails OK, but there's this monster memory leak, to the tune of about 100 MB every 3-4 seconds, that seems to be related to NSImage.
Have you tried using Instruments? It was built for this sort of thing.
Are you sure that it is a leak and not just the memory usage pattern.
You can trigger a collection with [[NSGarbageCollector defaultCollector] collectExhaustively];
>
> What's happening is that if I comment out the line that initializes a new image, the memory leak disappears.
If don't allocate anything then you won't get ANY memory usage!
> I've tried forcibly releasing the images, tried autoreleasing them, tried creating a fixed size buffer into which all the images are read, nothing seems to work.
With GC release et al are no-ops so calling them does nothing.
>
> I'm using garbage collection, so that along with the deliberate releasing of the images, makes me wonder why the collector isn't getting the hint, that it's ok to release the memory for these photos. Could someone point out what I'm doing in the following code that prevents the images from getting released?
>
> Thanks for any help
>
> - (NSString *) createJPEGThumbnail:(NSString *) inPath
> site:(NSString *) inSite
> {
> NSDictionary *siteRecord,
> *pubRecord;
>
> NSString *sourceName = [inPath lastPathComponent],
> *sourceRoot = [sourceName stringByDeletingPathExtension],
> *destName = [[sourceRoot stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding] stringByAppendingPathExtension: @"jpg"],
> *pubCode = [self pubCodeFromPath: inPath],
> *thumbPath;
>
> NSFileManager *manager = [NSFileManager defaultManager];
>
> siteRecord = [thumbDirectories objectForKey: inSite];
> pubRecord = [[siteRecord objectForKey: @"publications"] objectForKey: pubCode];
>
> if (pubRecord == nil)
> pubRecord = [[siteRecord objectForKey: @"publications"] objectForKey: @"~MISCELLANEOUS"];
>
> thumbPath = [[pubRecord objectForKey: @"thumb_path"] stringByAppendingPathComponent: destName];
>
> if (![manager fileExistsAtPath: thumbPath])
> {
> // I've tried both of these, didn't make the slightest difference.
> // Both leaked memory at a furious pace
>
> // Option 1:
> NSImage *image = [[[NSImage alloc] initWithContentsOfFile: inPath] autorelease];
>
> /* do some stuff */
What happens to image here?
If a reachable reference is created to image then the collector will not scavenge it.
>
>
> // Option 2
> NSImage *image = [[NSImage alloc] initWithContentsOfFile: inPath];
>
> /* do some stuff */
>
> [image release];
> }
>
> // make sure it worked
> if ([manager fileExistsAtPath: thumbPath])
> return thumbPath;
> else
> return nil;
> }
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden