Micropython: Various unexpeced errors, if initialized timer is not referenced by pythoncode

Created on 3 Feb 2019  路  3Comments  路  Source: micropython/micropython

When Timer is initialized, but instance is no longer referenced from python code, it's probably subject for GC.
Unfortunatelly after GC there are unexpected errors, when timer should fire.
Most probably underlaying RTOS or c++ code is still trying to reference memory, which was used by python Timer object, which is now gone.

initial topic: https://forum.micropython.org/viewtopic.php?f=2&t=5898&p=33812#p33812

Most helpful comment

It can be fixed in the following way:

~
from machine_for_forgetful import Timer
...
~

machine_for_forgetful.py:
~~~
import machine

things_to_remember = []

def Timer(args, *kwargs):
t = machine.Timer(args, *kwargs)
things_to_remember.append(t)
return t
~~~

All 3 comments

So, be sure to keep references to them.

yup, that's only way, whichi found working so far.
Perhaps it should be documented.
Better, if can be fixed. Either object prevented to be GCed, or necessary data copied elsewhere, where it's not subject for GC.

It can be fixed in the following way:

~
from machine_for_forgetful import Timer
...
~

machine_for_forgetful.py:
~~~
import machine

things_to_remember = []

def Timer(args, *kwargs):
t = machine.Timer(args, *kwargs)
things_to_remember.append(t)
return t
~~~

Was this page helpful?
0 / 5 - 0 ratings

Related issues

adritium picture adritium  路  3Comments

dpgeorge picture dpgeorge  路  3Comments

rolandvs picture rolandvs  路  3Comments

francescofact picture francescofact  路  3Comments

johnthagen picture johnthagen  路  5Comments