Hi,
I am implementing a HTTP server application that utilizes HTTP client in its generic handler. i.e. on receiving a request it makes several requests to other servers and after it gets responses back it finalizes original request. Here is pseudo code of the generic callback:
global_base;
generic_callback() {
 url = get_user_data(req);
 for (server : remote_servers) {
  Âevcon = evhttp_connection_base_new(global_base, ..);
  Âreq = evhttp_request_new(client_request_done, req, url);
  Âevhttp_make_request(evcon, req);
 }
}
The code works fine. I've decided to check it for memory leaks using valgrind and discovered that a lot of memory is lost in evhttp_connection_base_new(). I believe my code should cleanup the connections I've created.
I checked libevent examples and googled but I did not find how to make this cleanup properly. All examples show HTTP client with single connection created in main() function that is cleaned after event_base_dispatch() is over. But in my case I dynamically create connection and I need to clean it when it is unused. I was trying to run the evhttp_connection_free() function in client_request_done callback (the one passed to evhttp_request_new), but evhttp_request_get_connection(req) returns NULL. It means the connection object is not accessible in the callback function. Hmm... How do I expect to free the connection then?
I see version 2.1 has function evhttp_connection_free_on_completion() but it still does not help to prevent leaks in evhttp_connection_base_new().
Here is the code that I was trying to profile:
 https://github.com/anatol/pacoloco/blob/master/hub.c#L213
And here is the valgrind report ran against current HEAD (a77a82a03f85):
How to prevent this memory leak in the http client code?