G_DECLARE_FINAL_TYPE in a plugin library

I followed the selections example from gtk4-demos and there one will find G_DECLARE_FINAL_TYPE . This is however in a plugin which can be loaded any number of times, until now, because when I load this plugin a second time I get the error:

                  cannot register existing type

I followed an earlier disssion on here on the forum that suggested using RTLD_NODELETE but that’s hardly portable and isn’t even a flag g_module_open supports.

Should I avoid using using new GType:s if I need to have a drop-down list in a dynamically loaded user interface? Is there another way of achieving what the selections example does in gtk4-demo without using G_DEFINE_FINAL_TYPE?

(on a side note, does it really take almost 300 lines of code to create a dropdown list? That was the most intimidating example I’ve ever seen)

You cannot load a plugin multiple times: each type can only be registered once in the type system, and types cannot be unloaded. It has nothing to do with a type declared to be final.

No, it doesn’t take 300 lines of code to create a drop down list; it might take 300 lines of code to create a specific drop down list, if you do things like storing custom objects inside a list model, and you count the boilerplate. If you don’t want to use boilerplate, you should probably a programming language that is not C.

You cannot load a plugin multiple times: each type can only be registered once in the type system, and types cannot be unloaded. It has nothing to do with a type declared to be final.

This I understood, it wasn’t my point to imply there was a problem specific to the declartion being _FINAL_TYPE.

The question was rather, If I want to be able to have a plugin system that allows plugins to be unloaded (this is a fairly strong requirement), I do have to make due with the types that already exist and avoid any of the G_DECLARE_ constructs? I guess I will have to live with the limitations of that and see what functionality I can implement in my drop-down lists in the plugings.

If you don’t want to use boilerplate, you should probably a programming language that is not C

Maybe that should be part of the documentation :slight_smile:

I am pretty stuck with C. This year is 40 years since I learned it. It’s my only option apart from Fortran77 and I don’t ever want to go there again.

Again, it has nothing to do with the G_DECLARE macros: those are just helper macros to cut down the boilerplate and to encode some best practices. The issue is that you cannot unload the types registered by a plugin, which means if you unload a plugin that registers types you cannot reload it and re-register those types. That’s something that the GObject type system does not allow.

Now you have me really wondering. The G_DECLARE are macros and their “types” have to be in many places in the code, presumably.

If I need to unload and reload my libraries many times, how to I prevent that the types are registered again?

I know I can check if a given type name is registered but I cannot see how to make use of this. Where is the actual definition of the type taking place?

It seems like a small thing to check if a given GType name exists and not register the same type again if I knew where that happends.

You don’t. Libraries that register types are typically not going to be unloadable—anything that registers a static type (which means every single library in the GNOME stack, for instance) cannot be unloaded.

Plugins are treated specially in the GType type system, in the sense that they can register a dynamic type as a placeholder, and the type can complete its registration when the plugin has finished loading. Unloading of types has never been implemented, as it’s a massive headache to do it right when it comes to threading and multiple dependencies in a flat namespace.

That’s a source of TOCTOU bugs, and it’s never a good idea—type registration can happen in threads, or as a side effect of registering another type.

In the type system: gobject/gtype.c · main · GNOME / GLib · GitLab

Sorry, one final question:

Is there any example of how to use GTypeModule? I’ve been browsing a bunch of GNOME sources but it’s all very wrapped into a good number of layers of software that ends up in nothing I can learn anything from.

I am specficially hang up with where the GTypeModule* instance comes from initially that I am supposed to pass to g_type_register_type, which appears to be central. Is that just a cast from g_object_new()?

Also, how am I supposed to init the ->load and ->unload functions. What holds those function references?

I cannot dig this out of the documentation. I guess my (lack of) knowledge of object oriented programming does allow me to draw the obvious conclusions.

I saw that libpeas is making use of GTypeModule quite a log. Maybe I can learn something there.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.