Unregister dynamic types when they are not used anymore
Submitted by Patrik Olsson
Link to original bug (#614962)
Description
Created attachment 158047 Patch stub
Dynamic types are not really that dynamic. For example, GTypePlugins must live forever (and the GTypeModule implementation seems to force this by re-reffing on dispose). That's really bad design because it means applications will need to check if the module can be loaded before the GTypePlugin instance is created (to avoid creating plugin instances that might just fail on use and then become a living dead). But that's not reliable either because the module (i.e. the shared library on the file system) could be removed between checking if it exists and calling type_plugin_use. Not likely to happen, but still bad design!
There's really no reason, from what I can see, that they have to live forever. When a GTypePlugin goes away or is unloaded, the dynamic types registered by it should be unregistered.
To implement this I think g_type_register_dynamic could add a weak ref to the GTypePlugin. The weak ref callback would unregister the types again when the GTypePlugin goes away. GTypePlugins should also unregister types (with g_type_unregister_dynamic) when they are unloaded (i.e. use_count goes to 0).
I have written a patch stub, but I'm not sure how to complete it (or if it's correct). Perhaps somebody else could complete it or give me pointers on how to continue?
Patch 158047, "Patch stub":
unregister.patch
Version: 2.34.x