Re: Leaking CGColor objects
Re: Leaking CGColor objects
- Subject: Re: Leaking CGColor objects
- From: "Clark Cox" <email@hidden>
- Date: Sun, 26 Oct 2008 09:23:42 -0700
On Sun, Oct 26, 2008 at 7:25 AM, DKJ <email@hidden> wrote:
> I've got this in the init method of MyCALayer:
>
> self.foregroundColor =
> CGColorCreateGenericRGB( 1.0, 1.0, 0.9, 1.0 );
Here's your problem. CoreFoundation-style functions, of the form
<type>Create... (like CGColorCreateGenericRGB), return an object with
a reference count of 1 (just like [[<type alloc] init] in
Objective-C). You're responsible for balancing that retain count.
If you are running under garbage collection, you can do this:
self.foregroundColor = CFMakeCollectable(CGColorCreateGenericRGB(
1.0, 1.0, 0.9, 1.0 ));
If you're not running under garbage collection, you can do this:
CGColorRef temp = CGColorCreateGenericRGB( 1.0, 1.0, 0.9, 1.0 );
self.foregroundColor = temp;
CFRelease(temp);
> So I've got some questions:
>
> 1. Is there such a thing as a CGColor class? I don't see it in the
> documentation; but "CGColor" is what Instruments lists as the leaked
> objects.
Most (if not all) CoreFoundation-style objects are implicitly
Objective-C objects as well. So, while you will not find an @interface
declaration for CGColor, it exists at runtime. That said, don't rely
on the specific name of this class, as it is an implementation detail.
> 2. Are the CGColor objects being leaked when I remove MyCALayer objects from
> myArray? Should I set foregroundColor to nil when doing this?
No. As long as MyCALayer's dealloc is properly implemented (to release
foregroundColor, or set it to nil), you should never need to
explicitly do so in code that uses MyCALayer.
> 3. Are sublayers retained when added to a superlayer? I'm assuming so, since
> they're kept in an array. But then they should be released by
> removeFromSuperView, so that shouldn't cause a leak.
Correct.
--
Clark S. Cox III
email@hidden
_______________________________________________
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