Re: [OT] NSTimer +timerWithTimeInterval:
Re: [OT] NSTimer +timerWithTimeInterval:
- Subject: Re: [OT] NSTimer +timerWithTimeInterval:
- From: Martin Wierschin via Cocoa-dev <email@hidden>
- Date: Thu, 30 Apr 2020 08:35:59 -0700
Sandor is correct. Using "new" in a method name is not forbidden. That method
just has to return an object with a +1 retain count. The relevant docs are here:
https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmRules.html#//apple_ref/doc/uid/20000994-BAJHFBGH
<https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmRules.html#//apple_ref/doc/uid/20000994-BAJHFBGH>
The static analyzer in Xcode will detect any methods that fail to follow these
rules– at least, most of the time.
The "newData" selector name used for Carl's timer callback won't cause any
problems. That method doesn't return an object, so calling code can't leak or
overrelease any objects because the memory management conventions were
disregarded. The static analyzer ignores this method because there's no return
value to misuse.
Even if the timer's callback selector were expected to return an object with a
neutral retain count (eg: an autoreleased object), the timer implementation
wouldn't figure out that you'd incorrectly given it a selector whose name
includes "new". There's no runtime introspection that would change the behavior
of the timer; the naming conventions ensure properly balanced retains/releases
during compilation. At runtime the timer would happily call your method and
simply fail to release the return value. If you wrote your "new" method
properly to return a +1 retain count object you'd have a leak, not a crash or
change in timer behavior. If you wrote your "new" method incorrectly and
returned an autoreleased object there would be no leaks or runtime
implications, at least not from the timer's side of things.
All that said, the "newData" method name is best changed so as not to confuse
anyone who does know the correct naming conventions.
~Martin Wierschin
> On Apr 29, 2020, at 3:55 PM, Alex Zavatone via Cocoa-dev
> <email@hidden> wrote:
>
> Sandor, it’s somewhere in the naming guide for the Objectice-C fundamental
> docs. I could be confusing things though.
>
>> On Apr 29, 2020, at 5:27 PM, Sandor Szatmari <email@hidden>
>> wrote:
>>
>> Alex,
>>
>>> On Apr 29, 2020, at 17:12, Alex Zavatone via Cocoa-dev
>>> <email@hidden> wrote:
>>>
>>> Not sure about this, but in Objective-C, you’re not supposed to start
>>> methods with new.
>>
>> I’ve always operated under the premise that using a reserved prefix, such as
>> new, was not verboten. Rather, if one chose the prefix new one must ensure
>> that the method followed memory management conventions, and would return an
>> object with a +1 retain count. Am I mistaken about this?
>>
>> Sandor
>>
>>>
>>>>> On Apr 29, 2020, at 4:07 PM, Carl Hoefs via Cocoa-dev
>>>>> <email@hidden> wrote:
>>>>>>> NSTimer *newTimer = [NSTimer timerWithTimeInterval:1.0 // should be
>>>>>>> 1/sec
_______________________________________________
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