From 5115fdaa6f651ae4e1236e5cf7194438fe1c0533 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Mon, 25 Nov 2019 09:53:55 -0600 Subject: [PATCH] shared-bindings: I2SOut: Ensure object is deinitialised (or deinitialized, for those of us on this side of the pond) Otherwise, a sequence like ``` audio = audiobusio.I2SOut(bit_clock=board.D6, word_select=board.D9, data=board.D10) sine_wave_sample = audiocore.RawSample(sine_wave) audio.play(sine_wave_sample, loop=True) del audio ``` could free the memory associated with audio without stopping the related background task. Later, when fresh objects are allocated within a now-freed memory region, they can get overwritten in the background task, leading to a hard crash. This presumably can affect multiple I2S implementations, but it was reported against the nRF one. --- shared-bindings/audiobusio/I2SOut.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/shared-bindings/audiobusio/I2SOut.c b/shared-bindings/audiobusio/I2SOut.c index 81383c7776..8f7382fde5 100644 --- a/shared-bindings/audiobusio/I2SOut.c +++ b/shared-bindings/audiobusio/I2SOut.c @@ -117,7 +117,7 @@ STATIC mp_obj_t audiobusio_i2sout_make_new(const mp_obj_type_t *type, size_t n_a assert_pin(data_obj, false); const mcu_pin_obj_t *data = MP_OBJ_TO_PTR(data_obj); - audiobusio_i2sout_obj_t *self = m_new_obj(audiobusio_i2sout_obj_t); + audiobusio_i2sout_obj_t *self = m_new_obj_with_finaliser(audiobusio_i2sout_obj_t); self->base.type = &audiobusio_i2sout_type; common_hal_audiobusio_i2sout_construct(self, bit_clock, word_select, data, args[ARG_left_justified].u_bool); @@ -268,6 +268,7 @@ const mp_obj_property_t audiobusio_i2sout_paused_obj = { STATIC const mp_rom_map_elem_t audiobusio_i2sout_locals_dict_table[] = { // Methods + { MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&audiobusio_i2sout_deinit_obj) }, { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&audiobusio_i2sout_deinit_obj) }, { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) }, { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&audiobusio_i2sout___exit___obj) },