From 905b63ad9ab4e357bd1fe5a52422ffb8beb8a89e Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Wed, 19 Jun 2024 13:12:56 +0200 Subject: [PATCH] Bugfix: premature deletion of socket causes hard-fault when opcua-client disconnects. When an incoming connection is accepted by TCPSocket::accept() a new TCPSocket is generated and allocated on the heap. At the same time a TCPSocket internal flag called "_factory_allocated" is set which causes the dynamically allocated memory to be deallocated when calling TCPSocket::close(). As a consequence there is no need to deallocate or close any TCPSocket object in "shutdown", as this is done when calling UA_close (= mbed_close). Calling TCPSocket::close already here would also cause a crash as the object would have been deallocated by the time the OPCUA stack would invoke UA_close. --- src/arch/posix/mbed_tcp.h | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/arch/posix/mbed_tcp.h b/src/arch/posix/mbed_tcp.h index a9c5f41..0416191 100644 --- a/src/arch/posix/mbed_tcp.h +++ b/src/arch/posix/mbed_tcp.h @@ -48,8 +48,22 @@ extern "C" char str[8]; return itoa(err, str, 10); } - inline void shutdown(UA_SOCKET s, uint8_t flag) { - delete (Socket*)s; + inline void shutdown(UA_SOCKET s, uint8_t flag) + { + /* There is nothing to do here. + * + * When an incoming connection is accepted by TCPSocket::accept() + * a new TCPSocket is generated and allocated on the heap. At the + * same time a TCPSocket internal flag called "_factory_allocated" + * is set which causes the dynamically allocated memory to be + * deallocated when calling TCPSocket::close(). + * + * As a consequence there is no need to deallocate or close any + * TCPSocket object in here, as this is done when calling UA_close + * (= mbed_close). Calling close already here would cause a crash + * as the object would have been deallocated by the time the + * OPCUA stack would invoke UA_close. + */ } }