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: Christian Grothoff
Subject: Re: [libmicrohttpd] memory issue in MHD_create_response_from_data
Date: Fri, 6 Feb 2009 11:07:44 -0700
User-agent: KMail/1.10.3 (Linux/2.6.27-9-generic; KDE/4.1.3; i686; ; )

On Friday 06 February 2009 09:45:59 am you wrote:
> > As for getting
> > the parameter, try
> >
> > MHD_lookup_connection_value(con, MHD_GET_ARGUMENT_KIND, "start").
>
>  Thanks, Christian.
>
> 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++. 

> Then I changed to use auto but local variable and call
> MHD_create_response_from_data(size, local_point, MHD_NO, MHD_NO). The crash
> is gone but sometimes (about 50% of refresh) some portion of  my response
> was lost at brower. It seems the first 4 letters, <?xm, were not received
> by browser, resulting invalid xml file. I printed out response just before
> calling MHD_create_response_from_data(), it was correct. Any hint where I
> did wrong?

If you used a local variable, you should have used MHD_NO, MHD_YES for the 
options.  NO+NO only makes sense if the content is immutable in a global 
constant (say if you're passing a "const char*" / C string).

> My understanding is:
> 1. if the response is stored in a dynamic memory allocated by c++ new
> operator, it is best to call MHD_create_response_from_data(size,
> newed_point, MHD_YES, MHD_NO), and I should not delete the newed point by
> myself.

Again, use C malloc, not C++ new.  Then the above is correct.

> 2. if the response is stored in a local auto varible, it seems I should
> call MHD_create_response_from_data(size, newed_point, MHD_NO, MHD_YES). So
> queued response won't point to auto memoy which will be released when
> callback exits.

Right.

> 3. if the response is stored in glabal memery, I should call
> MHD_create_response_from_data(size, newed_point, MHD_NO, MHD_NO).
>
> Did I understand this correctly?

Yes.

Christian



reply via email to

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