Hello Everyone,

I only have a few days experience with libuv, but done a lot of reading of 
uv.h, the libuv-dox, and other resources, and I've gotten both a udp client 
and udp echo server running with just libuv. I understand all the memory 
management is on the user, which is an area I'm pretty green in. I'm using 
the current version of the master branch.

Both programs are fairly simple: the client opens a socket for sending and 
recieving, and while loops calls to uv_udp_send hundreds of char * packets 
("packet 1," "packet 2," etc.), individually malloc'd, to the server. The 
server opens a socket for receiving, has a receive callback that prints the 
message, copies that into a newly allocated buffer, sends it back to the 
same addr it got it from, and frees() the received buf->base. The client 
receives the echoed message, prints it, and frees the received buffer. So 
far that's the strategy I've seen in all the examples. 

In both cases, that leaves the send buffers to be freed. I realized I 
couldn't free them immediately after calling uv_udp_send, since that just 
points to the buffer and doesn't send it right away. So I went to free it 
in the send callback, but unlike the receive and read cbs, you can't just 
free(buf->base). I tried free(req->bufs[0].base) similar to the write 
examples, but it appears to have already been freed and set to null in 
uv__udp_run_completed, before the callback is called. So that segfaults, 
but free(req->bufsml[0].base) works and gives no valgrind errors or leaks. 
I tried iterating over all non-null array entries in bufsml [ int i=0; 
while (req->bufsml[i].base) free(req->bufsml[i].base); ++i;  ] but that 
gives me one valgrind error per packet ("Conditional jump or move depends 
on uninitialised value(s)"). 

Am I doing this right?

I haven't been able to find any samples or examples of udp echo servers 
using libuv that even free memory in the send_cb other than free(req), but 
as far as I can tell, they all have memory leaks. The tcp server example in 
libuv-dox uses free(wr->buf.base) in its write_cb, but valgrind reports it 
has a huge memory leak (maybe it didn't 8 or 9 months ago with the version 
it was made for, but it does now). I don't know what the difference between 
bufs and bufsml is, and don't know if I should be concerned that I can't 
seem to check all members of the bufsml array to see if I should free them, 
only blindly free the first. It seems like this is harder than it should be.

-- 
You received this message because you are subscribed to the Google Groups 
"libuv" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/libuv.
For more options, visit https://groups.google.com/d/optout.

Reply via email to