gobject: use a DllMain to initialize gobject on windows

It seems that VS 2015 optimizes out the constructor on windows,
so it is better to use a DllMain to initialize the library
and keep using a normal constructor on the other platforms.
This research was done by  Arnav Singh.

https://bugzilla.gnome.org/show_bug.cgi?id=752837
This commit is contained in:
Ignacio Casal Quinteiro 2015-10-09 13:22:34 +02:00
parent cd1eba043c
commit 7a29771a74

View File

@ -34,6 +34,10 @@
#include "glib-private.h"
#include "gconstructor.h"
#ifdef G_OS_WIN32
#include <windows.h>
#endif
#ifdef G_ENABLE_DEBUG
#define IF_DEBUG(debug_type) if (_g_type_debug_flags & G_TYPE_DEBUG_ ## debug_type)
#endif
@ -4360,17 +4364,8 @@ g_type_init (void)
g_assert_type_system_initialized ();
}
#if defined (G_HAS_CONSTRUCTORS)
#ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
#pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(gobject_init_ctor)
#endif
G_DEFINE_CONSTRUCTOR(gobject_init_ctor)
#else
# error Your platform/compiler is missing constructor support
#endif
static void
gobject_init_ctor (void)
gobject_init (void)
{
const gchar *env_string;
GTypeInfo info;
@ -4464,6 +4459,47 @@ gobject_init_ctor (void)
_g_signal_init ();
}
#if defined (G_OS_WIN32)
BOOL WINAPI DllMain (HINSTANCE hinstDLL,
DWORD fdwReason,
LPVOID lpvReserved);
BOOL WINAPI
DllMain (HINSTANCE hinstDLL,
DWORD fdwReason,
LPVOID lpvReserved)
{
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
gobject_init ();
break;
default:
/* do nothing */
;
}
return TRUE;
}
#elif defined (G_HAS_CONSTRUCTORS)
#ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
#pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(gobject_init_ctor)
#endif
G_DEFINE_CONSTRUCTOR(gobject_init_ctor)
static void
gobject_init_ctor (void)
{
gobject_init ();
}
#else
# error Your platform/compiler is missing constructor support
#endif
/**
* g_type_class_add_private:
* @g_class: class structure for an instantiatable type