[sword-devel] curl library and download termination

Eeli Kaikkonen eekaikko at mail.student.oulu.fi
Sun Nov 1 14:37:33 MST 2009

On Fri, 30 Oct 2009, Eeli Kaikkonen wrote:
> It shouldn't be too hard to replace per-object easy_init with static
> global_init.

This seems to be easier than any. Currently curlftpt.cpp has these

static CURLFTPTransport_init _CURLFTPTransport_init;

CURLFTPTransport_init::CURLFTPTransport_init() {
        //curl_global_init(CURL_GLOBAL_DEFAULT);  // curl_easy_init
automatically calls it if needed

CURLFTPTransport_init::~CURLFTPTransport_init() {

The only change needed should be to uncomment curl_global_init.
Apparently this has been written according to documentation but for some
reason left out. Maybe because curl_easy_init calls it only when needed.
But the documentation in several places discourages using the automatic
global init by easy_init and instead exhorts to use global init
explicitly: "This failsafe feature exists mainly for backward
compatibility because there was a time when the global functions didn't
exist. Because it is sufficient only in the simplest of programs, it is
not recommended for any program to rely on it."

I don't have tested this but my claim and the current code is based on
the documentation:

"The global constant situation merits special consideration when the
code you are writing to use libcurl is not the main program, but rather
a modular piece of a program, e.g. another library. As a module, your
code doesn't know about other parts of the program -- it doesn't know
whether they use libcurl or not. And its code doesn't necessarily run at
the start and end of the whole program.

A module like this must have global constant functions of its own, just
like curl_global_init() and curl_global_cleanup(). The module thus has
control at the beginning and end of the program and has a place to call
the libcurl functions. Note that if multiple modules in the program use
libcurl, they all will separately call the libcurl functions, and that's
OK because only the first curl_global_init() and the last
curl_global_cleanup() in a program change anything. (libcurl uses a
reference count in static memory).

In a C++ module, it is common to deal with the global constant situation
by defining a special class that represents the global constant
environment of the module. A program always has exactly one object of
the class, in static storage. That way, the program automatically calls
the constructor of the object as the program starts up and the
destructor as it terminates. As the author of this libcurl-using module,
you can make the constructor call curl_global_init() and the destructor
call curl_global_cleanup() and satisfy libcurl's requirements without
your user having to think about it."

	Eeli Kaikkonen (Mr.), Oulu, Finland
	e-mail: eekaikko at mailx.studentx.oulux.fix (with no x)

More information about the sword-devel mailing list