Replace wxNSTimerData -setTimer method with -initWithWxTimer: initializer
authorDavid Elliott <dfe@tgwbd.org>
Tue, 18 Oct 2005 15:27:48 +0000 (15:27 +0000)
committerDavid Elliott <dfe@tgwbd.org>
Tue, 18 Oct 2005 15:27:48 +0000 (15:27 +0000)
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

index 9cef73ce20a71d025a767ae2ea04a8aed6a270a5..8f112e17e748db208c39ff042bb5a9aa981c26e0 100644 (file)
@@ -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;
     }