MICROPY_PORT_DEINIT_FUNC called after gc_sweep_all
I'm using MICROPY_PORT_DEINIT_FUNC to release hardware resources upon soft reset.
Unfortunately, it's called only after gc ram is deallocated by gc_sweep_all.
My port uses gc allocated ram, and when MICROPY_PORT_DEINIT_FUNC is called it's too late, gc ram was already deallocated.
https://github.com/micropython/micropython/blob/10709846f38f8f6519dee27694ce583926a00cb9/ports/esp32/main.c#L145-L153
Is there any particular reason gc_sweep_all is called before mp_deinit?
Is it reasonable to move gc_sweep_all after mp_deinit?
esp32/main: Fix mp_deinit order.
Move mp_deinit to be called before gc_sweep_all to prevent mp_deinit from double freeing memory if MICROPY_PORT_DEINIT_FUNC is defined and the function tries to free memory already freed by gc_sweep_all
Summary
Prevent a core panic on soft-reset caused by "double freeing" memory.
Testing
Tested with esp32 and https://github.com/lvgl/lv_binding_micropython/pull/383
Trade-offs and Alternatives
Probably this PR https://github.com/micropython/micropython/pull/16063#