libuv 读取回调 uv_buf_t 清理

libuv read callback uv_buf_t cleanup

libuv 读取完成回调的签名为:

void (*uv_read_cb)(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf)

我从文档中了解到,我的回调负责释放提供的 uv_buf_t* 的基础成员。我的问题是 - 谁负责释放 buf 指向的内存?

考虑内部函数 uv__read. This is where your callback is invoked (set aside uv__stream_eof 对此 Q/A) 没有多大兴趣。
正如您在函数的 the very first line 处看到的,缓冲区被声明并定义为局部变量:

uv_buf_t buf;

如果你遍历整个函数,你可以看到相同的缓冲区 is used with uv_buf_init and then passed to your callback (see here, here, here, here, here and here 如果你想了解更多细节)。 那么,回到问题:

who is responsible for freeing the memory pointed to by buf?

你和 libuv 都不是。缓冲区在超出范围时会自动释放。你不必关心这个,文档很清楚:你的唯一责任是 base 数据成员。