From 27c896ce946092882f9665b1ec39f6a3d74c5094 Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Mon, 4 Apr 2022 18:48:46 -0500 Subject: [PATCH] bugfix for double-freeing packets --- src/packet.onyx | 2 ++ src/peer.onyx | 15 +++++++++++---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/packet.onyx b/src/packet.onyx index a2a7e58..8b251fc 100644 --- a/src/packet.onyx +++ b/src/packet.onyx @@ -34,6 +34,8 @@ Packet :: struct { // The allocator used for the data. Only used if // free_data is set to true. data_allocator := context.allocator; + + reference_count := 0; } Acknowledgement :: struct { diff --git a/src/peer.onyx b/src/peer.onyx index 6a2b1c4..c6e5128 100644 --- a/src/peer.onyx +++ b/src/peer.onyx @@ -84,6 +84,7 @@ peer_queue_outgoing_command :: #match { out := new(Outgoing_Command); out.command = command; out.packet = packet; + if packet != null do packet.reference_count += 1; peer_setup_outgoing_command(peer, out); return out; @@ -291,11 +292,17 @@ peer_remove_sent_reliable_command :: (peer: ^Peer, seq_num: u16, channel: Channe } peer_free_outgoing_command :: (peer: ^Peer, command: ^Outgoing_Command) { - if command.packet.free_data { - raw_free(command.packet.data_allocator, command.packet.data.data); - } + if command.packet != null { + command.packet.reference_count -= 1; + if command.packet.reference_count <= 0 { + if command.packet.free_data { + raw_free(command.packet.data_allocator, command.packet.data.data); + } - if command.packet != null do cfree(command.packet); + cfree(command.packet); + } + } + cfree(command.command); cfree(command); } -- 2.25.1