libmicrohttpd
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [libmicrohttpd] Memory leak and global variables


From: Kenneth Mastro
Subject: Re: [libmicrohttpd] Memory leak and global variables
Date: Thu, 28 Aug 2014 09:03:07 -0400

Not sure this is going to be much help, but a few thoughts:

* I'm far from an expert, but are you sure you can use thread-per-connection and use-select-internally together?  (You very well may be able to, I just thought they weren't normally used together.  Could be completely wrong about that, though.)

* There's a minor bug in the last release (0.9.37 and earlier) of MHD where it doesn't properly cycle through the existing connections when in thread-per-connection mode.  The fix was simple (essentially deleting one character), but if you're detecting leaks by looking at the result just before your program terminates, there's a remote chance that has something to do with it.

* I haven't noticed any leaks in MHD.  I'm not saying there aren't any, but I haven't seen anything like what you're describing.  (Granted - I'm not hammering it like you would be in a web-service-based solution.)

* You have a lot going on in that code.  If it were me, I'd strip it way down to a simple test case to try and isolate the leak(s).  You're doing a lot of mallocs in general - any chance you could (at least temporarily) remove them and reply with fake data?  The point is to try and see if the problem is memory that MHD is allocating or if it's something you're doing.

* You're doing a few mallocs at the start of the request - are you sure that's necessary (e.g., for the 'page' and 'urlcopy')?  Also, are you sure they're all getting cleaned up?  For testing, maybe you could just create a stack-based character string (i.e., no malloc) and tell MHD to copy the response string instead of de-allocating the memory later.  I'm not sure that would be any less efficient, anyway - malloc is kind of slow, and your strings are really short.  (I.e., instead of 'MUST_FREE' use the one that tells MHD to copy the buffer).  I mean - you're returning in the middle of the code all the time (MHD_YES, MHD_NO), but your cleanup code executes at the end of the function.  That seems very very suspicious to me.  In fact - I bet that's what your problem is (or at least one of them).  That connection request function gets called multiple times - but you'll return early some times and the cleanup code at the end never gets executed.  That leaves your 'urlcopy' (and possibly other things) not freed, right?

* I see you have a 'request completed' callback that frees the memory you allocated for/in your connection info.  I assume you've verified that it's getting called?

* As for the globals - I didn't analyze your code in any kind of depth, but you probably already know that MHD can take a pointer to any object you want and keep giving you the pointer through the connection cycle (typically a structure).  At a glance, it looks like you're taking advantage of that.  For things that you need across connections.. I'm not sure what the suggested usage pattern is.  I'm using C++, so I just use class data members.


That's all I've got.  Hope it helps.

Ken



On Wed, Aug 27, 2014 at 11:16 AM, Nicolas Mora <address@hidden> wrote:
Hello,

I'm using libmicrohttpd10 on a Raspberry Pi using Raspbian, the server running has a http daemon for a house automation daemon written in C.

I'm trying to get rid of all the memory leaks I can find, but it seems that some are related to libmicrohttpd and I still can't find them.

I initialize libmicrohttpd with the flags MHD_USE_THREAD_PER_CONNECTION and MHD_USE_SELECT_INTERNALLY on, then when a connection enters, I build the response and fill the buffer with a char* allocated with malloc.
The response is created with the flag MHD_RESPMEM_MUST_FREE, so the buffer is freed by libmicrohttpd if I undertand the documentation properly.
You can see my code there: https://github.com/babelouest/angharad/blob/master/angharad.c
Line 45 is the MHD_start_daemon call, line 199 is the function called, then lines 741 and 1006 are the functions used for POST requests.

There may probably be something wrong with my code but I don't see where. I have my memory consumption growing up, no matter what I try to free everything that is supposed to be dynamically allocated.

In my investigation, when I run the webapplication that calls all the initial REST commands called DEVICES, OVERVIEW, ACTIONS, SCRIPTS and SCHEDULES, most of the calls are simultaneous, the memory consumption increases by 10 to 20Ko. But when I run each call individually, and check the memory consumption after each call, I see no increase at all, I think the problem may be in my use of libmicrohttpd. Do you have an idea where ?

Also, I have another question about variables. I have 3 variables that must be passed to the function ran by libmicrohttpd, but I couldn't find a way to pass additional variables to this function, so I had to use them as global variables. Is there a way to do that more efficiently ?

Thanks in advance for your help.

/Nicolas



reply via email to

[Prev in Thread] Current Thread [Next in Thread]