Timer.Timer won't stop

i have problems with timers in 2 different apps, and cant seem to work out what is going wrong- 

App no.1

I create a view class and push it,

Within that view there is a variable 'maintimer' and in onShow() I do -

maintimer = new Timer.Timer();

maintimer.start(method(:onTimer), 1000, true);

to create a  1 second repeating timer. This works fine.

however when the view is popped the timer continues to run, I've tried calling maintimer.stop() before the view pops itself even tried maintimer.stop without the parenthesis, I've tried maintimer=null; and I've tried both together, but I can see in the debugger that the onTimer() function in the view is still being called every second even after its popped from the screen, I've tried assigning the view to null in the class which created it and yet it still exists and its onTimer function is still being called. When a new view is then created it causes some weird behaviour and crashes in the onTimer function.

In the second app it seems a similar thing is happening where a timer in a view is still extant after being stopped and the view popped as making a new instance of the view and push/popping it 3 times gives a "too many timers" error on the device itself, as if the timers aren't being disposed of despite being assigned null and the view class they were contained in being popped.

what do I need to get the timer to stop? and what do I need to do to get the variable freed up so I can use it with a new timer (or the view instance disposed of so I can create a new instance of it from scratch for that matter)?

thanks

  • Where do you have the newtimer variable initially defined?  I have found that you want to make sure it is defined at the class level, out side of any function.  That way you can put the code you have in onShow() and in onStop() you can include maintimer.stop.

  • I've possibly solved the first case, where the timer was being started in onShow(), it seems onShow() was called again before the view was popped because i was using a menu to confirm exit. I've moved the timer.start() to onLayout() and now it stops correctly when the view is popped.

    Still can't work out why the timer in the second app isn't released, is there any way in the simulator to see what references to a variable still exist to work out why it's not getting garage collected even when i say it to null?

  • I went back and looked at my timer implementation.  I have two timers implemented.  The first if part of the main view so the only time it gets removed is when the app is shut down.  The second is in a delegate for a second view.  It behaves as you want.  In both cases the timers are scoped at the class level. 

    Below is a sample of how it is configured.  The timer get setup when example1 is called, then get disabled and cleared when example2 get called.

    class test extendes WatchUi.View {
    
    var maintimer;
    
        function example1() {
            maintimer = new Timer.Timer();
            maintimer.start( method(:timercallback), 1000, true );
        }
    
        function example2() {
            maintimer.stop();
            maintimer = null;
        }
    
        function timercallback () {
            //do something here
        }
    }

    I hope this helps.