Re: Releasing properties?
Re: Releasing properties?
- Subject: Re: Releasing properties?
- From: Keary Suska <email@hidden>
- Date: Sat, 27 Aug 2016 08:31:11 -0600
> On Aug 27, 2016, at 8:22 AM, じょいすじょん <email@hidden> wrote:
>
>
>> On 2016 Aug 27, at 23:09, Keary Suska <email@hidden> wrote:
>>
>>>
>>> On Aug 27, 2016, at 5:22 AM, Andreas Falkenhahn <email@hidden> wrote:
>>>
>>> Consider the following example properties:
>>>
>>> NSApplication: @property(strong) NSMenu *mainMenu
>>> NSFont: @property(readonly, copy) NSString *familyName
>>> NSColorPanel: @property(copy) NSColor *color
>>>
>>> AFAIU I must not release the NSMenu/NSString/NSColor obtained from these
>>> properties because I don't own the objects returned by those properties and
>>> Apple's memory management policy docs clearly say:
>>>
>>> "You must not relinquish ownership of an object you do not own"
>>>
>>> So I think I've got that right but I'd be glad if somebody could just
>>> confirm that I got it right ;)
>>>
>>> I'm just a little confused because of the keywords "strong" and "copy" in
>>> the @property declarations but AFAIU these only refer to setting those
>>> properties, not getting those properties, i.e. the object is copied when
>>> *setting* the property, not when *getting* it. Right?
>>>
>>> And another question: Can it also happen that a class has a property
>>> which returns an object which I *must* release or is it a general rule
>>> that getting a @property always returns objects which I do not own
>>> and hence mustn't released?
>>
>> There are only specific method conventions that establish ownership: alloc+init and new. This is because ownership should only be granted when specifically requested by the caller. There is no good reason for an object to require that a caller own a returned object. Now, some API could decide to buck the convention and have a different object construction naming, but the point is the same. If an API does anything else, it is badly designed. Cocoa will never do this.
>>
>> One could even argue (and some have) that there is no good reason for *any* object method to return a retained object, as that forces the caller to explicitly require ownership. And that is key: ownership is a function of the caller, not the object.
>>
>> HTH,
>>
>> Keary Suska
>> Esoteritech, Inc.
>> "Demystifying technology for your home or business"
>>
>>
> Just to keep everyone on the rules, I'm going to post the docs links.
> It's too easy to quote it wrong. (not saying anybody did)
>
> Objective-C
>
> https://developer.apple.com/library/mac/documentation/General/Conceptual/DevPedia-CocoaCore/MemoryManagement.html
>
> https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/MemoryMgmt.html
>
> Core Foundation (because you WILL use it at some point)
>
> https://developer.apple.com/library/mac/documentation/CoreFoundation/Conceptual/CFMemoryMgmt/Concepts/Ownership.html
Doh! Forgot “copy”. Best to point to docs since they are clear and succinct.
Keary Suska
Esoteritech, Inc.
"Demystifying technology for your home or business"
_______________________________________________
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