Subject: Re: Global constructors in DLL ?
From: [email-masked] (Mumit Khan)

13 Aug 1998 01:53:51 -0700
Message-ID: <[email-masked]>
References: <[email-masked]>
To: "Colin Peters" <[email-masked]>
Cc: "Edwin Essenius" <[email-masked]>,
        "GNU-win32" <[email-masked]>, [email-masked],
        [email-masked]

"Colin Peters" <[email-masked]> writes:
> Edwin Essenius <[email-masked]> wrote:
> 
> >I am using cywin B19.2, with the sergey patches an trying to build a
> >C++ DLL. Except for the Global constructors all seems to work.
> >Global variables in the DLL are initialized, but the global constructors
> >are not called when it is declared in a DLL.
> 
> 
> If you have written or obtained your own startup code for the DLL (what the
> entry point is set to on the link line, or DllMain possibly) then you should
> check that you are calling __main in the startup code on DLL_PROCESS_ATTACH.
> __do_global_dtors should be called on DLL_PROCESS_DETACH. Also make sure to
> disable atexit inside the DLL (mainly for the call to __main) or the main
> process may attempt to call your destructors when it exits, which could be
> after the DLL has been detached.

Colin's right on the mark as to the cause of the global constructors not
working correctly. Here's the change to gcc sources that causes this
problem:

  Mon Mar 30 13:11:05 1998  Stan Cox  <[email-masked]>

	* libgcc2.c: (__main, __do_global_dtors, __do_global_ctors):
	For __CYGWIN32__ use the versions in winsup/dcrt0.cc.

The problem is that the current default DLL startup in winsup calls 
*its* definition of __do_global_ctors, which doesn't call GCC generated 
C++ global initializers (cf: __CTOR__LIST__ and __DTOR_LIST__), and hence 
your C++ globals aren't being initialized.

I get around it by using my own DLL startup, but that's a path I won't
recommend since you might be missing things that the winsup DLL startup
does beyond just initializing globals.

Stan, could you please take a look at this problem? The easiest way is to
simply copy the libgcc2.c version of DO_GLOBAL_CTORS_BODY (gbl-ctors.h)
into winsup's __do_global_ctors (and similarly for __do_global_dtors).

Regards,
Mumit

ps: I'm copying this message to cygwin32-developers list as well, since this 
has the potential to be another "cygwin32 comes with broken C++" story.

-
For help on using this list (especially unsubscribing), send a message to
"[email-masked]" with one line of text: "help".