Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Memory leak.




On Aug 30, 2005, at 9:59 AM, Brian O'Brien wrote:

- (NSArray *) readFiles:(NSArray *)fileNames
{    
    int n = [fileNames count];
    NSMutableArray *ret = [NSMutableArray arrayWithCapacity:n];
    for (int i=0; i < n; i++)
    {
        Image * img = [self readFile:[fileNames objectAtIndex:i]];
        if (img != nil)
            [ret addObject:img];
    }
    return ret;
}

If you are leaking Image objects (and I'm guessing you are), then the culprit is likely the readFile: method. If [self readFile:] is returning an Image* that needs to have -release called on it, then you should consider changing it to call -autorelease on the returned Image*.

This is a Cocoa convention. Unless a method is defined to explicitly transfer ownership, such as +alloc, or -copy, you should always ensure that a reference returned by a method is temporary. This is why getters are often written this way:

- (id)foo {
    return [[_foo retain] autorelease];
}

This keeps the _foo object reference temporarily live for the caller, regardless of what the owning object does with it. In your case, readFile: should probably return an autoreleased Image object.

This isn't really an objective-C question, but rather a Cocoa framework question.

- Patrick

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Objc-language mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/objc-language/email@hidden

This email sent to email@hidden

References: 
 >Memory leak. (From: "Brian O'Brien" <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.