cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: fail to `malloc` pointers after a call to curl_easy_perform.

From: Ray Satiro via curl-library <curl-library_at_cool.haxx.se>
Date: Fri, 15 Jan 2016 02:50:11 -0500

On 1/14/2016 1:47 PM, ataro wrote:
> I'm developing a GUI app on Microsoft Windows to make HTTP requests
> with libcurl.
>
> Because this is a GUI app, I need to initialize the curl easy handle
> from a separate thread in order to avoid the main window procedure
> from being blocked until curl_easy_perform return.
>
> The problem is that in order to do the HTTP requests I do some
> `malloc` calls to store in the memory some string which I use them in
> the HTTP request.
> The strange thing is that after I make a call to curl_easy_perform
> function, all my calls to the `free` CRT function are failed with the
> return code 9 which according to
> msdn.microsoft.com/en-us/library/t3ayayh1.aspx
> <https://msdn.microsoft.com/en-us/library/t3ayayh1.aspx> is an error
> of "Bad File Number". In contrast, all the calls to the `free`
> function before the call to curl_easy_perform are returned with the
> code 0 which means that the free function successfully freed the memory.
>
> I didn't find any clue to this strange behavior. can you help me please?
>

Unless you set errno to 0 before calling free there's no way to tell if
errno was set by free. This is not explicitly documented in free [1] but
it is in errno [2].

free says:
"If an error occurs in freeing the memory, errno is set with information
from the operating system on the nature of the failure. For more
information, see errno, _doserrno, _sys_errlist, and _sys_nerr."

errno says:
"Run-time library calls that set errno on an error do not clear errno on
success. Always clear errno by calling _set_errno(0) immediately before
a call that may set it, and check it immediately after the call."

Both could have explained it better. In most cases a function will
document with which of its return values errno has been set and so it
may not be necessary to set errno to 0 beforehand. In this case though
you must.

If you are already doing this then you will need to give us more
information, preferably a self contained example with the minimum amount
of code needed to reproduce your issue, or if not that then some code
that shows what you are talking about and someone on this list may try
to reproduce.

[1]: https://msdn.microsoft.com/en-us/library/we1whae7.aspx
[2]: https://msdn.microsoft.com/en-us/library/t3ayayh1.aspx

-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2016-01-15