From 440c69afa436150f2a797aa8f192d68090832c5c Mon Sep 17 00:00:00 2001 From: simonb Date: Sun, 7 Jan 2007 14:07:45 +0000 Subject: fix intrinsic problem with refcount adjust slit menus to deconstruct properly --- ChangeLog | 2 ++ src/FbTk/RefCount.hh | 57 +++++++++++++++++++++++++++++++++++----------------- src/Slit.cc | 6 +++--- src/Slit.hh | 2 +- 4 files changed, 45 insertions(+), 22 deletions(-) diff --git a/ChangeLog b/ChangeLog index ff1e160..eee970c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,6 +1,8 @@ (Format: Year/Month/Day) Changes for 1.0rc3: *07/01/07: + * Fix RefCount crash and Slit deconstruction ordering (Simon) + RefCount.hh Slit.hh/cc * Support per-window transparency settings. - new "Transparency" menu in the window menu - new apps file attribute: diff --git a/src/FbTk/RefCount.hh b/src/FbTk/RefCount.hh index ba2a697..ab4c663 100644 --- a/src/FbTk/RefCount.hh +++ b/src/FbTk/RefCount.hh @@ -25,6 +25,7 @@ namespace FbTk { /// holds a pointer with reference counting, similar to std:auto_ptr + template class RefCount { public: @@ -43,37 +44,47 @@ public: unsigned int usedBy() const { return (m_refcount != 0 ? *m_refcount : 0); } #endif private: - /// increase referense count + /// increase reference count void incRefCount(); - /// decrease referense count + /// decrease reference count void decRefCount(); + /// decrease refcount count + void decRefCountCount(); Pointer *m_data; ///< data holder mutable unsigned int *m_refcount; ///< holds reference counting + + // This one counts the number of active references pointing to the m_refcount data! + // when it reaches zero, *then* we can delete it, otherwise someone else might check it. + mutable unsigned int *m_refcount_count; ///< holds reference counting }; // implementation template -RefCount::RefCount():m_data(0), m_refcount(new unsigned int(0)) { +RefCount::RefCount():m_data(0), m_refcount(new unsigned int(0)), m_refcount_count(new unsigned int(1)) { } template RefCount::RefCount(RefCount ©): m_data(copy.m_data), - m_refcount(copy.m_refcount) { + m_refcount(copy.m_refcount), + m_refcount_count(copy.m_refcount_count) { + (*m_refcount_count)++; incRefCount(); } template -RefCount::RefCount(Pointer *p):m_data(p), m_refcount(new unsigned int(0)) { +RefCount::RefCount(Pointer *p):m_data(p), m_refcount(new unsigned int(0)), m_refcount_count(new unsigned int(1)) { incRefCount(); } template RefCount::RefCount(const RefCount ©): m_data(copy.m_data), - m_refcount(copy.m_refcount) { + m_refcount(copy.m_refcount), + m_refcount_count(copy.m_refcount_count) { + (*m_refcount_count)++; incRefCount(); } @@ -86,6 +97,8 @@ template RefCount &RefCount::operator = (const RefCount ©) { decRefCount(); // dec current ref count m_refcount = copy.m_refcount; // set new ref count + m_refcount_count = copy.m_refcount_count; + (*m_refcount_count)++; m_data = copy.m_data; // set new data pointer incRefCount(); // inc new ref count return *this; @@ -96,27 +109,35 @@ RefCount &RefCount::operator = (Pointer *p) { decRefCount(); m_data = p; // set data pointer m_refcount = new unsigned int(0); // create new counter + m_refcount_count = new unsigned int(1); incRefCount(); return *this; } template void RefCount::decRefCount() { - if (m_refcount == 0) - return; - if (*m_refcount == 0) { // already zero, then delete refcount - delete m_refcount; - m_refcount = 0; - return; + if (m_refcount != 0) { + (*m_refcount)--; + if (*m_refcount == 0) { // destroy m_data and m_refcount if nobody else is using this + if (m_data != 0) + delete m_data; + m_data = 0; + } } - (*m_refcount)--; - if (*m_refcount == 0) { // destroy m_data and m_refcount if nobody else is using this - if (m_data != 0) - delete m_data; - m_data = 0; + decRefCountCount(); +} + +template +void RefCount::decRefCountCount() { + if (*m_refcount_count == 0) + return; // shouldnt happen + (*m_refcount_count)--; + if (*m_refcount_count == 0) { delete m_refcount; - m_refcount = 0; + delete m_refcount_count; } + m_refcount = 0; + m_refcount_count = 0; } template diff --git a/src/Slit.cc b/src/Slit.cc index e9e2e91..fb12ca6 100644 --- a/src/Slit.cc +++ b/src/Slit.cc @@ -261,12 +261,12 @@ unsigned int Slit::s_eventmask = SubstructureRedirectMask | ButtonPressMask | Slit::Slit(BScreen &scr, FbTk::XLayer &layer, const char *filename) : m_hidden(false), m_screen(scr), - m_slitmenu(scr.menuTheme(), - scr.imageControl(), - *scr.layerManager().getLayer(Layer::MENU)), m_clientlist_menu(scr.menuTheme(), scr.imageControl(), *scr.layerManager().getLayer(Layer::MENU)), + m_slitmenu(scr.menuTheme(), + scr.imageControl(), + *scr.layerManager().getLayer(Layer::MENU)), frame(scr.rootWindow()), //For KDE dock applets m_kwm1_dockwindow(XInternAtom(FbTk::App::instance()->display(), diff --git a/src/Slit.hh b/src/Slit.hh index cb5b11c..ef1c58e 100644 --- a/src/Slit.hh +++ b/src/Slit.hh @@ -145,8 +145,8 @@ private: FbTk::Timer m_timer; SlitClients m_client_list; - FbMenu m_slitmenu, m_clientlist_menu; std::auto_ptr m_layermenu; + FbMenu m_clientlist_menu, m_slitmenu; std::string m_filename; struct frame { -- cgit v0.11.2