Skip to content

Backport “gkeyfile: remain usable after g_key_file_free()” to glib-2-58

Philip Withnall requested to merge 2-58-339-key-file-free-fix into glib-2-58

Previously, in the case where 'kf' has more than one ref, calling g_key_file_free(kf) would break it. For example, calling g_key_file_has_key(kf, ...) would hit the following assertion:

g_hash_table_lookup: assertion 'hash_table != NULL' failed

This is because g_key_file_free() calls g_key_file_clear() which sets self->groups and other fields to NULL; most lookup functions assume these fields are non-NULL.

One fix would be to call g_key_file_init() right after g_key_file_clear() in g_key_file_free(). However, in the case where there are no other refs to the keyfile, this would mean allocating many new hash tables which will be immediately destroyed when g_key_file_unref() removes the last ref. Instead, inline the unref, and re-initialize the internal state when the keyfile is still alive.

Backport of !339 (merged).

Merge request reports