Skip to content

Commit

Permalink
avoid memory leak in uvx_client_send(), when xclient is not connected…
Browse files Browse the repository at this point in the history
… to a server.
  • Loading branch information
liigo committed Jan 16, 2019
1 parent 03a7ebc commit d33b2bd
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
1 change: 1 addition & 0 deletions uvx.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ int uvx_client_connect(uvx_client_t* xclient, uv_loop_t* loop, const char* ip, i
// send data to the connected tcp server (not only xserver).
// don't use `data` any more, it will be `free`ed later.
// please make sure that `data` was `malloc`ed before, so that it can be `free`ed correctly.
// if no server is connected, free data immediately, to avoid memory leak.
// returns 1 on success, or 0 if fails.
int uvx_client_send(uvx_client_t* xclient, void* data, unsigned int size);

Expand Down
6 changes: 4 additions & 2 deletions uvx_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,12 @@ int uvx_client_connect(uvx_client_t* xclient, uv_loop_t* loop, const char* ip, i
}

int uvx_client_send(uvx_client_t* xclient, void* data, unsigned int size) {
if (xclient->uvserver)
if (xclient->uvserver) {
return uvx_send_to_stream((uv_stream_t*)xclient->uvserver, data, size);
else
} else {
free(data);
return 0;
}
}

static void _uv_on_connect(uv_connect_t* conn, int status);
Expand Down

0 comments on commit d33b2bd

Please sign in to comment.