Re: Memory Leaks in CocoaEcho Sample
Re: Memory Leaks in CocoaEcho Sample
- Subject: Re: Memory Leaks in CocoaEcho Sample
- From: Marco Masser <email@hidden>
- Date: Wed, 22 Oct 2008 23:09:31 +0200
Sorry about any confusion that I may have caused! The color I got from
the NSGradient's -getColor:location:atIndex: actually was
autoreleased, but the NSGradient was not. Therefore, the color always
had an retain count of 1.
Sorry about that.
If you are trying to find a leak, then use one of the tools on the
system designed for exactly that -- leaks, Object Graph in
Instruments for GC, Object Alloc for non-GC, etc...
I didn't try to find a leak with this NSGradient method, I tried to
find a rule of thumb for the memory management for objects created and
returned by reference. Apart from NSNetService's -
getInputStream:outputStream: and NSGradient's -
getColor:location:atIndex:, I couldn't find such a case (there are
plenty of methods returning primitives or C-structs by reference,
though).
Now that I think of it, any method using an NSError** might be worth
looking into: In this case, the basic memory management rules apply.
The Error Handling Programming Guide is clear about this:
[...] you should send autorelease to [the NSError object] before you
return it to the caller.
http://developer.apple.com/documentation/Cocoa/Conceptual/ErrorHandlingCocoa/CreateCustomizeNSError/chapter_4_section_4.html
Now... in this particular case...
How can you look at the retain count of an object after the
autorelease pool was drained without it being retained by
something? Didn't you have to retain it to keep the NSColor iVar
around?
I just made an NSColor ivar and retained it after calling -
getColor:location:atIndex: and took a look on its retain count in a
second method that was called after the NSAutoreleasePool had been
drained (i.e. I took a look at the retain count after pressing a
button). That way, all -autorelease messages must have been dealt with
and what remains are the "real" retains.
_______________________________________________
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