@@ -529,6 +529,7 @@ static term nif_socket_open(Context *ctx, int argc, term argv[])
529529#ifndef AVM_NO_SMP
530530 rsrc_obj -> socket_lock = smp_rwlock_create ();
531531 if (IS_NULL_PTR (rsrc_obj -> socket_lock )) {
532+ // destroy resource object without calling dtor
532533 free (rsrc_obj );
533534 RAISE_ERROR (OUT_OF_MEMORY_ATOM );
534535 }
@@ -537,6 +538,7 @@ static term nif_socket_open(Context *ctx, int argc, term argv[])
537538 rsrc_obj -> fd = socket (domain , type , protocol );
538539 if (UNLIKELY (rsrc_obj -> fd == -1 || rsrc_obj -> fd == CLOSED_FD )) {
539540 AVM_LOGE (TAG , "Failed to initialize socket." );
541+ rsrc_obj -> fd = CLOSED_FD ;
540542 enif_release_resource (rsrc_obj );
541543 return make_errno_tuple (ctx );
542544 } else {
@@ -555,6 +557,7 @@ static term nif_socket_open(Context *ctx, int argc, term argv[])
555557 LWIP_END ();
556558 rsrc_obj -> socket_state = SocketStateUDPIdle ;
557559 } else {
560+ rsrc_obj -> socket_state = SocketStateClosed ;
558561 enif_release_resource (rsrc_obj );
559562 RAISE_ERROR (BADARG_ATOM );
560563 }
@@ -586,7 +589,7 @@ static term nif_socket_open(Context *ctx, int argc, term argv[])
586589 RAISE_ERROR (OUT_OF_MEMORY_ATOM );
587590 }
588591 term obj = enif_make_resource (erl_nif_env_from_context (ctx ), rsrc_obj );
589- enif_release_resource (rsrc_obj );
592+ enif_release_resource (rsrc_obj ); // decrement refcount after enif_alloc_resource
590593
591594 size_t requested_size = TUPLE_SIZE (2 ) + TUPLE_SIZE (2 ) + REF_SIZE ;
592595 if (UNLIKELY (memory_ensure_free_with_roots (ctx , requested_size , 1 , & obj , MEMORY_CAN_SHRINK ) != MEMORY_GC_OK )) {
@@ -788,6 +791,7 @@ static struct SocketResource *make_accepted_socket_resource(struct tcp_pcb *newp
788791#ifndef AVM_NO_SMP
789792 conn_rsrc_obj -> socket_lock = smp_rwlock_create ();
790793 if (IS_NULL_PTR (conn_rsrc_obj -> socket_lock )) {
794+ // destroy resource without calling destructor
791795 free (conn_rsrc_obj );
792796 return NULL ;
793797 }
@@ -1563,7 +1567,7 @@ static term nif_socket_listen(Context *ctx, int argc, term argv[])
15631567static term make_accepted_socket_term (Context * ctx , struct SocketResource * conn_rsrc_obj )
15641568{
15651569 term obj = enif_make_resource (erl_nif_env_from_context (ctx ), conn_rsrc_obj );
1566- enif_release_resource (conn_rsrc_obj );
1570+ enif_release_resource (conn_rsrc_obj ); // decrement refcount after enif_allocate_resource in make_accepted_socket_resource
15671571
15681572 term socket_term = term_alloc_tuple (2 , & ctx -> heap );
15691573 uint64_t ref_ticks = globalcontext_get_ref_ticks (ctx -> global );
@@ -1636,7 +1640,7 @@ static term nif_socket_accept(Context *ctx, int argc, term argv[])
16361640 TRACE ("nif_socket_accept: Created socket on accept fd=%i\n" , rsrc_obj -> fd );
16371641
16381642 term new_resource = enif_make_resource (erl_nif_env_from_context (ctx ), conn_rsrc_obj );
1639- enif_release_resource (conn_rsrc_obj );
1643+ enif_release_resource (conn_rsrc_obj ); // decrement refcount after enif_alloc_resource
16401644
16411645 size_t requested_size = TUPLE_SIZE (2 ) + TUPLE_SIZE (2 ) + REF_SIZE ;
16421646 if (UNLIKELY (memory_ensure_free_with_roots (ctx , requested_size , 1 , & new_resource , MEMORY_CAN_SHRINK ) != MEMORY_GC_OK )) {
@@ -1673,6 +1677,7 @@ static term nif_socket_accept(Context *ctx, int argc, term argv[])
16731677 AVM_LOGW (TAG , "Failed to allocate memory: %s:%i." , __FILE__ , __LINE__ );
16741678 LWIP_END ();
16751679 SMP_RWLOCK_UNLOCK (rsrc_obj -> socket_lock );
1680+ enif_release_resource (new_resource );
16761681 RAISE_ERROR (OUT_OF_MEMORY_ATOM );
16771682 }
16781683
0 commit comments