From 4487f614042c15fd682eb326e3f9dc00d6332ab6 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Wed, 18 May 2022 11:37:13 -0400 Subject: [PATCH] Restore automatic gc_collect() after an import --- locale/circuitpython.pot | 4 ++++ py/builtinimport.c | 4 ++++ shared-module/rgbmatrix/RGBMatrix.c | 2 +- 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/locale/circuitpython.pot b/locale/circuitpython.pot index 9f4bf97a73..acf7e24b9f 100644 --- a/locale/circuitpython.pot +++ b/locale/circuitpython.pot @@ -1022,6 +1022,10 @@ msgstr "" msgid "Failed to acquire mutex, err 0x%04x" msgstr "" +#: shared-module/rgbmatrix/RGBMatrix.c +msgid "Failed to allocate %q buffer" +msgstr "" + #: ports/mimxrt10xx/common-hal/busio/UART.c ports/nrf/common-hal/busio/UART.c #: ports/raspberrypi/common-hal/busio/UART.c msgid "Failed to allocate RX buffer" diff --git a/py/builtinimport.c b/py/builtinimport.c index dbacc3d657..10539d01a4 100644 --- a/py/builtinimport.c +++ b/py/builtinimport.c @@ -455,6 +455,10 @@ STATIC mp_obj_t process_import_at_level(qstr full_mod_name, qstr level_mod_name, // not a package. This will be caught on the next iteration // because the file will not exist. } + + // Loading a module thrashes the heap significantly so we explicitly clean up + // afterwards. + gc_collect(); } if (outer_module_obj != MP_OBJ_NULL && VERIFY_PTR(MP_OBJ_TO_PTR(outer_module_obj))) { diff --git a/shared-module/rgbmatrix/RGBMatrix.c b/shared-module/rgbmatrix/RGBMatrix.c index 0bfed0aa6c..c6dbfc02b0 100644 --- a/shared-module/rgbmatrix/RGBMatrix.c +++ b/shared-module/rgbmatrix/RGBMatrix.c @@ -123,7 +123,7 @@ void common_hal_rgbmatrix_rgbmatrix_reconstruct(rgbmatrix_rgbmatrix_obj_t *self, mp_raise_ValueError(translate("Invalid argument")); break; case PROTOMATTER_ERR_MALLOC: - mp_raise_msg(&mp_type_MemoryError, NULL); + mp_raise_msg_varg(&mp_type_MemoryError, translate("Failed to allocate %q buffer"), MP_QSTR_RGBMatrix); break; default: mp_raise_msg_varg(&mp_type_RuntimeError,