From: Brendan Hansen Date: Mon, 4 Apr 2022 23:48:46 +0000 (-0500) Subject: bugfix for double-freeing packets X-Git-Tag: v0.0.1~2 X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=27c896ce946092882f9665b1ec39f6a3d74c5094;p=onyx_net.git bugfix for double-freeing packets --- 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); }