CoreImage: Newbie can't find memory leak
CoreImage: Newbie can't find memory leak
- Subject: CoreImage: Newbie can't find memory leak
- From: Chris Lewis <email@hidden>
- Date: Tue, 14 Feb 2006 20:51:44 +0000
Hi guys,
Thanks in advance to anyone reading this, I know it's long. I am very grateful!
I'm a Cocoa newbie, coming from a C/Java background. I've been using
CoreImage to implement a machine vision project, and it's going really
well. I'm loving the power/speed I've been getting. Today I ran into a
brick wall, and I'm really really stuck, and leaking memory like hell.
I have a webcam (hooked to CocoaSequenceGrabber) that's calling a
function each time it grabs an image. I'm running the camera live,
putting the image through some CoreImage filters, and outputting the
result. This has been going great, but now I am trying to create a
filter that creates a composite image from all the images it has seen.
My problem is that it leaks memory like crazy, the numbers in
ObjectAlloc just keep going up, whereas I'd been keeping a steady
amount of active objects before.
Here's the calling function:
======================
- (void)getCurrentImage : (NSNotification *)note
{
CIImage *ciThreshold;
// SNIP. ciThreshold is correctly created here, no problems there. //
[compositor addImage:ciThreshold];
}
======================
I can't see anything wrong with this.
compositor is an instance of ImageCompositor, a class I made to do the
compositing.
I have an instance variable CIImage *composite, which holds the
current composite.
addImage checks if anything has been added before, and if it hasn't,
makes a copy of that image.
======================
- (void)addImage : (CIImage *)aImage
{
if (composite == nil)
{
NSLog(@"Making new composite start point");
composite = [aImage copy];
}
else
{
[self addImageToComposite:aImage];
}
}
======================
So this is where I hand off to, and the leak occurs.
======================
- (void)addImageToComposite : (CIImage *)aImage
{
CIImage *newComposite = [ImageMethodLibrary compositeImage:composite
overlayingImage:aImage];
[newComposite retain];
[composite release];
NSLog(@"Composite retain count is %d", [composite retainCount]);
composite = newComposite;
}
======================
For completeness, this is the filter:
======================
+ (CIImage *)compositeImage : (CIImage *)aImage
overlayingImage : (CIImage *)overlayImage
{
CIFilter *f = [CIFilter filterWithName:@"CIMaximumCompositing"];
[f setValue:aImage forKey:@"inputBackgroundImage"];
[f setValue:overlayImage forKey:@"inputImage"];
CIImage *returnImage = [f valueForKey:@"outputImage"];
return returnImage;
}
======================
It's in addImageToComposite that I'm leaking. ObjectAlloc tells me I'm
making a huge amount of active CIImage instances (the function could
be called up to 30 times a second), so I guessed I'm missing a release
somewhere. I did the retain because CoreImage says that it'll
autorelease unretained objects, which seems to fit with what I've been
doing. I've just been getting the image, converting it to an NSImage
and then jamming it into a NSImageView. That wasn't causing a leak, so
I guess the objects must have been autoreleased.
So I looked at the retain count. And it's completely off-the-wall.
Most of the time the retain count is 3 after I tried to get rid of it,
but sometimes it'll be 2! How on EARTH it can vary is beyond me. Even
worse, because an Autorelease is scheduled somewhere (and it's not me
that's done it), if I try and hack it to force a release by simply
calling [composite release] the amount of times it appears in the
retainCount, I end up with the NSPopAutoReleasePool crashing the
program.
Does anyone know where the leak is, and how I can patch it up? Is
there a way of telling Cocoa that you don't mind if an autorelease
fails, or forcing a full release?
I appreciate all of your time, thank you ever so much. I've spent all
day on this, and I really have got nowhere *sigh* Hopefully one of you
guys will notice my newbie mistake without too much trouble!
Chris Lewis
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden