basctl/source/basicide/doceventnotifier.cxx |    8 +-------
 comphelper/source/misc/compbase.cxx         |   15 +++++++++++++++
 include/comphelper/compbase.hxx             |    3 +++
 3 files changed, 19 insertions(+), 7 deletions(-)

New commits:
commit fc0cb364ae479ccb98e4c064da87ad151cdb5f39
Author:     Noel Grandin <noel.gran...@collabora.co.uk>
AuthorDate: Tue Oct 1 11:39:45 2024 +0200
Commit:     Noel Grandin <noel.gran...@collabora.co.uk>
CommitDate: Tue Oct 1 15:18:21 2024 +0200

    create method for doing the "dispose-on-destruct"
    
    which is (a) a little tricky and (b) needs to be on the base class to
    get the locking just right
    
    Change-Id: I425d4747e7e0ca0741fded9a6a70f6fa9995e102
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/174312
    Tested-by: Jenkins
    Reviewed-by: Noel Grandin <noel.gran...@collabora.co.uk>

diff --git a/basctl/source/basicide/doceventnotifier.cxx 
b/basctl/source/basicide/doceventnotifier.cxx
index 9d408d7f967a..dfaa9eb13930 100644
--- a/basctl/source/basicide/doceventnotifier.cxx
+++ b/basctl/source/basicide/doceventnotifier.cxx
@@ -105,13 +105,7 @@ namespace basctl
 
     DocumentEventNotifier::Impl::~Impl ()
     {
-        std::unique_lock aGuard(m_aMutex);
-        if ( !impl_isDisposed_nothrow(aGuard) )
-        {
-            acquire();
-            aGuard.unlock(); // dispose locks m_aMutex
-            dispose();
-        }
+        disposeOnDestruct();
     }
 
     void SAL_CALL DocumentEventNotifier::Impl::documentEventOccured( const 
DocumentEvent& _rEvent )
diff --git a/comphelper/source/misc/compbase.cxx 
b/comphelper/source/misc/compbase.cxx
index 1827527d1d56..ad333acfaaa5 100644
--- a/comphelper/source/misc/compbase.cxx
+++ b/comphelper/source/misc/compbase.cxx
@@ -30,6 +30,21 @@ void SAL_CALL WeakComponentImplHelperBase::dispose()
     maEventListeners.disposeAndClear(aGuard, aEvt);
 }
 
+// This is only called from the destructor to do cleanup that
+// might not have occurred
+void WeakComponentImplHelperBase::disposeOnDestruct()
+{
+    std::unique_lock aGuard(m_aMutex);
+    assert(m_refCount == 0 && "only supposed to be called from the 
destructor");
+    if (m_bDisposed)
+        return;
+    m_bDisposed = true;
+    // bump the ref-count so we dont accidentally do a double delete
+    // if something else increases and then decreases our ref-count
+    cppu::OWeakObject::acquire();
+    disposing(aGuard);
+}
+
 void WeakComponentImplHelperBase::disposing(std::unique_lock<std::mutex>&) {}
 
 void SAL_CALL WeakComponentImplHelperBase::addEventListener(
diff --git a/include/comphelper/compbase.hxx b/include/comphelper/compbase.hxx
index c0ebf3f1ff1d..d12dedb363a2 100644
--- a/include/comphelper/compbase.hxx
+++ b/include/comphelper/compbase.hxx
@@ -56,6 +56,9 @@ protected:
         if (m_bDisposed)
             throw css::lang::DisposedException(OUString(), 
static_cast<cppu::OWeakObject*>(this));
     }
+    // only to be called from the destructor, to run the dispose logic, if 
necessary
+    void disposeOnDestruct();
+
     comphelper::OInterfaceContainerHelper4<css::lang::XEventListener> 
maEventListeners;
 };
 

Reply via email to