• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Fixing my massive memory leak with NSImage/CALayer
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Fixing my massive memory leak with NSImage/CALayer


  • Subject: Fixing my massive memory leak with NSImage/CALayer
  • From: Nathan Vander Wilt <email@hidden>
  • Date: Tue, 1 Apr 2008 11:07:24 -0700

I want to pop up photographs inside their own Core Animation layers, but I get huge real memory usage that never goes down.

I have an FRPhoto class whose instances manage other metadata for the image, and also have a convenience method to get an NSImage* as follows:

- (NSImage*)image {
	if (!cachedImage) {
		cachedImage = [[NSImage alloc] initByReferencingFile:filename];
	}
	return cachedImage;
}

...which I use to show the photo, in my view's method:

- (void)showPhoto:(FRPhoto*)photo {
CALayer* layer = [CALayer layer];
CGImageRef image = [[[[photo image] representations] objectAtIndex:0] CGImage];
layer.contents = (id)image;
layer.frame = CGRectMake(0.0f, 0.0f, 50.0f, 50.0f);
layer.contentsGravity = kCAGravityResizeAspect;
[interactionLayer addSublayer:layer];
[layersForPhotos setObject:layer forKey:photo];
}


To put the FRPhoto into the layersForPhotos dictionary, I just implemented a lazy retained "copy":

- (id)copyWithZone:(NSZone*)zone {
	(void)zone;
	return [self retain];
}

These FRPhoto instances are kept basically indefinitely, so when I hide the photo (also in the view) I make sure to call -[NSImage recache]:

- (void)hidePhoto:(FRPhoto*)photo {
	CALayer* layer = [layersForPhotos objectForKey:photo];
	[layersForPhotos removeObjectForKey:photo];
	[layer removeFromSuperlayer];
	layer.contents = nil;
	[[photo image] recache];
}


So by my reckoning: The CALayer should be autoreleased. And its contents, too, were supposed to be an autoreleased CGImage, and I set that property to nil just in case the layer itself is somehow sticking around. The NSImage that my FRPhoto holds on to should have "invalidate[d] and free[d] the offscreen caches of all image representations". But yet my memory usage just goes up and up and up, every photo I view. Images I have looked at already pop right back up, so they definitely act like they're still cached somewhere.


Even after many non-photo-showing event cycles, or just leaving it be for a few minutes, absolutely no significant amount of memory has been released according to both Instruments' ObjectAlloc and the Activity Monitor. By the time I've look at a dozen images my RAM is totally overwhelmed, making iPhoto's 15 second beachballs every couple minutes look pretty attractive, compared to total system-wide gridlock.

What am I doing wrong? How can I get these images out of memory?

thanks,
-natevw

_______________________________________________

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


  • Follow-Ups:
    • Re: Fixing my massive memory leak with NSImage/CALayer
      • From: Jens Alfke <email@hidden>
  • Prev by Date: Re: ERROR: __CFURLCache::StepSQLStatement
  • Next by Date: Re: Getting Localized System Strings
  • Previous by thread: Re: CoreAnimation with and manual animation...
  • Next by thread: Re: Fixing my massive memory leak with NSImage/CALayer
  • Index(es):
    • Date
    • Thread