From 2b888aa2f346b52b16fbefbea6a2d9d8085d8ff1 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 16 Mar 2021 12:40:16 +1100 Subject: [PATCH] extmod/modbluetooth: Free temp arrays in gatts register services. This helps to reduce memory fragmentation, by freeing the heap data as soon as it is not needed. It also helps the compiler keeps a reference to the beginning of both arrays, which need to be traceable by the GC (otherwise some compilers may optimise this reference to something else). Signed-off-by: Damien George --- extmod/modbluetooth.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/extmod/modbluetooth.c b/extmod/modbluetooth.c index 269493f0a9..e379a8c6a3 100644 --- a/extmod/modbluetooth.c +++ b/extmod/modbluetooth.c @@ -619,6 +619,11 @@ STATIC mp_obj_t bluetooth_ble_gatts_register_services(mp_obj_t self_in, mp_obj_t } result->items[i] = MP_OBJ_FROM_PTR(service_handles); } + + // Free temporary arrays. + m_del(uint16_t *, handles, len); + m_del(size_t, num_handles, len); + return MP_OBJ_FROM_PTR(result); } STATIC MP_DEFINE_CONST_FUN_OBJ_2(bluetooth_ble_gatts_register_services_obj, bluetooth_ble_gatts_register_services);