@@ -309,7 +309,7 @@ static void uart_driver_do_read(Context *ctx, GenMessage gen_message)
309309 int local_pid = term_to_local_process_id (pid );
310310
311311 if (uart_data -> reader_process_pid != term_invalid_term ()) {
312- if (UNLIKELY (memory_ensure_free (ctx , TUPLE_SIZE (2 ) * 2 + REF_SIZE ) != MEMORY_GC_OK )) {
312+ if (UNLIKELY (memory_ensure_free_with_roots (ctx , TUPLE_SIZE (2 ) * 2 , 1 , & ref , MEMORY_CAN_SHRINK ) != MEMORY_GC_OK )) {
313313 ESP_LOGE (TAG , "[uart_driver_do_read] Failed to allocate space for error tuple" );
314314 globalcontext_send_message (glb , local_pid , MEMORY_ATOM );
315315 return ;
@@ -326,7 +326,7 @@ static void uart_driver_do_read(Context *ctx, GenMessage gen_message)
326326
327327 if (count > 0 ) {
328328 int bin_size = term_binary_heap_size (count );
329- if (UNLIKELY (memory_ensure_free (ctx , bin_size + TUPLE_SIZE (2 ) * 2 + REF_SIZE ) != MEMORY_GC_OK )) {
329+ if (UNLIKELY (memory_ensure_free_with_roots (ctx , bin_size + TUPLE_SIZE (2 ) * 2 , 1 , & ref , MEMORY_CAN_SHRINK ) != MEMORY_GC_OK )) {
330330 ESP_LOGE (TAG , "[uart_driver_do_read] Failed to allocate space for return value" );
331331 globalcontext_send_message (glb , local_pid , MEMORY_ATOM );
332332 }
@@ -387,7 +387,7 @@ static void uart_driver_do_write(Context *ctx, GenMessage gen_message)
387387 free (buffer );
388388
389389 int local_pid = term_to_local_process_id (pid );
390- if (UNLIKELY (memory_ensure_free (ctx , TUPLE_SIZE (2 ) + REF_SIZE ) != MEMORY_GC_OK )) {
390+ if (UNLIKELY (memory_ensure_free_with_roots (ctx , TUPLE_SIZE (2 ), 1 , & ref , MEMORY_CAN_SHRINK ) != MEMORY_GC_OK )) {
391391 ESP_LOGE (TAG , "[uart_driver_do_write] Failed to allocate space for return value" );
392392 globalcontext_send_message (glb , local_pid , MEMORY_ATOM );
393393 }
@@ -406,7 +406,7 @@ static void uart_driver_do_close(Context *ctx, GenMessage gen_message)
406406
407407 sys_unregister_listener (glb , & uart_data -> listener );
408408
409- if (UNLIKELY (memory_ensure_free (ctx , TUPLE_SIZE (2 ) + REF_SIZE ) != MEMORY_GC_OK )) {
409+ if (UNLIKELY (memory_ensure_free_with_roots (ctx , TUPLE_SIZE (2 ), 1 , & ref , MEMORY_CAN_SHRINK ) != MEMORY_GC_OK )) {
410410 ESP_LOGE (TAG , "[uart_driver_do_close] Failed to allocate space for return value" );
411411 globalcontext_send_message (glb , local_pid , MEMORY_ATOM );
412412 }
@@ -419,6 +419,7 @@ static void uart_driver_do_close(Context *ctx, GenMessage gen_message)
419419 }
420420
421421 free (uart_data );
422+ ctx -> platform_data = NULL ;
422423}
423424
424425static NativeHandlerResult uart_driver_consume_mailbox (Context * ctx )
0 commit comments