Merge branch 'fix-leaking-g_get_language_names_with_category' into 'master'

gcharset: fix leaking g_get_language_names_with_category

See merge request GNOME/glib!338
diff --git a/glib/gcharset.c b/glib/gcharset.c
index bfcd125..a97b33a 100644
--- a/glib/gcharset.c
+++ b/glib/gcharset.c
@@ -576,15 +576,16 @@
  *
  * g_get_language_names() returns g_get_language_names_with_category("LC_MESSAGES").
  *
- * Returns: (array zero-terminated=1) (transfer none): a %NULL-terminated array of strings owned by GLib
- *    that must not be modified or freed.
+ * Returns: (array zero-terminated=1) (transfer none): a %NULL-terminated array of strings owned by
+ *    the thread g_get_language_names_with_category was called from.
+ *    It must not be modified or freed. It must be copied if planned to be used in another thread.
  *
  * Since: 2.58
  */
 const gchar * const *
 g_get_language_names_with_category (const gchar *category_name)
 {
-  static GPrivate cache_private = G_PRIVATE_INIT ((void (*)(gpointer)) g_hash_table_remove_all);
+  static GPrivate cache_private = G_PRIVATE_INIT ((void (*)(gpointer)) g_hash_table_unref);
   GHashTable *cache = g_private_get (&cache_private);
   const gchar *languages;
   GLanguageNamesCache *name_cache;
diff --git a/glib/tests/charset.c b/glib/tests/charset.c
index 0a1c8ce..363eedf 100644
--- a/glib/tests/charset.c
+++ b/glib/tests/charset.c
@@ -59,6 +59,18 @@
     }
 }
 
+static void
+test_language_names_with_category_async (void)
+{
+  g_thread_join (g_thread_new (
+      NULL, (GThreadFunc)g_get_language_names_with_category, "LC_CTYPE"));
+
+  /* g_get_language_names_with_category returns a pointer to a memory
+     which is owned by a thread it has been called from. The thread is dead now,
+     therefore returned pointer can't be used at this stage.
+  */
+}
+
 int
 main (int argc, char *argv[])
 {
@@ -67,6 +79,7 @@
   g_test_bug_base ("http://bugs.gnome.org/");
 
   g_test_add_func ("/charset/language_names_with_category", test_language_names_with_category);
+  g_test_add_func ("/charset/language_names_with_category_async", test_language_names_with_category_async);
 
   return g_test_run ();
 }