Skip to content

Gtk3/Gtk4: avoid redundant gtk_init when GTK is already initialized by the host#25

Merged
vtrlx merged 1 commit into
vtrlx:masterfrom
lvitals:fix/skip-gtk-init-when-already-initialized
Jul 6, 2026
Merged

Gtk3/Gtk4: avoid redundant gtk_init when GTK is already initialized by the host#25
vtrlx merged 1 commit into
vtrlx:masterfrom
lvitals:fix/skip-gtk-init-when-already-initialized

Conversation

@lvitals

@lvitals lvitals commented Jul 3, 2026

Copy link
Copy Markdown

LuaGObject's override/Gtk3.lua and override/Gtk4.lua both unconditionally
call Gtk.disable_setlocale() followed by Gtk.init_check() as soon as the
module loads. This assumes LuaGObject is the one responsible for
initializing GTK.

That assumption doesn't hold when LuaGObject is loaded as a plugin/scripting
runtime inside a host application that has already called gtk_init() itself
before any plugin gets a chance to run. In that case the disable_setlocale()
call is a no-op that only produces a harmless but noisy GTK warning on every
load:

  Gtk-WARNING **: gtk_disable_setlocale() must be called before gtk_init()

This PR guards the initialization block on whether GTK has already been
initialized, checked via Gdk.Display.get_default() returning non-nil. If a
default display already exists, we assume the host already initialized GTK
and skip the redundant disable_setlocale()/init_check() calls. Otherwise,
behavior is unchanged from before.

This is a small, backwards-compatible change: LuaGObject continues to
initialize GTK itself when nothing else has, and now stays quiet when
embedded in an application that already owns GTK's lifecycle

…ialized

  Both override modules unconditionally call Gtk.disable_setlocale() and
  Gtk.init_check() on load, assuming LuaGObject itself owns GTK's
  initialization. When LuaGObject is loaded inside a host process that
  already called gtk_init() (e.g. as a plugin runtime embedded in a
  larger GTK application), this produces a harmless but noisy
  "gtk_disable_setlocale() must be called before gtk_init()" Gtk-WARNING
  on every load.

  Guard the call on Gdk.Display.get_default() already being non-nil,
  which indicates GTK has already been initialized by the host.
@vtrlx vtrlx merged commit be099a0 into vtrlx:master Jul 6, 2026
@vtrlx

vtrlx commented Jul 6, 2026

Copy link
Copy Markdown
Owner

Hi Leandro! Thank you for this change.

Incidentally, I'm curious—how did this issue come up? Are you building an application which is extended with LuaGObject? If so (and if possible), I'd be interested in seeing an example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants