Can Gtk Widget creation APIs fail?

Hello,

I’m working on GTK C++ Linux application. I wanted to know if any widget creation API can fail?
If yes, in what scenarios? And can we get some error code or something?

By widget creation, I mean gtk_button_new, gtk_entry_new, gtk_fixed_new, etc.

Thanks.

Hi,

Yes, they can fail.

Typically:

  • out of memory
  • if gtk_init() failed (no display, …), then subsequent widget creation APIs will fail too

The APIs will return NULL.

Do you need to react on something specific? If yes, what’s your usecase?

1 Like

Thanks. Generally if any API fails, we try to get the info if available, nothing specific to react on.

And just for better understanding, can other APIs (e.g. gtk_widget_set_size_request) fail or it is gauranteed to be success? Most of them return void.

At a generic level, GObject creation via g_object_new() cannot fail. The constructor must return a valid GObject instance. GObject types that can fail at construction should implement the GInitable or GAsyncInitable interfaces.

Any wrapper around g_object_new(), like gtk_button_new(), is considered a C convenience API; those may fail through precondition checks on their arguments. Failures on precondition checks are considered programmer errors, and they will emit a critical warning; they are not recoverable by definition, so you won’t get an error code that you can catch.

See above: failures on precondition checks will emit a critical warning.

If a function has a recoverable error state, it will use GError in their arguments.

For more information, see the GLib documentation on error reporting.

2 Likes

Thanks for the detailed explanation.

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