Need a clarification on tcp-echo-server example #4363
Answered
by
bnoordhuis
RealYukiSan
asked this question in
Q&A
-
I'm sorry if this question sounds like a dumb question, but I'll ask it anyway. Why isn't the req variable, which is allocated dynamically using malloc, freed within the Will this function be safe from memory leaks? Is it acceptable to not free variables allocated by malloc? here's the snippet code that I'm talking about: void echo_read(uv_stream_t *client, ssize_t nread, const uv_buf_t *buf) {
if (nread > 0) {
write_req_t *req = (write_req_t*) malloc(sizeof(write_req_t));
req->buf = uv_buf_init(buf->base, nread);
uv_write((uv_write_t*) req, client, &req->buf, 1, echo_write);
return;
}
if (nread < 0) {
if (nread != UV_EOF)
fprintf(stderr, "Read error %s\n", uv_err_name(nread));
uv_close((uv_handle_t*) client, on_close);
}
free(buf->base);
} |
Beta Was this translation helpful? Give feedback.
Answered by
bnoordhuis
Mar 16, 2024
Replies: 1 comment 1 reply
-
It's freed in echo_write. The uv_write_t needs to persist until that function is called. |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
RealYukiSan
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's freed in echo_write. The uv_write_t needs to persist until that function is called.