Glad my comments helped.
One quick thing/clarification - that bug in the latest 'released' version of MHD was related to cleaning up the list of internal connection/request structures. (This occurred during the 'stop daemon' call - manifesting itself with a very long shutdown time.) To get the fix for this, you'd have to download the latest source - the released version still has the bug.
From what I can tell, MHD re-uses the same internal structure/thread if it's not servicing multiple requests. I.e., if you do one request at a time, the same one gets used over and over. If you do 3 at once, new ones get created so 3 get used. Something related to this could explain why you don't see the memory leak when you do the requests one at a time. Maybe there's a leak in MHD for each new structure/thread that gets created and/or each one that gets created beyond the first? Alternatively, maybe your code just isn't thread-safe?
Does the memory leak continue to occur/grow over time, or is it just 20kB per connection/request the first time? In other words - if you hit it with 6 simultaneous requests and get 120kB of memory leak, wait 10 seconds, then hit it again with another 6 simultaneous requests .. do you get ANOTHER 120kB of memory leak, or does it stop getting worse? Knowing that may provide you with some clues. (E.g., if it doesn't go up any more, it seems more likely to be an MHD thing in its internal structures. If it continues to go up, it could be either/or.)
Have you tried Valgrind or some printfs to make sure that free
is called for every malloc you're doing? Even a simple counter would
help. I'm guessing you really need to make absolutely sure the free is getting called for every one - especially with lots of early-returns.
Another thought - maybe one of your (or MHD's, I guess) global variables is getting overwritten with each request. Doing one request at a time gives the code a chance to de-allocate the memory before the pointer gets overwritten. That seems more likely to me than some cleanup code never being called. What I'm getting at - I'm assuming your code is getting called by MHD's
multiple threads simultaneously rather than serially. Any
globals/static storage would get overwritten - you can't just assume the
function can clean up before the next gets called without using mutexes
or semaphores or something. Are any of your globals keeping track of memory? What about static variables?
Oh - and how are you determining you have a memory leak, anyway? (I'm not doubting you have one, just curious as to how you know.)