Re: PDFOutline memory management
Re: PDFOutline memory management
- Subject: Re: PDFOutline memory management
- From: John Calhoun <email@hidden>
- Date: Tue, 17 Apr 2007 13:33:13 -0700
On Apr 16, 2007, at 8:39 PM, Adam R. Maxwell wrote:
I was checking over some code for memory leaks with MallocDebug, and
discovered an NSOutlineView datasource method that retains an object
from a PDFOutline instance variable before returning it, but never
releases it. Removing the retains causes a crash
On Tiger, you're right removing the retain will cause a crash.
On Leopard, you do not need to retain.
Unfortunately the underlying implementation of PDFOutline from Tiger
to Leopard changed significantly.
On Tiger, [PDFOutline childAtIndex:] returned and auto-released, one-
off child. This is why removing the retain crashes (because of course
NSOutline doesn't do a retain either).
On Leopard, a PDFOutline retains all of its children, so [PDFOutline
childAtIndex:] returns the same retained child every time (no need for
you to retain).
I think Leopard's implementation is better. In any event, retaining
and returning the PDFOutline (as in the sample code) is just plain
wrong. I'll try to update it to work around this bug.
I didn't stop to try any code, but it seems to me a subclass of
PDFOutline might solve the issue for Tiger. It seems you could keep
your own instance variable to hold the children (an NSMutableArray).
You could override -[PDFOutline childAtIndex:] and when first called
for a child (you array is NULL), allocate your array and call -[super
childAtIndex:] for each child (-[PDFOutline numberOfChildren:]). Once
populated, you can simply return the retained instance from your
array. Of course at dealloc() you need to release your array.
Something like:
@interface MyPDFOutline : PDFOutline
{
NSMutableArray *iChildren;
}
@end
@implementation MyPDFOutline
- (PDFOutline *) childAtIndex: (int) index
{
if (iChildren == NULL)
{
int count;
int i;
iChildren = [[NSMutableArray] alloc] initWithCapacity: 3];
count = [self numberOfChildren];
for (i = 0; i < count; i++)
[iChildren addObject: [super childAtIndex: i];
}
return [iChildren objectAtIndex: index];
}
- (void) dealloc
{
[iChildren release];
[super dealloc];
}
john calhoun—_______________________________________________
Cocoa-dev mailing list (email@hidden)
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