[PATCH] examples: Get pedantic about initialization and cleanup
Date: Tue, 25 Nov 2014 02:22:35 -0500
These changes make the examples reflect what is API documented regarding
initialization and cleanup.
All examples will now:
- Call curl_global_init immediately after the program starts.
- Call curl_global_cleanup immediately before the program exits.
- Call curl_easy_cleanup for each easy handle.
- Follow the cleanup call order for multi handles and the easy handles
they contain. Specifically this order: curl_multi_remove_handle,
More can be done. To really get pedantic there needs to be logic to
check every curl function that can return an error. For example right
now in some cases curl_multi_init() isn't checked for NULL.
I already have received some feedback:
On 11/23/2014 3:24 PM, Steve Holme wrote:
> 1) You have introduced EXIT_SUCCESS and EXIT_FAILURE for when they are
> not already defined but from the examples I went over I couldn't see
> where EXIT_SUCCESS was being used, with the exception of 10-at-a-time.c
Yes. Although 0 is synonymous with success I figured since I was making
sure EXIT_FAILURE is defined I would check for EXIT_SUCCESS as well.
> 2) Some examples perform the return of the curl result code (for
> example "return rv" in cacertinmem.c) - EXIT_FAILURE then clashes with
> CURLE_UNSUPPORTED_PROTOCOL which could be confusing to a new user
> running that code.
The curl result code is returned by these examples:
You are right that could be confusing. How about in all examples instead
of returning EXIT_FAILURE if curl_global_init/cleanup failure exits
> 3) Do we really want to promote the use of atexit()? I personally
> prefer the clean up that was already present in the examples that
> performed it. Whilst I didn't write most of these examples (just added
> some of the imap and pop3 examples) the current clean up code matches
> how I write it.
I don't see what's wrong with atexit. I use it often. Also it was easier
to add code to call an exit handler rather than modify all the examples
so that for every exit path curl_global_cleanup is called.
I do have some reservations about curl_global_cleanup because it's not
always possible to use it in the way it's documented. On Windows at
least you're going to have situations where on process termination or
dll unload there are still multiple threads running. I'd certainly like
to learn more about this. WSACleanup will be fine because it's
refcounted but that OpenSSL cleanup could be a killer. They don't have
any refcounted general init or cleanup functions (please correct me if
this is wrong).
> 4) I would recommend a one line comment for the curl_global_init() and
> curl_global_cleanup() function calls as they a) now bulk out the code
> quite a bit and b) are repeated in every example so may be ignored in
> a lot of causes - just making reading more difficult.
That runs contrary to why I made these changes, to make the examples
conform to the documentation and stress the importance of initialization
and cleanup. To help someone who's new to libcurl and is just going to
skim one of the examples before some c&p. Getting the initialization
right seems very important, but the cleanup as I said I'm unclear. My
comments in the examples reflect what is documented though.
- application/gzip attachment: stored