Can Gtk Widget creation APIs fail? (original) (raw)
December 16, 2024, 10:34am 1
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.
gwillems December 16, 2024, 11:14am 2
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?
Shyam (Shyam Butani) December 16, 2024, 11:57am 3
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.
ebassi (Emmanuele Bassi) December 16, 2024, 1:08pm 4
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.
Shyam (Shyam Butani) December 17, 2024, 5:23am 5
Thanks for the detailed explanation.
system (system) Closed January 16, 2025, 5:23am 6
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.