help icon indicating copy to clipboard operation
help copied to clipboard

Does i need to free send buffers? When?

Open alex-eri opened this issue 5 years ago • 5 comments

Buffer is allocated using malloc. Then i call uv_udp_send.

I see memcpy https://github.com/libuv/libuv/blob/e7ebae26247d2fee0a04547eb7f9aa8f78d4a642/src/unix/udp.c#L695

so i think its ok to free(buf->base) after uv_udp_send.

in tcp example https://github.com/libuv/libuv/blob/e7ebae26247d2fee0a04547eb7f9aa8f78d4a642/docs/code/tcp-echo-server/main.c#L36 buf free on send callback. Its on original buffer or on cloned?

alex-eri avatar May 05 '20 16:05 alex-eri

I suppose the documentation could be clearer about that: free it in your uv_udp_send_cb callback.

bnoordhuis avatar May 05 '20 19:05 bnoordhuis

Its on original buffer or on cloned?

Libuv takes ownership of the buffer (the pointed-to memory) but makes copies of the list of uv_buf_t structs that you pass it.

bnoordhuis avatar May 05 '20 19:05 bnoordhuis

It looks to me that in the Windows implementation the copying of the uv_but_t is absent, so if the operation is overlapped the caller must be responsible for keeping the uv_buf_t around. Am I correct?

mkravchik avatar Apr 25 '21 11:04 mkravchik

Libuv doesn't have to copy on Windows because WSASendTo() already does.

bnoordhuis avatar Apr 26 '21 08:04 bnoordhuis

Thank you, it was not obvious from the MS documentation, but now I see it there:

If this function is completed in an overlapped manner, it is the Winsock service provider's responsibility to capture the WSABUF structures before returning from this call. This enables applications to build stack-based WSABUF arrays pointed to by the lpBuffers parameter.

mkravchik avatar Apr 26 '21 11:04 mkravchik