← index #6773Issue #6088
Related · high · value 1.447
QUERY · ISSUE

uncaught exception in Timer(1) interrupt handler MemoryError:

openby Irvingaoopened 2021-01-15updated 2026-03-24
proposed-close

Hello, here is my problem:

import pyb
import ps2
import encoder
from pyb import LED, Timer

from machine import I2C,Pin
from oled import SSD1306_I2C

def mainTimer_cb(cb):
    '''
    #函数功能:用于总体程序的逻辑定时功能
    '''
    global count
    LED(3).toggle()
    count += 1
    print(count)
    if (count%8) == 0: 
        LED(2).toggle() 
    elif (count%20) == 0:
        count = 0
    elif (count%5):
        oled_draw(count)

def oled_draw(text):
    oled.text(str(text),20,0)

count = 0

mainTimer = Timer(1, freq=2, callback=mainTimer_cb) 

i2c = I2C(scl = Pin("X9"),sda = Pin("X10"),freq = 10000) 
oled = SSD1306_I2C(128, 64, i2c) #创建oled对象

oled.text("count:",0,0)
oled.text("i love u!",20,40)
oled.show()

while True:
    i = 0

when i ran this code in pyb, it always got the memory error. So i wanna ask how can i write my main code in callback function? Because i can do this by C in STM32, just so confused to the error.

CANDIDATE · ISSUE

Unable to catch MemoryError in ISR

closedby andrewleechopened 2020-05-28updated 2020-06-01

I'm not sure if this is expected behavior, or a bug?

>>> def locked():
...     try:
...         _ = bytearray(1)
...         print(False)
...         return
...     except MemoryError:
...         pass
...     print(True)

>>> locked()
False
>>> import micropython
>>>micropython.heap_lock();locked()
True

>>> from pyb import Timer
>>> tim = Timer(5)
>>> tim.init(freq=1, callback=locked)
>>> uncaught exception in Timer(5) interrupt handler
Traceback (most recent call last):
  File "logger/logger.py", line 33, in heap_locked
MemoryError: 

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied