我测试了我使用 libuv 的小程序。
程序的调试输出显示内存泄漏。
(健康)状况
libuv 版本
#define UV_VERSION_MAJOR 0
#define UV_VERSION_MINOR 9
- 操作系统:Windows 7.0
- 编译器:vs2010
我的测试代码
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <crtdbg.h>
#include <conio.h>
#include <uv.h>
void on_new_connection(uv_stream_t *server, int status) {
}
int main()
{
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
uv_tcp_t server;
uv_tcp_init(uv_default_loop(), &server);
struct sockaddr_in bind_addr = uv_ip4_addr("0.0.0.0", 9123);
uv_tcp_bind(&server, bind_addr);
// leak occurred here
uv_listen((uv_stream_t*)&server, 128, on_new_connection);
//uv_close((uv_handle_t*)&server, NULL);
return 0;
}
结果
Detected memory leaks!
Dumping objects ->
{56} normal block at 0x002A3258, 11136 bytes long.
Data: <T B > 54 F8 42 00 09 00 00 00 CD CD CD CD CD CD CD CD
Object dump complete.
内部泄漏位置
调用栈
uv_tcp_listen(...)
uv_listen(...)
main(...)
代码
uv_tcp_listen(...)
{
....
if(!handle->accept_reqs) {
handle->accept_reqs = (uv_tcp_accept_t*)
malloc(uv_simultaneous_server_accepts * sizeof(uv_tcp_accept_t)); <<
....
}