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

标签 http free libevent

我使用 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/

相关文章:

javascript - 使用 Angular 发送 ajax 请求以获取 .json 文件时出错?

c++ - 删除时无效指针崩溃,但指针不同

http - Libevent HTTP 服务器和压缩?

c - hiredis "undefined reference to"编译错误

python - Google App Engine urlfetch 到 POST 文件

快速 uploadTaskWithRequest 和 didReceiveData

c# - 从 http 调用中获得正确的响应,而不是异常

c - 内存释放期间运行时堆异常

c - 我是否在我的结构指针上正确地调用了 free() ?

libevent - libev 和 libevent 有什么区别?