libmicrohttpd
[Top][All Lists]
Advanced

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

Re: [libmicrohttpd] memory issue in MHD_create_response_from_data


From: Alexander Antimonov
Subject: Re: [libmicrohttpd] memory issue in MHD_create_response_from_data
Date: Sat, 07 Feb 2009 21:36:02 +0200
User-agent: Thunderbird 2.0.0.14 (X11/20080505)

Christian Grothoff wrote:
On Friday 06 February 2009 09:45:59 am you wrote:
When I used dynamic memory allocated by new operator and gave to
MHD_create_response_from_data(size, newed_point, MHD_YES, MHD_NO), I got
double freeing memory crash while I did not delete the dynamic memory in my
own code.

First of all, you should not use "new" for this, use "malloc". This is a C library, not C++.
Christian

I've just realized that interface of the MHD_create_response_from_data
is not so good. Even if "malloc" is used instead of the "new", there is
still an issue.

Let me explain.
Say, we have an executable and a shared object.
A general rule for such kind of interaction is if one of the sides
creates an object and passes its ownership to the other side, the first
side should provide corresponding freeing function for the object. When
the side which has taken the ownership decides to release the object, it
calls the freeing function provided by the first side.
The same is correct in the opposite direction.

In the MHD there are:
-   "struct MHD_Response* MHD_create_response_from_data()"
    and corresponding function "void MHD_destroy_response()"

-   "struct MHD_PostProcessor* MHD_create_post_processor()"
    and corresponding function "int MHD_destroy_post_processor()"

If one side allocates memory with "malloc" the other side should use the
"free" function from the first side to free that memory.

Well, maybe current MHD_create_response_from_data will work if both
sides are compiled by the same compiler (and use the same "C" runtime),
it almost definitely will not if compilers is different (problem point
is linking phase).

Why it is so you can read in details here:
http://lists.trolltech.com/qt-interest/2003-01/thread00220-0.html
the right answers start from message 12.

In the light of the above, I think it's better to change interface of
the MHD_create_response_from_data to something like this:

  typedef void(*) MHD_DataFreeCallback (void *cls);

  struct MHD_Response* MHD_create_response_from_data(
                       size_t size,
                       void * data,
                       MHD_DataFreeCallback crdfc,
                       int must_copy);

crdfc == NULL is equivalent to must_free == MHD_NO,
crdfc != NULL is equivalent to must_free == MHD_YES, and crdfc should be
used by MHD to free the data. In this case it doesn't matter what was
used to allocate memory: "new" or "malloc".



reply via email to

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