Re: newbie question about g_object unref()
- From: Luis Lopez <luis lopez cuarentaydos com>
- To: David NeÄas <yeti physics muni cz>
- Cc: gtk-app-devel <gtk-app-devel-list gnome org>
- Subject: Re: newbie question about g_object unref()
- Date: Fri, 19 Nov 2010 14:44:30 +0100
El 19/11/10 14:36, David NeÄas escribiÃ:
If you want to nullify a pointer when you release your reference (which
I also find often useful as once you released your reference you may not
be longer sure the object exists at all) you should use a macro for
that, I use one defined approximately
do { if (obj) { g_object_unref(obj); (obj) = NULL; } } while (0)
which, beside nullifying the pointer, is idempotent and can also be
freely used on NULL pointer.
I use a generic solution which handles this case for strings, gobjects,
g(s)list (not deep free), etc...
#define gel_free_and_invalidate(obj,value,func) \
do { \
if (obj != value) \
{ \
func(obj); \
obj = value; \
} \
} while(0)
#define gel_free_and_invalidate_with_args(obj,value,func,...) \
do { \
if (obj != value) \
{ \
func(obj, __VA_ARGS__); \
obj = value; \
} \
} while(0)
In the case of unref and nullify a GObject you can use this:
gel_free_and_invalidate(o, NULL, g_object_unref);
for strings:
gel_free_and_invalidate(str, NULL, g_free);
g_source ids:
gel_free_and_invalidate(id, 0, g_source_remove);
etc..
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]