Domanda

The Question

Excerpt from the documentation:

Decreases the reference count on a GMainLoop object by one. 
If the result is zero, free the loop and free all associated memory.

I could not find information regarding this reference counter. What is it initially set to and how is it used?

Details

In particular, I'm confused about this piece of example code (in the main method) (note that set_cancel is a static method:

void (*old_sigint_handler)(int);

old_sigint_handler = signal (SIGINT, set_cancel);

 /* Create a new glib main loop */
 data.main_loop = g_main_loop_new (NULL, FALSE);

 old_sigint_handler = signal (SIGINT, set_cancel);

 /* Run the main loop */
 g_main_loop_run (data.main_loop);

 signal (SIGINT, old_sigint_handler);

 g_main_loop_unref (data.main_loop);

If g_main_loop is blocking, how it ever going to stop? I could not find information on this signal method either. But that might be native to the library (although I do not know).

Note: I reduced the code above to what I thought was the essential part. It is from a camera interface library called aravis under docs/reference/aravis/html/ArvCamera.html

È stato utile?

Soluzione

I could not find information regarding this reference counter. What is it initially set to and how is it used?

It is initially set to 1. Whenever you store a reference to the object you increment the reference counter, and whenever you remove a reference you decrement the reference counter. It's a form of manual garbage collection. Just google "reference counting" and you'll get lots of information.

If g_main_loop is blocking, how it ever going to stop?

Somewhere someone will call g_main_loop_quit. Judging by the question I'm guessing you're not very familiar with the concept of an event loop—GLib's manual isn't a very gentle introduction to the basic concept, you may want to try the Wikipedia article or just search for "event loop".

I could not find information on this signal method either. But that might be native to the library (although I do not know).

The signal is a standard function (both C and POSIX). Again, there is lots of information out there, including good old man pages (man 2 signal).

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top