salhelper/source/timer.cxx |   36 ++++++++++++++++++++++--------------
 1 file changed, 22 insertions(+), 14 deletions(-)

New commits:
commit 249fb2c6b1b8163bfa4bd6c1fcff000a42d2e967
Author:     Stephan Bergmann <[email protected]>
AuthorDate: Thu Dec 28 09:54:15 2023 +0100
Commit:     Stephan Bergmann <[email protected]>
CommitDate: Thu Dec 28 14:38:55 2023 +0100

    Keep elements of salhelper::TimerManager::m_pHead list refcounted
    
    ...in case client code would release all its references to such an element 
while
    it is still enqueued in the list
    
    Change-Id: I680aa0af878a0193a388dfe7307007f4c35a1634
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/161377
    Tested-by: Jenkins
    Reviewed-by: Stephan Bergmann <[email protected]>

diff --git a/salhelper/source/timer.cxx b/salhelper/source/timer.cxx
index 2af5b6bfdd90..3c6c0a00d922 100644
--- a/salhelper/source/timer.cxx
+++ b/salhelper/source/timer.cxx
@@ -36,7 +36,7 @@ public:
     void registerTimer(salhelper::Timer* pTimer);
 
     /// unregister timer
-    void unregisterTimer(salhelper::Timer const * pTimer);
+    void unregisterTimer(salhelper::Timer * pTimer);
 
     /// lookup timer
     bool lookupTimer(const salhelper::Timer* pTimer);
@@ -226,6 +226,8 @@ void TimerManager::registerTimer(Timer* pTimer)
     if (!pTimer)
         return;
 
+    pTimer->acquire();
+
     bool notify = false;
     {
         std::lock_guard Guard(m_Lock);
@@ -263,25 +265,33 @@ void TimerManager::registerTimer(Timer* pTimer)
     }
 }
 
-void TimerManager::unregisterTimer(Timer const * pTimer)
+void TimerManager::unregisterTimer(Timer * pTimer)
 {
     if (!pTimer)
         return;
 
-    // lock access
-    std::lock_guard Guard(m_Lock);
+    auto found = false;
+    {
+        // lock access
+        std::lock_guard Guard(m_Lock);
 
-    Timer** ppIter = &m_pHead;
+        Timer** ppIter = &m_pHead;
 
-    while (*ppIter)
-    {
-        if (pTimer == (*ppIter))
+        while (*ppIter)
         {
-            // remove timer from list
-            *ppIter = (*ppIter)->m_pNext;
-            return;
+            if (pTimer == (*ppIter))
+            {
+                // remove timer from list
+                *ppIter = (*ppIter)->m_pNext;
+                found = true;
+                break;
+            }
+            ppIter= &((*ppIter)->m_pNext);
         }
-        ppIter= &((*ppIter)->m_pNext);
+    }
+
+    if (found) {
+        pTimer->release();
     }
 }
 
@@ -320,8 +330,6 @@ void TimerManager::checkForTimeout()
     // remove expired timer
     m_pHead = pTimer->m_pNext;
 
-    pTimer->acquire();
-
     aLock.unlock();
 
     pTimer->onShot();

Reply via email to