cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: Where to call curl_global_cleanup in a UNIX shared library?

From: Bjorn Reese <breese_at_mail1.stofanet.dk>
Date: Sat, 01 Dec 2001 13:45:21 +0000

Miklos Nemeth wrote:

> It's clear that one can call curl_global_cleanup
> in the DllMain when the DLL is shut down.
>
> Is there similar point in UNIX shared libraries?

No, but I can think of three alternatives:

 1. You can add a clean-up function with the ANSI C function atexit().
    This clean-up function can then call curl_global_cleanup.

    void global_end(void) { curl_global_cleanup(); }

    /* The following must be added to where you initialize your library */
    atexit(global_end);
    curl_global_init(yourflagshere);

 2. In C++ you can have a global object whose destructor calls
    curl_global_cleanup. For example

    class Global
    {
    public:
      Global() { curl_global_init(yourflagshere); }
      ~Global() { curl_global_cleanup(); }
    };
    Global global;

 3. With some C compilers you can specify so-called init/fini functions
    (which are the same as constructors/destructors in C++). Put the call
    to curl_global_cleanup in the fini function. If and how you use these
    depends on your compiler. For example, with GCC you write

    void global_start() __attribute__ ((constructor));
    void global_start() { curl_global_init(yourflagshere); }
    void global_end() __attribute__ ((destructor));
    void global_end() { curl_global_cleanup(); }

    With the Sun Workshop compiler you must use pragmas

    void global_start() { curl_global_init(yourflagshere); }
    #pragma init (global_start)
    void global_end() { curl_global_cleanup(); }
    #pragma fini (global_end)

    For other compilers you have to consult your documentation.
Received on 2001-12-01