diff --git a/glib/tests/constructor-helper.c b/glib/tests/constructor-helper.c index 476c361b1..4b3c107b1 100644 --- a/glib/tests/constructor-helper.c +++ b/glib/tests/constructor-helper.c @@ -61,6 +61,9 @@ void string_add_exclusive (const char *string); MODULE_EXPORT int string_remove (const char *string); +MODULE_EXPORT +int string_find (const char *string); + MODULE_EXPORT void string_check (const char *string); @@ -204,6 +207,19 @@ string_remove (const char *string) return 1; } +/**< private > + * string_find: + * + * @string: NULL-terminated string. Must not be empty + * + * Returns 1 if the string is present, 0 otherwise + */ +MODULE_EXPORT +int string_find (const char *string) +{ + return string_find_index_ (string) < sizeof (buffer); +} + /**< private > * string_check: * diff --git a/glib/tests/constructor.c b/glib/tests/constructor.c index 955d071ab..001d7542f 100644 --- a/glib/tests/constructor.c +++ b/glib/tests/constructor.c @@ -40,6 +40,8 @@ void string_add_exclusive (const char *string); MODULE_IMPORT void string_check (const char *string); +MODULE_IMPORT +int string_find (const char *string); #if G_HAS_CONSTRUCTORS @@ -66,9 +68,13 @@ dtor (void) { string_add_exclusive (G_STRINGIFY (PREFIX) "_" "dtor"); -#ifdef BUILD_TEST_EXECUTABLE - _Exit (EXIT_SUCCESS); -#endif + if (string_find ("app_dtor") && string_find ("lib_dtor")) + { + /* All destructors were invoked, this is the last. + * Call _Exit (EXIT_SUCCESS) to exit immediately + * with a success code */ + _Exit (EXIT_SUCCESS); + } } #endif /* G_HAS_CONSTRUCTORS */ @@ -210,7 +216,12 @@ test_lib (gconstpointer data) unload_library (); #if G_HAS_DESTRUCTORS - string_check ("lib_" "dtor"); + /* Destructors in dynamically-loaded libraries do not + * necessarily run on dlclose. On some systems dlclose + * is effectively a no-op (e.g with the Musl LibC) and + * destructors run at program exit */ + g_test_message ("Destructors run on module unload: %s\n", + string_find ("lib_" "dtor") ? "yes" : "no"); #endif #if defined (_WIN32) && defined (G_HAS_TLS_CALLBACKS) string_check ("lib_" "tlscb_process_detach");