Freeing ENetPacket

Adam D. Ruppe via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Nov 28 17:57:23 PST 2015


On Sunday, 29 November 2015 at 01:30:14 UTC, Kyle wrote:
> void sendUbytes(ENetPeer* dest, ref ubyte[] data)
> {
>     //create packet
>     ENetPacket* packet = enet_packet_create(cast(ubyte*)data, 
> data.length * ubyte.sizeof, ENET_PACKET_FLAG_RELIABLE);

So I'm not familiar with this library, but a note on D: it is 
usually a mistake to pass "ref something[]". The ref is 
unnecessary.

I would also recommend against using `cast(ubyte*)data`. Instead, 
use `data.ptr`.

>     //send packet to peer over channel id 0
>     enet_peer_send(dest, 0, packet);
>
>     //destroy packet
>     enet_packet_destroy(packet);
> }
>
> If I comment out the last part (enet_packet_destroy()) the 
> program eventually consumes all my RAM. If I do not, I get 
> occasional segfaults. Please help. Thanks.


The tutorial says that after you send it, you should not destroy 
it - the library will do that for you.

http://enet.bespin.org/Tutorial.html#SendingPacket

"Once the packet is handed over to ENet with enet_peer_send(), 
ENet will handle its deallocation and enet_packet_destroy() 
should not be used upon it."


So you definitely don't want to destroy.

Are you sure the memory leak is in here? Maybe it is crashing 
before you can see it use all the memory with this line there.



Are you compiling it as a 64 bit or a 32 bit program?


More information about the Digitalmars-d-learn mailing list