diff --git a/python/port/mod/matplotlib/pyplot/modpyplot.cpp b/python/port/mod/matplotlib/pyplot/modpyplot.cpp index 8d8d86baf..c8e67f872 100644 --- a/python/port/mod/matplotlib/pyplot/modpyplot.cpp +++ b/python/port/mod/matplotlib/pyplot/modpyplot.cpp @@ -72,6 +72,12 @@ void modpyplot_gc_collect() { ); } +void modpyplot_flush_used_heap() { + if (sPlotStore) { + // Clean the store object + sPlotStore->flush(); + } +} mp_obj_t modpyplot_arrow(size_t n_args, const mp_obj_t *args) { assert(n_args == 4); assert(sPlotStore != nullptr); diff --git a/python/port/port.cpp b/python/port/port.cpp index 49a81856a..77c7b6c4a 100644 --- a/python/port/port.cpp +++ b/python/port/port.cpp @@ -82,6 +82,10 @@ void MicroPython::ExecutionEnvironment::runCode(const char * str) { mp_obj_print_helper(&mp_plat_print, (mp_obj_t)nlr.ret_val, PRINT_EXC); mp_print_str(&mp_plat_print, "\n"); /* End of mp_obj_print_exception. */ + + // Flush the store if an error is encountered to avoid being stuck with a full memory + modpyplot_flush_used_heap(); + // TODO: do the same for other modules? } // Disable the user interruption