Closing libUV Handles Correctly

后端 未结 2 2065
粉色の甜心
粉色の甜心 2021-01-31 06:48

I\'m trying to find out how to fix these memory leaks I\'m getting while running this program with Valgrind. The leaks occur with the two allocations in nShell_client_main

2条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-31 07:15

    libuv is not done with a handle until it's close callback is called. That is the exact moment when you can free the handle.

    I see you call uv_loop_close, but you don't check for the return value. If there are still pending handles, it will return UV_EBUSY, so you should check for that.

    If you want to close a loop and close all handles, you need to do the following:

    • Use uv_stop to stop the loop
    • Use uv_walk and call uv_close on all handles which are not closing
    • Run the loop again with uv_run so all close callbacks are called and you can free the memory in the callbacks
    • Call uv_loop_close, it should return 0 now

提交回复
热议问题