From 31e61c7200c7a7ec288fdc5c472118cd74e7576f Mon Sep 17 00:00:00 2001 From: Patrick Ohly Date: Wed, 21 Dec 2011 14:10:08 +0100 Subject: [PATCH] GMainLoop smart pointer: moved to GLibSupport.h Better use a shared pointer for GMainLoop. Avoids errors with ref counting when coping an eptr and boost::intrusive_ptr has the same low overhead. --- src/syncevo/GLibSupport.cpp | 5 ++++- src/syncevo/GLibSupport.h | 6 ++++++ src/syncevo/SmartPtr.h | 8 ++------ 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/syncevo/GLibSupport.cpp b/src/syncevo/GLibSupport.cpp index 4c9d3294..6c2719d4 100644 --- a/src/syncevo/GLibSupport.cpp +++ b/src/syncevo/GLibSupport.cpp @@ -251,7 +251,10 @@ class GLibTest : public CppUnit::TestFixture { list events; static const char *name = "GLibTest.out"; unlink(name); - GMainLoopPtr loop(g_main_loop_new(NULL, FALSE), "main loop"); + GMainLoopCXX loop(g_main_loop_new(NULL, FALSE), false); + if (!loop) { + SE_THROW("could not allocate main loop"); + } GLibNotify notify(name, boost::bind(notifyCallback, boost::ref(events), _1, _2, _3)); { events.clear(); diff --git a/src/syncevo/GLibSupport.h b/src/syncevo/GLibSupport.h index 0832851c..46bd9080 100644 --- a/src/syncevo/GLibSupport.h +++ b/src/syncevo/GLibSupport.h @@ -106,6 +106,12 @@ SE_END_CXX SE_GOBJECT_TYPE(GFile) SE_GOBJECT_TYPE(GFileMonitor) +void inline intrusive_ptr_add_ref(GMainLoop *ptr) { g_main_loop_ref(ptr); } +void inline intrusive_ptr_release(GMainLoop *ptr) { g_main_loop_unref(ptr); } +SE_BEGIN_CXX +typedef boost::intrusive_ptr GMainLoopCXX; +SE_END_CXX + SE_BEGIN_CXX /** diff --git a/src/syncevo/SmartPtr.h b/src/syncevo/SmartPtr.h index 59baa830..d0af9661 100644 --- a/src/syncevo/SmartPtr.h +++ b/src/syncevo/SmartPtr.h @@ -88,10 +88,6 @@ class UnrefGString { public: static void unref(gchar *ptr) { g_free(ptr); } }; -class UnrefGMainLoop { - public: - static void unref(GMainLoop *ptr) { g_main_loop_unref(ptr); } -}; #endif // HAVE_GLIB /** @@ -214,10 +210,10 @@ template class arrayptr : public eptr > { }; #ifdef HAVE_GLIB -/** eptr for glib event handle */ +/** eptr for glib event handle - not reference counted, owned by at most one instance */ typedef SmartPtr GLibEvent; typedef SmartPtr GStringPtr; -typedef SmartPtr GMainLoopPtr; +// for GMainLoop see GLibSupport.h #endif SE_END_CXX