Skip to content

Commit d6a067b

Browse files
committed
Make PWMAudioOut object ll, have finaliser
.. for reasons stated in the comment
1 parent b1d9331 commit d6a067b

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

shared-bindings/audiopwmio/PWMAudioOut.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,12 @@ STATIC mp_obj_t audiopwmio_pwmaudioout_make_new(const mp_obj_type_t *type, size_
114114
validate_obj_is_free_pin_or_none(args[ARG_right_channel].u_obj, MP_QSTR_right_channel);
115115

116116
// create AudioOut object from the given pin
117-
audiopwmio_pwmaudioout_obj_t *self = m_new_obj(audiopwmio_pwmaudioout_obj_t);
117+
// The object is made long-lived because many implementations keep
118+
// a pointer to the object (e.g., for the interrupt handler), which
119+
// will not work properly if the object is moved. It is created
120+
// with a finaliser as some ports use these (rather than 'reset' functions)
121+
// to ensure resources are collected at interpreter shutdown.
122+
audiopwmio_pwmaudioout_obj_t *self = m_new_ll_obj_with_finaliser(audiopwmio_pwmaudioout_obj_t);
118123
self->base.type = &audiopwmio_pwmaudioout_type;
119124
common_hal_audiopwmio_pwmaudioout_construct(self, left_channel_pin, right_channel_pin, args[ARG_quiescent_value].u_int);
120125

@@ -249,6 +254,7 @@ MP_PROPERTY_GETTER(audiopwmio_pwmaudioout_paused_obj,
249254
STATIC const mp_rom_map_elem_t audiopwmio_pwmaudioout_locals_dict_table[] = {
250255
// Methods
251256
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&audiopwmio_pwmaudioout_deinit_obj) },
257+
{ MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&audiopwmio_pwmaudioout_deinit_obj) },
252258
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) },
253259
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&audiopwmio_pwmaudioout___exit___obj) },
254260
{ MP_ROM_QSTR(MP_QSTR_play), MP_ROM_PTR(&audiopwmio_pwmaudioout_play_obj) },

0 commit comments

Comments
 (0)