gpt4 book ai didi

http - 我应该调用 evhttp_request_free 来释放 http 服务器中的资源吗?

转载 作者:塔克拉玛干 更新时间:2023-11-01 19:08:42 25 4
gpt4 key购买 nike

我使用 libevent2.1.1 编写了一个简单的 http 服务器,我认为我应该在 http_server_callback 中发布 evhttp_requestevhttp_request_free。但是当我运行它时,发生了错误。请告诉我为什么,我应该怎么做。

enter image description here

void http_server_callback (struct evhttp_request *req, void *arg)
{
evhttp_send_reply (req, HTTP_OK, "OK", NULL);

evhttp_request_free(req);
}

int http_server_run (void)
{
struct event_base *base;
struct evhttp *http;
struct evhttp_bound_socket *handle;

base = event_base_new ();
if (! base)
{
fprintf (stderr, "Couldn't create an event_base: exiting\n");
return 1;
}

http = evhttp_new (base);
if (! http)
{
fprintf (stderr, "couldn't create evhttp. Exiting.\n");
return 1;
}

evhttp_set_gencb (http, http_server_callback, NULL);

handle = evhttp_bind_socket_with_handle (http, "127.0.0.1", 8888);
if (! handle)
{
fprintf (stderr, "couldn't bind to port 8888. Exiting.\n");
return 1;
}

event_base_dispatch (base);

return 0;
}

int main (void)
{
WSADATA WSAData;
WSAStartup (0x101, &WSAData);

http_server_run();

return 0;
}

提前致谢

最佳答案

req 会在服务器写完后在回调函数evhttp_send_done 中释放。所以它会导致双重释放。
libevent 中的源代码:

static void
evhttp_send_done(struct evhttp_connection *evcon, void *arg)
{
int need_close;
struct evhttp_request *req = TAILQ_FIRST(&evcon->requests);
TAILQ_REMOVE(&evcon->requests, req, next);

need_close =
(REQ_VERSION_BEFORE(req, 1, 1) &&
!evhttp_is_connection_keepalive(req->input_headers))||
evhttp_is_connection_close(req->flags, req->input_headers) ||
evhttp_is_connection_close(req->flags, req->output_headers);

EVUTIL_ASSERT(req->flags & EVHTTP_REQ_OWN_CONNECTION);
evhttp_request_free(req);
...
}

关于http - 我应该调用 evhttp_request_free 来释放 http 服务器中的资源吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25635862/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com