[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [libmicrohttpd] MHD_RESPMEM_PERSISTENT
From: |
Evgeny Grin |
Subject: |
Re: [libmicrohttpd] MHD_RESPMEM_PERSISTENT |
Date: |
Tue, 24 Jan 2017 19:48:56 +0300 |
User-agent: |
Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.6.0 |
Hi Miguel,
Quote from manual for MHD_destroy_response: "Note that MHD may keep some
of the resources around if the response is still in the queue for some
clients, so the memory may not necessarily be freed immediately."
https://www.gnu.org/software/libmicrohttpd/manual/html_node/microhttpd_002dresponse-enqueue.html
So until response is actually send - it's not really destroyed.
If response is queued multiple times - it will be destroyed only after
last response was sent.
Instead of your memcpy() you should use MHD_RESPMEM_MUST_COPY. As soon
as response is really destroyed - buffer will be freed.
--
Best Wishes,
Evgeny Grin
On 24.01.2017 19:22, Miguel Sancho wrote:
> Hi Silvio, thanks for answering,
>
>> Could you show what error and how the buffer was assigned?
>
> The error is that the downloaded binary file from the MHD server with
> the MHD_RESPMEM_PERSISTENT option is corrupt:
>
> * Same size as the original
> * The initial bytes are ok
> * but from a certain point file contents are set to '0'. The
> free(buffer) seems to impact the content downloaded in the http
> response, I do not how because free() is after the
> MHD_destroy_response()
> * Buffer is correctly downloaded when using the MHD_RESPMEM_MUST_COPY
> option but I wanted to minimize the memory impacts
>
> The buffer is allocated by the MHD server with malloc, simplifying:
> unsigned char* buffer = (unsigned char*)malloc(dataSize);
> memcpy(buffer, data, dataSize);
>
> Another thing I observe is that if*I remove the free(buffer)*, the file
> is correctly downloaded. However this solution is not valid because in
> this case I have a memory leak:
> / response = MHD_create_response_from_//buffer(
>
> size,(void*)buffer,*MHD_RESPMEM_PERSISTENT*);
> ret = MHD_queue_response (connection, 200, response);
> MHD_destroy_response (response);
> //free(buffer); *// Memory Leak!*/*
> *
> Thanks
>
> Miguel