leafo / lua-enet

Bindings to ENet for Lua
http://leafo.net/lua-enet/
85 stars 25 forks source link

Fix memory leak in peer_send if sending to non-"connected" peer #19

Open pfirsich opened 6 years ago

pfirsich commented 6 years ago

If the peer being sent to is not in the "connected" state, enet_peer_send will return -1 and the packet created by read_packet will not be freed.

I stumbled into this bug while I (foolishly) wrote this loop:

for peerIndex = 1, host:peer_count() do
    local peer = host:get_peer(peerIndex)
    if peer ~= event.peer then
        peer:send(event.data, channel, flag)
    end
end

In that way I find the documentation on host:get_peer a little bit confusing: "Returns the connected peer at the specified index", because get_peer may very well return non-"connected" peers. Should the documentation be adjusted as well in that regard?

pfirsich commented 4 years ago

Looking at this a long time after, I notice I did some other dumb stuff in my fork by accident, because I have forgotten this PR. Obviously it should only be this commit: https://github.com/leafo/lua-enet/pull/19/commits/67f30730efd882d013ed4786e3f9970a4f66b06c I'm not interested in cleaning that up, but noticed it just now.