Re: Questions regarding release
Re: Questions regarding release
- Subject: Re: Questions regarding release
- From: Wim Lewis via Cocoa-dev <email@hidden>
- Date: Sat, 28 Sep 2019 20:57:40 -0700
On Sep 2'6, 2019, at 9:47 AM, Gabr'iel Zachmann via Cocoa-dev
<email@hidden> wrote:
>>
>> I don’t believe that’s entirely true. “make” methods return a +0 value.
>>
>> The issue in the below code to my eye is that you allocate a path with
>> CGPathCreateWithRect (+1) but then don't release it.
>>
>
> In that case, I am wondering:
> doesn't ownership pass to the textlayer ?
> If yes, shouldn't textlayer release the path when it gets destroyed by the
> ARC?
No, the general rule for Cocoa and CoreFoundation retain-release (whether
manual or ARC) is that each piece of code manages its own retain-counts,
leaving it up to other code to perform its own retain (or copy, or anything
else) if that other code needs it. The exceptions are "create", "alloc/init",
which return to you an object with an "extra" retain count that you are
responsible for releasing. A very few other documented functions/methods have
nonstandard retain/release behavior as well. (And of course the retain and
release calls themselves.)
This is a bit different from the traditional C style where you always have to
know whether a given method "takes ownership" of an allocated object, or if it
keeps a reference (so you must understand the object's lifespan so you can free
it later). The equivalent of "taking ownership" would be "consuming a
reference", but unless you are micro-optimizing it's rarely necessary to
deviate from the normal, retain-count-neutral convention.
(One point of confusion might be the accessor syntax — if foo is an object,
then "foo.bar = 12" is actually compiled to "[foo setBar:12]"; if you want to
mess with an object's instance variables directly, you'd do "foo->bar = 12",
taking advantage of the fact that an object is just a pointer to a struct. It's
rarely a good idea to mess with an object's instance variables directly,
though!)
_______________________________________________
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