Re: Could somebody please fix NSTimer?
Re: Could somebody please fix NSTimer?
- Subject: Re: Could somebody please fix NSTimer?
- From: Gordon Apple <email@hidden>
- Date: Sun, 13 Jan 2013 17:44:17 -0600
- Thread-topic: Could somebody please fix NSTimer?
It may be ancient but, IMHO, it should be fixed, at least for ARC. It is a
major source of unnecessary retain cycles and the fix is simple. I went
ahead and created a simple wrapper for NSTimer, as I originally described,
and it works perfectly. I am going to file an enhancement request.
You¹re right. I have a strong C++ background which does color my thinking
about how things should work. In my main window controllers, I use ³wakeup²
calls to other controllers, rather then using ³awakeFromNib², because the
latter is unpredictable as to calling order. Maybe I should similarly add
³shutDown² calls so I can finalize and release contained controllers in a
prescribed order. I might even add a protocol with those two methods.
On 1/12/13 2:33 PM, "Kyle Sluder" <email@hidden> wrote:
> On Jan 12, 2013, at 12:16 PM, Gordon Apple <email@hidden> wrote:
>
>> Re: Could somebody please fix NSTimer? And just how does one go about doing
>> that? The only way I can see is to use an owner¹s dealloc to call something
>> like ³wouldLikeToDealloc² on the owned object.
>
> Retain/release is just an implementation of a pattern. You can implement it
> yourself, or use one of the other existing implementations the framework
> provides for you: -viewDidMoveToSuperview, -windowWillClose, etc.
>
>> From the literature, it appears that NSTimer is the main offender in this
>> regard.
>
> NSTimer is something like twenty years old, but it is also decently
> documented. The run loop maintains a strong reference to the timer. The timer
> maintains a strong reference to its target. The chain is broken when the timer
> is invalidated.
>
>> I frequently use generic NSViewControllers and NSWindowControllers which have
>> no link to the controller containing the timer.
>
> The good thing about writing application code, rather than framework code, is
> that you can fix this problem and your solution can be both specific and
> correct.
>
> Meanwhile, you have no guarantee that some random piece of framework code
> hasn't retained your window controller or view controller, and now all of a
> sudden the timer you installed elsewhere is still firing at your view
> controllers but the model objects have been deallocated
>
> This isn't C++. RAII is not a valid pattern in Cocoa. Do not use object
> lifecycle to manage interactions between app components. Instead, make the
> targets of your timers aware enough of their use that they can signal for the
> timer to be invalidated at the appropriate time.
>
> --Kyle Sluder
>
_______________________________________________
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