Hello Gavin,

[resend to the list, sorry fot the noise Gavin]

Le 2022-01-17 à 14 h 40, Gavin Henry a écrit :> Hi all,
>
> I'm using json_dumps from jansson in my MHD_create_response_from_buffer
>
> Where is the best place to free this? I've tried at the end of my responses:
>
> MHD_destroy_response(response);
> free(json_reply)
> return ret;
>
> but this obviously breaks the data that goes to the client:
> If I understand your code, you use MHD_create_response_from_buffer with the MHD_RESPMEM_PERSISTENT mode, which, according to the documentation [1], means that 'reply' should be static/global, which isn't.

You can either:
- use MHD_RESPMEM_MUST_FREE and let MHD free 'reply' after use using free (if free suits your case) - use MHD_RESPMEM_MUST_COPY, then free 'reply' after with your own free function - or use MHD_create_response_from_buffer_with_free_callback and pass your free callback function pointer: MHD_create_response_from_buffer_with_free_callback(strlen(reply_data), (void *)reply_data, &free)

If you use libc free, I suggest the first case, but if you can use a different malloc/free function family, then you will need MHD_create_response_from_buffer_with_free_callback. The second option can be avoided in your case, since it will duplicate 'reply' content in the heap.

/Nicolas

[1] https://www.gnu.org/software/libmicrohttpd/manual/html_node/microhttpd_002dresponse-create.html

Reply via email to