From 06c2bab0852eea898770b124bed58f024f9b7cb0 Mon Sep 17 00:00:00 2001 From: David Elliott Date: Tue, 18 Oct 2005 15:27:48 +0000 Subject: [PATCH] Replace wxNSTimerData -setTimer method with -initWithWxTimer: initializer which correctly calls the superclass (NSObject) init. Add an -init method for correctness; technically not needed as it is never called but it's an Objective-C best practice. Release the wxNSTimerData instance just after passing it to the NSTimer factory method. NSTimer retains it and we don't keep a pointer to it so we should not keep a refence to it. This fixes the bug in wxTimer::Stop where the program crashes in the NSCFTimer userInfo method because the NSTimer has already been invalidated. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@35941 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- src/cocoa/timer.mm | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/src/cocoa/timer.mm b/src/cocoa/timer.mm index 9cef73ce20..8f112e17e7 100644 --- a/src/cocoa/timer.mm +++ b/src/cocoa/timer.mm @@ -2,7 +2,7 @@ // Name: src/cocoa/timer.mm // Purpose: wxTimer for wxCocoa // Author: Ryan Norton -// Modified by: +// Modified by: David Elliott // Created: 2005-02-04 // RCS-ID: $Id$ // Copyright: (c) Ryan Norton @@ -54,16 +54,28 @@ IMPLEMENT_CLASS(wxTimer, wxTimerBase) wxTimer* m_timer; } -- (id)setTimer:(wxTimer*)theTimer; +- (id)init; +- (id)initWithWxTimer:(wxTimer*)theTimer; - (wxTimer*)timer; @end // interface wxNSTimerData : NSObject @implementation wxNSTimerData : NSObject -- (id)setTimer:(wxTimer*)theTimer; +- (id)init { + if(!(self = [super init])) + return nil; + m_timer = NULL; + return self; +} + +- (id)initWithWxTimer:(wxTimer*)theTimer; +{ + if(!(self = [super init])) + return nil; m_timer = theTimer; return self; } + - (wxTimer*)timer { return m_timer; @@ -100,12 +112,14 @@ bool wxTimer::Start(int millisecs, bool oneShot) wxAutoNSAutoreleasePool thePool; + wxNSTimerData *userInfo = [[wxNSTimerData alloc] initWithWxTimer:this]; m_cocoaNSTimer = [[NSTimer scheduledTimerWithTimeInterval: millisecs / 1000.0 //seconds target: wxTimer::sm_cocoaDelegate selector: @selector(onNotify:) - userInfo: [[wxNSTimerData alloc] setTimer:this] + userInfo: userInfo repeats: oneShot == false] retain]; + [userInfo release]; return IsRunning(); } @@ -114,9 +128,8 @@ void wxTimer::Stop() { if (m_cocoaNSTimer) { - NSObject* theUserInfo = [m_cocoaNSTimer userInfo]; + // FIXME: Is this safe to do if !isValid ? [m_cocoaNSTimer invalidate]; - [theUserInfo release]; [m_cocoaNSTimer release]; m_cocoaNSTimer = NULL; } -- 2.45.2