c - Closing libUV Handles Correctly -


i'm trying find out how fix these memory leaks i'm getting while running program valgrind. leaks occur 2 allocations in nshell_client_main. i'm not sure how free them.

i've tried freeing them @ nshell_connect, it's causing libuv abort program. i've tried freeing them @ end of nshell_client_main, read/write errors when closing loop. know how i'm supposed close these handles? i've read this, got me started. but, seams out-dated because uv_ip4_addr has different prototype in latest version.

(nshell_main "entry" point)

#include "nport.h" #include "nshell-main.h"  void nshell_close(     uv_handle_t * term_handle ){ }  void nshell_connect(uv_connect_t * term_handle, int status){     uv_close((uv_handle_t *) term_handle, 0); }  nerror * nshell_client_main(nshell * n_shell, uv_loop_t * n_shell_loop){      int uv_error = 0;      nerror * n_error = 0;      uv_tcp_t * n_shell_socket = 0;     uv_connect_t * n_shell_connect = 0;      struct sockaddr_in dest_addr;      n_shell_socket = malloc(sizeof(uv_tcp_t));      if (!n_shell_socket){         // handle error     }      uv_error = uv_tcp_init(n_shell_loop, n_shell_socket);      if (uv_error){         // handle error     }      uv_error = uv_ip4_addr("127.0.0.1", nport, &dest_addr);      if (uv_error){         // handle error     }      n_shell_connect = malloc(sizeof(uv_connect_t));      if (!n_shell_connect){         // handle error     }      uv_error = uv_tcp_connect(n_shell_connect, n_shell_socket, (struct sockaddr *) &dest_addr, nshell_connect);      if (uv_error){         // handle error     }      uv_error = uv_run(n_shell_loop, uv_run_default);      if (uv_error){         // handle error     }      return 0; }  nerror * nshell_loop_main(nshell * n_shell){      int uv_error = 0;      nerror * n_error = 0;      uv_loop_t * n_shell_loop = 0;      n_shell_loop = malloc(sizeof(uv_loop_t));      if (!n_shell_loop){         // handle error     }      uv_error = uv_loop_init(n_shell_loop);      if (uv_error){         // handle error     }      n_error = nshell_client_main(n_shell, n_shell_loop);      if (n_error){         // handle error     }      uv_loop_close(n_shell_loop);     free(n_shell_loop);      return 0; } 

the assertion happening @ end of switch statement in excerpt of code (taken joyent's libuv page on github):

void uv_close(uv_handle_t* handle, uv_close_cb close_cb) {   assert(!(handle->flags & (uv_closing | uv_closed)));    handle->flags |= uv_closing;   handle->close_cb = close_cb;    switch (handle->type) {   case uv_named_pipe:     uv__pipe_close((uv_pipe_t*)handle);     break;    case uv_tty:     uv__stream_close((uv_stream_t*)handle);     break;    case uv_tcp:     uv__tcp_close((uv_tcp_t*)handle);     break;    case uv_udp:     uv__udp_close((uv_udp_t*)handle);     break;    case uv_prepare:     uv__prepare_close((uv_prepare_t*)handle);     break;    case uv_check:     uv__check_close((uv_check_t*)handle);     break;    case uv_idle:     uv__idle_close((uv_idle_t*)handle);     break;    case uv_async:     uv__async_close((uv_async_t*)handle);     break;    case uv_timer:     uv__timer_close((uv_timer_t*)handle);     break;    case uv_process:     uv__process_close((uv_process_t*)handle);     break;    case uv_fs_event:     uv__fs_event_close((uv_fs_event_t*)handle);     break;    case uv_poll:     uv__poll_close((uv_poll_t*)handle);     break;    case uv_fs_poll:     uv__fs_poll_close((uv_fs_poll_t*)handle);     break;    case uv_signal:     uv__signal_close((uv_signal_t*) handle);     /* signal handles may not closed immediately. signal code */     /* close uv__make_close_pending whenever appropriate. */     return;    default:     assert(0); // assertion happening here   }    uv__make_close_pending(handle); } 

i call uv__tcp_close manually, it's not in public headers (and not right solution anyway).

libuv not done handle until it's close callback called. exact moment when can free handle.

i see call uv_loop_close, don't check return value. if there still pending handles, return uv_ebusy, should check that.

if want close loop , close handles, need following:

  • use uv_stop stop loop
  • use uv_walk , call uv_close on handles not closing
  • run loop again uv_run close callbacks called , can free memory in callbacks
  • call uv_loop_close, should return 0 now

Comments

Popular posts from this blog

java - How to print docx and pdf? -

html - jquery - p element wont show after I hid it -

qml - Is it possible to implement SystemTrayIcon functionality in Qt Quick application -