(How) am I supposed to destroy a uv_async_t?

丶灬走出姿态 提交于 2020-01-15 03:35:50

问题


After I'm done with a uv_async_t, I'm supposed to destroy it to avoid any leaks, right? From glancing at the docs, it appears I'm supposed to use uv_close() for this, but it takes a uv_handle_t*, not a uv_async_t*. Furthermore, it looks like casting it (as in uv_close((uv_handle_t *)async, NULL)) would cause a strict aliasing violation. Is that what I'm supposed to do anyway?


回答1:


Yes, you have to cast the uv_async_t* to uv_handle_t*. That's how libuv internally works.

All handles share the base structure, so IIRC strict aliasing rules are not broken because it amounts to casting it to the first member of the structure.

A note on your example call to uv_close: you can only free the memory for a handle in the close callback, not before, so if you pass NULL and the handle was allocated on the heap you won't know when you can free the memory.



来源:https://stackoverflow.com/questions/38833227/how-am-i-supposed-to-destroy-a-uv-async-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!