Re: glib 2.34 g_main_context_unref hangs



On 01/18/2013 09:46 AM, Dan Winship wrote:
On 01/17/2013 02:48 PM, Ken Bass wrote:
On 01/17/2013 01:58 PM, Dan Winship wrote:
g_main_context_default() does not give you a ref on the default context,
so your code here is destroying an object internal to glib. If you call
this more than once (or, if you do *anything* involving the default
context after this point), I'd definitely expect it to fail.

Beyond that, it's hard to say what you might be doing wrong without
seeing more code.


Odd that it worked all these years
Yeah, probably you were just lucky before...

For some reason I was under the impression it gave me a reference,
particularly because
when I look at that default context, its ref_count is 1, not 0.
Right, it's created with a ref_count of 1 when you first call
g_main_context_default(), but every time you call it after that, it just
returns the same one again without reffing it.
I might not have been clear here. I meant the reference count was still 1 right before program exit.
My clean
up routine is the last thing executed before exit(), so
wouldn't this mean there is a memory leak?
If you are only calling the cleanup routine once, then there shouldn't
be any problem... is it possible you're freeing the GMainContext twice
somehow?
Yes, I only call the cleanup routine once. But I call:
 g_main_loop_unref(main_loop);
 g_main_context_unref(context);

g_main_loop_unref() internally does unrefs the context (which should be the default context). So in essence the above is freeing it twice. What I thought was odd was that after the g_main_loop_unref() call, the ref_count of the context was still 1. Maybe I was confused and this was invalid memory left over from before the unref/free that I am seeing in gdb.



Anyway, there is no memory "leak" (because glib still has a pointer to
the context, and could still return it it to you if you called
g_main_context_default() again). There is just memory that is left
unfreed on exit, which is not a bug. (OTOH, if you are valgrinding and
trying to get rid of all possible leaks, it is useful to be able to free
some of that stuff, and I know that freeing the default GMainContext
immediately before exit *does* work, because all of the libsoup test
programs do that. So, there may be something else going wrong in your
code, like trying to free it twice or something.)
I am pretty certain that my reason for freeing this many years ago was due to valgrind analysis and cleanup.

You say that you know freeing the default GMainContext works, does the libsoup test do both the
g_main_loop_unref(main_loop);
g_main_context_unref(default_context);

If so, that is no different that what I am doing, which would appear to be a double unref.

Thanks for your help. I ended up just checking if I was dealing with the default context and if so, do not
unref it since I do not own it. That works fine and does make sense.


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]