简体   繁体   中英

g_main_loop never returns

I am using Gegl to render some output and to create a window and GLib to create an event loop. The problem I am struggling with is that the g_main_loop_run() function never returns (that is when I close the application widow (or hit Alt+F4), the code after it is not reached at all).

A minimal example, that I made up:

#include <gegl.h>
#include <stdio.h>


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

  GeglNode *gegl = gegl_node_new();
  GeglNode *text = gegl_node_new_child(gegl,
                                       "operation", "gegl:text",
                                       "size", 100.0,
                                       "color", gegl_color_new ("rgb(1.0,1.0,1.0)"),
                                       "string", "Hello",
                                       NULL);
  GeglNode *display = gegl_node_new_child(gegl,
                                          "operation", "gegl:display",
                                          NULL);

  gegl_node_link(text, display);
  gegl_node_process(display);

  GMainLoop *loop = g_main_loop_new(NULL, 0);

  printf("before\n");
  g_main_loop_run(loop);
  // the code from here on never executes
  printf("after\n");

  g_main_loop_unref(loop);

  g_object_unref(gegl);

  gegl_exit();

  return 0;
}

The code from line that says printf("after\\n"); and on never executes. Another thing that I observed was that when I used g_timeout_add_seconds() and set up a callback, which called the g_main_loop_quit , then the g_main_loop_run correctly returned and program continued as I would have expected. I also tried to use gtk_main() , but it behaved the same.

I suppose, the problem will be in some setting, that I failed to pass to GMainLoop , but I haven't found anything on Google, or in documentation ...

g_main_loop_run(loop); is a blocking call. typically it is run at the end of the application as a main event loop or in a seperate thread. so you add sources at first and do a loop run at the end so it polls on the sources and executes the callback

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM