skip to Main Content

I’m trying to learn GTK3, but the documentation is problematic at best. The most frustrating problem comes from using g_application_run. It seems to create a lag of ~25 seconds when the program is run.

Here is the example:

#include <gtk/gtk.h>

static void activate(GtkApplication *app, gpointer user_data)
{
    GtkWidget *window;

    window = gtk_application_window_new(app);
    gtk_window_set_default_size(GTK_WINDOW(window), 300, 200);
    gtk_widget_show_all(window);

exit(0);
}

int main(int argc, char **argv)
{
    GtkApplication *app;

    app = gtk_application_new("org.gtk.example", G_APPLICATION_FLAGS_NONE);
    g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
    g_application_run(G_APPLICATION(app), argc, argv);
    g_object_unref(app);

    return 0;
}

Very minimal, it creates a window, shows it and exits. Basically testing the time to load. When run with time I get the following results:

$ time ./capplication_new 

real    0m25.177s
user    0m0.151s
sys     0m0.014s

Over 25 seconds for the program to run. This is really unacceptable. What is most frustrating about this is that it’s the newest code you’re supposed to start using. But if I run this minimal example of creating a window, showing it and exiting:

#include <gtk/gtk.h>

int main (int argc, char *argv[])
{
    gtk_init(&argc, &argv);

    GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_default_size(GTK_WINDOW(window), 300, 200);
    g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
    gtk_widget_show(window);

exit(0);

    gtk_main();
}

When run with time, the results are:

$ time ./binit 

real    0m0.139s
user    0m0.108s
sys     0m0.023s

Is there a way to stop it from lagging? Why is it lagging? Why is this the new code if it lags?

**EDIT:

$ uname -a
Linux testing 5.10.0-11-amd64 #1 SMP Debian 5.10.92-1 (2022-01-18) x86_64 GNU/Linux

2

Answers


  1. Chosen as BEST ANSWER

    I didn't find out why it was happening, but I was able to find a solution.

    You need to add the following to your .xinitrc or .xsession file:

    dbus-update-activation-environment --systemd DBUS_SESSION_BUS_ADDRESS DISPLAY XAUTHORITY
    

  2. To find out why it is lagging on your machine and not on other peoples’ machines you will need to do some more debugging. I suggest starting with ltrace (which you may need to install). You can run:

    ltrace -t -f -e '@*' ./capplication_new
    

    That should show you all the library calls being made inside your application and help highlight the ones causing the delay.

    From your timings we can see that your app is spending almost all of the 25 seconds doing nothing but waiting on something else. Hopefully the ltrace will help determine what that is. When we know that, we can work out how to fix it.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search