From 2ed42e313a81894f344234dd912784a420ebea13 Mon Sep 17 00:00:00 2001 From: weiph1029 Date: Thu, 17 Oct 2024 13:46:22 +0800 Subject: [PATCH] fix memory leak --- examples/generic/main.c | 2 +- src/buffer.c | 1 + src/peer_connection.c | 2 ++ src/sctp.c | 23 +++++++++++++---------- src/sctp.h | 2 ++ src/stun.c | 3 ++- 6 files changed, 21 insertions(+), 12 deletions(-) diff --git a/examples/generic/main.c b/examples/generic/main.c index a49d57e..eae1fc8 100644 --- a/examples/generic/main.c +++ b/examples/generic/main.c @@ -24,7 +24,7 @@ static void onclose(void* user_data) { } static void onmessage(char* msg, size_t len, void* user_data, uint16_t sid) { - printf("on message: %d %s", sid, msg); + printf("on message: %d %.*s", sid, (int)len, msg); if (strncmp(msg, "ping", 4) == 0) { printf(", send pong\n"); diff --git a/src/buffer.c b/src/buffer.c index ac3a47c..5d24090 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -27,6 +27,7 @@ void buffer_free(Buffer* rb) { if (rb) { free(rb->data); rb->data = NULL; + free(rb); rb = NULL; } } diff --git a/src/peer_connection.c b/src/peer_connection.c index 1c1289c..26920d4 100644 --- a/src/peer_connection.c +++ b/src/peer_connection.c @@ -208,6 +208,8 @@ PeerConnection* peer_connection_create(PeerConfiguration* config) { void peer_connection_destroy(PeerConnection* pc) { if (pc) { + sctp_destroy_socket(&pc->sctp); + dtls_srtp_deinit(&pc->dtls_srtp); agent_destroy(&pc->agent); buffer_free(pc->data_rb); buffer_free(pc->audio_rb); diff --git a/src/sctp.c b/src/sctp.c index 301b4e5..42d68ae 100644 --- a/src/sctp.c +++ b/src/sctp.c @@ -444,7 +444,6 @@ static void sctp_process_notification(Sctp* sctp, union sctp_notification* notif default: break; } - free(notification); // we need to free the memory that usrsctp allocates } static int sctp_incoming_data_cb(struct socket* sock, union sctp_sockstore addr, void* data, size_t len, struct sctp_rcvinfo recv_info, int flags, void* userdata) { @@ -460,6 +459,7 @@ static int sctp_incoming_data_cb(struct socket* sock, union sctp_sockstore addr, } else { sctp_handle_incoming_data(sctp, data, len, ntohl(recv_info.rcv_ppid), recv_info.rcv_sid, flags); } + free(data); // we need to free the memory that usrsctp allocates return 0; } #endif @@ -561,9 +561,7 @@ int sctp_create_socket(Sctp* sctp, DtlsSrtp* dtls_srtp) { } while (0); if (ret < 0) { - usrsctp_shutdown(sctp->sock, SHUT_RDWR); - usrsctp_close(sctp->sock); - sctp->sock = NULL; + sctp_destroy_socket(sctp); return -1; } @@ -573,6 +571,16 @@ int sctp_create_socket(Sctp* sctp, DtlsSrtp* dtls_srtp) { return 0; } +void sctp_destroy_socket(Sctp* sctp) { +#if CONFIG_USE_USRSCTP + if (sctp && sctp->sock) { + usrsctp_shutdown(sctp->sock, SHUT_RDWR); + usrsctp_close(sctp->sock); + sctp->sock = NULL; + } +#endif +} + int sctp_is_connected(Sctp* sctp) { return sctp->connected; } @@ -580,12 +588,7 @@ int sctp_is_connected(Sctp* sctp) { void sctp_destroy(Sctp* sctp) { #if CONFIG_USE_USRSCTP if (sctp) { - if (sctp->sock) { - usrsctp_shutdown(sctp->sock, SHUT_RDWR); - usrsctp_close(sctp->sock); - sctp->sock = NULL; - } - + sctp_destroy_socket(sctp); free(sctp); sctp = NULL; } diff --git a/src/sctp.h b/src/sctp.h index 5956bba..2f2463e 100644 --- a/src/sctp.h +++ b/src/sctp.h @@ -161,6 +161,8 @@ void sctp_destroy(Sctp* sctp); int sctp_create_socket(Sctp* sctp, DtlsSrtp* dtls_srtp); +void sctp_destroy_socket(Sctp* sctp); + int sctp_is_connected(Sctp* sctp); void sctp_incoming_data(Sctp* sctp, char* buf, size_t len); diff --git a/src/stun.c b/src/stun.c index 38f748e..d32f6cd 100644 --- a/src/stun.c +++ b/src/stun.c @@ -238,7 +238,8 @@ int stun_msg_write_attr(StunMessage* msg, StunAttrType type, uint16_t length, ch stun_attr->type = htons(type); stun_attr->length = htons(length); - memcpy(stun_attr->value, value, length); + if (value) + memcpy(stun_attr->value, value, length); length = 4 * ((length + 3) / 4); header->length = htons(ntohs(header->length) + sizeof(StunAttribute) + length);