Skip to content

Commit

Permalink
Merge pull request #157 from weiph1029/main
Browse files Browse the repository at this point in the history
fix memory leak
  • Loading branch information
sepfy authored Oct 18, 2024
2 parents 656bfa9 + 2ed42e3 commit c33e1f6
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 12 deletions.
2 changes: 1 addition & 1 deletion examples/generic/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
1 change: 1 addition & 0 deletions src/buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ void buffer_free(Buffer* rb) {
if (rb) {
free(rb->data);
rb->data = NULL;
free(rb);
rb = NULL;
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/peer_connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
23 changes: 13 additions & 10 deletions src/sctp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand Down Expand Up @@ -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;
}

Expand All @@ -573,19 +571,24 @@ 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;
}

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;
}
Expand Down
2 changes: 2 additions & 0 deletions src/sctp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion src/stun.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit c33e1f6

Please sign in to comment.