basctl/source/basicide/unomodel.cxx              |    2 
 basic/source/basmgr/basicmanagerrepository.cxx   |    2 
 basic/source/basmgr/basmgr.cxx                   |   18 
 basic/source/uno/scriptcont.cxx                  |    2 
 editeng/source/misc/svxacorr.cxx                 |   34 -
 embeddedobj/source/inc/oleembobj.hxx             |   28 +
 embeddedobj/source/msole/olecomponent.cxx        |  529 +++++++++++++----------
 embeddedobj/source/msole/olecomponent.hxx        |   12 
 embeddedobj/source/msole/oleembed.cxx            |   28 -
 embeddedobj/source/msole/olepersist.cxx          |   19 
 embeddedobj/source/msole/olevisual.cxx           |    7 
 filter/source/msfilter/msdffimp.cxx              |   30 -
 filter/source/msfilter/msoleexp.cxx              |    8 
 filter/source/msfilter/rtfutil.cxx               |    6 
 filter/source/msfilter/svdfppt.cxx               |   22 
 filter/source/msfilter/svxmsbas2.cxx             |    8 
 fpicker/source/win32/VistaFilePickerImpl.cxx     |    1 
 include/editeng/svxacorr.hxx                     |    5 
 include/filter/msfilter/msdffimp.hxx             |    6 
 include/filter/msfilter/svdfppt.hxx              |    4 
 include/filter/msfilter/svxmsbas.hxx             |    2 
 include/oox/ole/olehelper.hxx                    |    8 
 include/sfx2/event.hxx                           |    9 
 include/sfx2/objsh.hxx                           |   13 
 include/sot/object.hxx                           |    6 
 include/sot/storage.hxx                          |    6 
 include/svx/gallerybinaryengine.hxx              |    4 
 include/vcl/scheduler.hxx                        |    7 
 include/vcl/svapp.hxx                            |   12 
 oox/source/ole/olehelper.cxx                     |   18 
 oox/source/ole/vbaexport.cxx                     |   12 
 sc/inc/global.hxx                                |    4 
 sc/inc/rangelst.hxx                              |    1 
 sc/inc/tablink.hxx                               |    7 
 sc/source/core/data/global.cxx                   |    2 
 sc/source/filter/excel/excel.cxx                 |   32 -
 sc/source/filter/excel/excimp8.cxx               |    6 
 sc/source/filter/excel/expop2.cxx                |    4 
 sc/source/filter/excel/xepivot.cxx               |    4 
 sc/source/filter/excel/xeroot.cxx                |    2 
 sc/source/filter/excel/xestream.cxx              |    4 
 sc/source/filter/excel/xiescher.cxx              |    2 
 sc/source/filter/excel/xipivot.cxx               |    4 
 sc/source/filter/excel/xiroot.cxx                |    2 
 sc/source/filter/excel/xlroot.cxx                |   12 
 sc/source/filter/ftools/ftools.cxx               |   16 
 sc/source/filter/inc/XclImpChangeTrack.hxx       |    2 
 sc/source/filter/inc/ftools.hxx                  |   10 
 sc/source/filter/inc/xcl97esc.hxx                |    2 
 sc/source/filter/inc/xeroot.hxx                  |    2 
 sc/source/filter/inc/xiescher.hxx                |    2 
 sc/source/filter/inc/xiroot.hxx                  |    2 
 sc/source/filter/inc/xlroot.hxx                  |   16 
 sc/source/filter/xcl97/XclExpChangeTrack.cxx     |    4 
 sc/source/filter/xcl97/XclImpChangeTrack.cxx     |    2 
 sc/source/filter/xcl97/xcl97rec.cxx              |    2 
 sc/source/ui/app/drwtrans.cxx                    |   15 
 sc/source/ui/app/seltrans.cxx                    |    6 
 sc/source/ui/app/transobj.cxx                    |   19 
 sc/source/ui/docshell/arealink.cxx               |    5 
 sc/source/ui/docshell/docsh4.cxx                 |    3 
 sc/source/ui/docshell/externalrefmgr.cxx         |   15 
 sc/source/ui/docshell/tablink.cxx                |   18 
 sc/source/ui/inc/docsh.hxx                       |    2 
 sc/source/ui/inc/drwtrans.hxx                    |    2 
 sc/source/ui/inc/instbdlg.hxx                    |    5 
 sc/source/ui/inc/linkarea.hxx                    |    3 
 sc/source/ui/inc/transobj.hxx                    |    2 
 sc/source/ui/miscdlgs/instbdlg.cxx               |    5 
 sc/source/ui/miscdlgs/linkarea.cxx               |    9 
 sc/source/ui/unoobj/exceldetect.cxx              |    2 
 sc/source/ui/unoobj/unodoc.cxx                   |    2 
 sc/source/ui/view/drawvie4.cxx                   |    6 
 sc/source/ui/view/viewfun3.cxx                   |    4 
 sd/inc/drawdoc.hxx                               |    2 
 sd/inc/sdfilter.hxx                              |    4 
 sd/inc/sdmod.hxx                                 |    4 
 sd/source/filter/eppt/eppt.cxx                   |   20 
 sd/source/filter/eppt/eppt.hxx                   |   10 
 sd/source/filter/eppt/epptso.cxx                 |    4 
 sd/source/filter/ppt/pptin.cxx                   |    8 
 sd/source/filter/ppt/pptin.hxx                   |    4 
 sd/source/filter/ppt/propread.hxx                |    3 
 sd/source/filter/sdpptwrp.cxx                    |   24 -
 sd/source/ui/app/sdmod.cxx                       |    4 
 sd/source/ui/app/sdmod1.cxx                      |   10 
 sd/source/ui/dlg/morphdlg.cxx                    |    4 
 sd/source/ui/dlg/vectdlg.cxx                     |    4 
 sd/source/ui/inc/DrawDocShell.hxx                |    3 
 sd/source/ui/inc/sdtreelb.hxx                    |    2 
 sd/source/ui/unoidl/sddetect.cxx                 |    2 
 sd/source/ui/unoidl/unodoc.cxx                   |    4 
 sfx2/source/appl/appcfg.cxx                      |   10 
 sfx2/source/appl/appopen.cxx                     |    3 
 sfx2/source/appl/sfxpicklist.cxx                 |    6 
 sfx2/source/appl/xpackcreator.cxx                |    4 
 sfx2/source/control/bindings.cxx                 |    1 
 sfx2/source/doc/objcont.cxx                      |    1 
 sfx2/source/doc/objxtor.cxx                      |   38 +
 sfx2/source/doc/oleprops.cxx                     |    4 
 sfx2/source/inc/objshimp.hxx                     |    4 
 sot/qa/cppunit/test_sot.cxx                      |   20 
 sot/source/base/object.cxx                       |    6 
 sot/source/sdstor/storage.cxx                    |   22 
 starmath/qa/cppunit/test_cursor.cxx              |    2 
 starmath/qa/cppunit/test_node.cxx                |    2 
 starmath/qa/cppunit/test_nodetotextvisitors.cxx  |    2 
 starmath/qa/cppunit/test_parse.cxx               |    2 
 starmath/qa/cppunit/test_starmath.cxx            |    2 
 starmath/qa/extras/mmlexport-test.cxx            |    2 
 starmath/qa/extras/mmlimport-test.cxx            |    2 
 starmath/source/document.cxx                     |    2 
 starmath/source/eqnolefilehdr.cxx                |    2 
 starmath/source/mathtype.cxx                     |   10 
 starmath/source/smdetect.cxx                     |    2 
 starmath/source/unodoc.cxx                       |    2 
 starmath/source/unofilter.cxx                    |    2 
 svtools/source/misc/imagemgr.cxx                 |    2 
 svx/source/gallery2/gallerybinaryengine.cxx      |   22 
 sw/inc/doc.hxx                                   |    2 
 sw/inc/shellio.hxx                               |   10 
 sw/qa/core/filters-test.cxx                      |    2 
 sw/qa/core/macros-test.cxx                       |    2 
 sw/qa/core/uwriter.cxx                           |    2 
 sw/qa/extras/htmlexport/htmlexport.cxx           |    6 
 sw/source/core/doc/DocumentLayoutManager.cxx     |    4 
 sw/source/core/doc/docnew.cxx                    |    4 
 sw/source/core/inc/SwXMLTextBlocks.hxx           |    4 
 sw/source/core/ole/ndole.cxx                     |    8 
 sw/source/core/swg/SwXMLTextBlocks.cxx           |   14 
 sw/source/filter/basflt/iodetect.cxx             |    4 
 sw/source/filter/basflt/shellio.cxx              |    4 
 sw/source/filter/html/htmlplug.cxx               |    2 
 sw/source/filter/html/htmlreqifreader.cxx        |   16 
 sw/source/filter/writer/writer.cxx               |    2 
 sw/source/filter/ww8/wrtw8esh.cxx                |    6 
 sw/source/filter/ww8/wrtww8.cxx                  |   12 
 sw/source/filter/ww8/wrtww8.hxx                  |    6 
 sw/source/filter/ww8/wrtww8gr.cxx                |   16 
 sw/source/filter/ww8/ww8glsy.cxx                 |    2 
 sw/source/filter/ww8/ww8glsy.hxx                 |    8 
 sw/source/filter/ww8/ww8par.cxx                  |   36 -
 sw/source/filter/ww8/ww8par.hxx                  |   10 
 sw/source/filter/ww8/ww8par4.cxx                 |   20 
 sw/source/filter/ww8/ww8par5.cxx                 |    6 
 sw/source/ui/uno/swdetect.cxx                    |    4 
 sw/source/uibase/app/apphdl.cxx                  |    2 
 sw/source/uibase/app/docsh.cxx                   |    4 
 sw/source/uibase/app/docsh2.cxx                  |    3 
 sw/source/uibase/dochdl/swdtflvr.cxx             |    4 
 sw/source/uibase/inc/glosdoc.hxx                 |    4 
 sw/source/uibase/inc/unoatxt.hxx                 |    3 
 sw/source/uibase/misc/glshell.cxx                |    4 
 sw/source/uibase/uno/unodoc.cxx                  |    6 
 sw/source/uibase/uno/unotxvw.cxx                 |    4 
 vcl/inc/svdata.hxx                               |    5 
 vcl/source/app/scheduler.cxx                     |   70 ++-
 vcl/source/app/svapp.cxx                         |    4 
 vcl/source/app/svmain.cxx                        |    3 
 vcl/win/dtrans/MtaOleClipb.cxx                   |   35 -
 vcl/win/dtrans/WinClipboard.cxx                  |   14 
 vcl/win/window/salframe.cxx                      |   10 
 writerperfect/source/common/WPXSvInputStream.cxx |   30 -
 163 files changed, 1045 insertions(+), 802 deletions(-)

New commits:
commit a02e74098cb4f23184b237b59b61297822063d9e
Author:     Mike Kaganski <mike.kagan...@collabora.com>
AuthorDate: Sun Apr 7 21:54:16 2024 +0500
Commit:     Mike Kaganski <mike.kagan...@collabora.com>
CommitDate: Thu May 16 16:40:27 2024 +0500

    Reapply "Downgrade sfx::SfxBindings aAutoTimer to an idle"
    
    This reverts commit e95ada626ccf0c1e915a1582c372242ef9b9b59a.

diff --git a/sfx2/source/control/bindings.cxx b/sfx2/source/control/bindings.cxx
index 4ea06231562a..44fbbd10c2f0 100644
--- a/sfx2/source/control/bindings.cxx
+++ b/sfx2/source/control/bindings.cxx
@@ -146,6 +146,7 @@ SfxBindings::SfxBindings()
 
     // all caches are valid (no pending invalidate-job)
     // create the list of caches
+    pImpl->aAutoTimer.SetPriority(TaskPriority::HIGH_IDLE);
     pImpl->aAutoTimer.SetInvokeHandler( LINK(this, SfxBindings, NextJob) );
 }
 
commit f3d4ada570ee7ddca243b554b4e9f1a9465a9741
Author:     Mike Kaganski <mike.kagan...@collabora.com>
AuthorDate: Sun Apr 7 21:54:16 2024 +0500
Commit:     Mike Kaganski <mike.kagan...@collabora.com>
CommitDate: Thu May 16 16:40:27 2024 +0500

    Reapply "Only hold clipboard mutexes to obtain the values, to avoid 
deadlocks"
    
    This reverts commit fb2be820d207c0090057ed6b7c24c538e1e02741.

diff --git a/vcl/win/dtrans/MtaOleClipb.cxx b/vcl/win/dtrans/MtaOleClipb.cxx
index 865025057dcc..19a37dc52e19 100644
--- a/vcl/win/dtrans/MtaOleClipb.cxx
+++ b/vcl/win/dtrans/MtaOleClipb.cxx
@@ -713,25 +713,32 @@ DWORD WINAPI 
CMtaOleClipboard::clipboardChangedNotifierThreadProc( _In_ LPVOID p
         MsgWaitForMultipleObjects(2, pInst->m_hClipboardChangedNotifierEvents, 
false, INFINITE,
                                   QS_ALLINPUT | QS_ALLPOSTMESSAGE);
 
-        ClearableMutexGuard aGuard2( pInst->m_ClipboardChangedEventCountMutex 
);
-
-        if ( pInst->m_ClipboardChangedEventCount > 0 )
+        bool hadEvents;
         {
-            pInst->m_ClipboardChangedEventCount--;
-            if ( 0 == pInst->m_ClipboardChangedEventCount )
-                ResetEvent( pInst->m_hClipboardChangedEvent );
-
-            aGuard2.clear( );
+            ClearableMutexGuard 
aGuard2(pInst->m_ClipboardChangedEventCountMutex);
+            hadEvents = pInst->m_ClipboardChangedEventCount > 0;
+            if (hadEvents)
+            {
+                pInst->m_ClipboardChangedEventCount--;
+                if (0 == pInst->m_ClipboardChangedEventCount)
+                    ResetEvent(pInst->m_hClipboardChangedEvent);
+            }
+        }
 
-            // nobody should touch m_pfncClipViewerCallback while we do
-            MutexGuard aClipViewerGuard( pInst->m_pfncClipViewerCallbackMutex 
);
+        if (hadEvents)
+        {
+            LPFNC_CLIPVIEWER_CALLBACK_t pClipViewerCallback;
+            {
+                // nobody should touch m_pfncClipViewerCallback while we do
+                // but don't hold the mutex while calling the callback itself: 
it could deadlock
+                MutexGuard 
aClipViewerGuard(pInst->m_pfncClipViewerCallbackMutex);
+                pClipViewerCallback = pInst->m_pfncClipViewerCallback;
+            }
 
             // notify all clipboard listener
-            if ( pInst->m_pfncClipViewerCallback )
-                pInst->m_pfncClipViewerCallback( );
+            if (pClipViewerCallback)
+                pClipViewerCallback();
         }
-        else
-            aGuard2.clear( );
     }
 
     return 0;
diff --git a/vcl/win/dtrans/WinClipboard.cxx b/vcl/win/dtrans/WinClipboard.cxx
index c2fa269dfc5d..33c8023a925c 100644
--- a/vcl/win/dtrans/WinClipboard.cxx
+++ b/vcl/win/dtrans/WinClipboard.cxx
@@ -373,13 +373,17 @@ void CWinClipboard::unregisterClipboardViewer() { 
m_MtaOleClipboard.registerClip
 
 void WINAPI CWinClipboard::onClipboardContentChanged()
 {
-    osl::MutexGuard aGuard(s_aClipboardSingletonMutex);
+    rtl::Reference<CWinClipboard> pWinClipboard;
+    {
+        // Only hold the mutex to obtain a safe reference to the impl, to 
avoid deadlock
+        osl::MutexGuard aGuard(s_aClipboardSingletonMutex);
+        pWinClipboard.set(s_pCWinClipbImpl);
+    }
 
-    // reassociation to instance through static member
-    if (nullptr != s_pCWinClipbImpl)
+    if (pWinClipboard)
     {
-        s_pCWinClipbImpl->m_foreignContent.clear();
-        s_pCWinClipbImpl->notifyAllClipboardListener();
+        pWinClipboard->m_foreignContent.clear();
+        pWinClipboard->notifyAllClipboardListener();
     }
 }
 
commit a5917101db50ffbc4be953e88be93bb45c408973
Author:     Mike Kaganski <mike.kagan...@collabora.com>
AuthorDate: Sun Apr 7 21:54:16 2024 +0500
Commit:     Mike Kaganski <mike.kagan...@collabora.com>
CommitDate: Thu May 16 16:40:26 2024 +0500

    Reapply "This assert was wrong"
    
    This reverts commit 5910bf0893a9c91240c40c15c41592e5650e5400.

diff --git a/embeddedobj/source/msole/olecomponent.cxx 
b/embeddedobj/source/msole/olecomponent.cxx
index 142d92ddf1c3..ca104a7c3425 100644
--- a/embeddedobj/source/msole/olecomponent.cxx
+++ b/embeddedobj/source/msole/olecomponent.cxx
@@ -200,7 +200,8 @@ private:
 
     template <typename T> void registerInterface(T* pInterface, DWORD& cookie)
     {
-        assert(cookie == 0); // do not set again
+        if (cookie != 0) // E.g., on subsequent RunObject calls
+            return;
         HRESULT hr = m_pGlobalTable->RegisterInterfaceInGlobal(pInterface, 
__uuidof(T), &cookie);
         SAL_WARN_IF(FAILED(hr), "embeddedobj.ole", "RegisterInterfaceInGlobal 
failed");
     }
commit 5c23766fc3fd52e41158c2ae9f47093e314f05cd
Author:     Mike Kaganski <mike.kagan...@collabora.com>
AuthorDate: Sun Apr 7 21:54:15 2024 +0500
Commit:     Mike Kaganski <mike.kagan...@collabora.com>
CommitDate: Thu May 16 16:40:26 2024 +0500

    Reapply "Set the condition, even when main thread message queue is empty"
    
    This reverts commit e2a97cc4719ea40b9a7f343cd000b2ae5ec6a28e.

diff --git a/vcl/source/app/scheduler.cxx b/vcl/source/app/scheduler.cxx
index 61b82d21b984..f7075584924b 100644
--- a/vcl/source/app/scheduler.cxx
+++ b/vcl/source/app/scheduler.cxx
@@ -285,6 +285,9 @@ Scheduler::IdlesLockGuard::IdlesLockGuard()
         // condition to proceed. Only main thread returning to 
Application::Execute guarantees that
         // the flag really took effect.
         pSVData->m_inExecuteCondtion.reset();
+        // Put an empty event to the application's queue, to make sure that it 
loops through the
+        // code that sets the condition, even when there's no other events in 
the queue
+        Application::PostUserEvent({});
         SolarMutexReleaser releaser;
         pSVData->m_inExecuteCondtion.wait();
     }
commit c35b555aa320ce14f634ab4a3117e37d46fe9a0b
Author:     Mike Kaganski <mike.kagan...@collabora.com>
AuthorDate: Sun Apr 7 21:54:15 2024 +0500
Commit:     Mike Kaganski <mike.kagan...@collabora.com>
CommitDate: Thu May 16 16:40:26 2024 +0500

    Reapply "Relax SolarMutexReleaser precondition to not require SolarMutex 
lock"
    
    This reverts commit 9b0756f2dbd490d8516e132954badb5d7d7c26db.

diff --git a/embeddedobj/source/msole/olecomponent.cxx 
b/embeddedobj/source/msole/olecomponent.cxx
index da10b9d9d233..142d92ddf1c3 100644
--- a/embeddedobj/source/msole/olecomponent.cxx
+++ b/embeddedobj/source/msole/olecomponent.cxx
@@ -206,15 +206,6 @@ private:
     }
 };
 
-namespace
-{
-struct SafeSolarMutexReleaser
-{
-    SolarMutexGuard guard; // To make sure we actually hold it prior to release
-    SolarMutexReleaser releaser;
-};
-}
-
 static DWORD GetAspectFromFlavor( const datatransfer::DataFlavor& aFlavor )
 {
     if ( aFlavor.MimeType.indexOf( ";Aspect=THUMBNAIL" ) != -1 )
@@ -569,7 +560,7 @@ void OleComponent::RetrieveObjectDataFlavors_Impl()
             HRESULT hr;
             sal::systools::COMReference< IEnumFORMATETC > pFormatEnum;
             {
-                SafeSolarMutexReleaser releaser;
+                SolarMutexReleaser releaser;
                 hr = pDataObject->EnumFormatEtc(DATADIR_GET, &pFormatEnum);
             }
             if ( SUCCEEDED( hr ) && pFormatEnum )
@@ -859,7 +850,7 @@ void OleComponent::InitEmbeddedCopyOfLink( 
rtl::Reference<OleComponent> const &
     // the object must be already disconnected from the temporary URL
     auto pStorage(m_pNativeImpl->CreateNewStorage(getTempURL()));
 
-    SafeSolarMutexReleaser releaser;
+    SolarMutexReleaser releaser;
 
     auto 
pDataObject(pOleLinkComponentObj.QueryInterface<IDataObject>(sal::systools::COM_QUERY));
     if ( pDataObject && SUCCEEDED( OleQueryCreateFromData( pDataObject ) ) )
@@ -987,7 +978,7 @@ void OleComponent::CloseObject()
     auto pOleObject(m_pNativeImpl->get<IOleObject>());
     if (pOleObject && OleIsRunning(pOleObject))
     {
-        SafeSolarMutexReleaser releaser;
+        SolarMutexReleaser releaser;
         HRESULT hr = pOleObject->Close(OLECLOSE_NOSAVE); // must be saved 
before
         SAL_WARN_IF(FAILED(hr), "embeddedobj.ole", "IOleObject::Close failed");
     }
@@ -1005,7 +996,7 @@ uno::Sequence< embed::VerbDescriptor > 
OleComponent::GetVerbList()
         sal::systools::COMReference< IEnumOLEVERB > pEnum;
         HRESULT hr;
         {
-            SafeSolarMutexReleaser releaser;
+            SolarMutexReleaser releaser;
             hr = pOleObject->EnumVerbs(&pEnum);
         }
         if (SUCCEEDED(hr))
@@ -1048,7 +1039,7 @@ void OleComponent::ExecuteVerb( sal_Int32 nVerbID )
     if (!pOleObject)
         throw embed::WrongStateException(); // TODO
 
-    SafeSolarMutexReleaser releaser;
+    SolarMutexReleaser releaser;
 
     // TODO: probably extents should be set here and stored in aRect
     // TODO: probably the parent window also should be set
@@ -1065,7 +1056,7 @@ void OleComponent::SetHostName( const OUString& 
aEmbDocName )
     if (!pOleObject)
         throw embed::WrongStateException(); // TODO: the object is in wrong 
state
 
-    SafeSolarMutexReleaser releaser;
+    SolarMutexReleaser releaser;
     pOleObject->SetHostNames(L"app name", o3tl::toW(aEmbDocName.getStr()));
 }
 
@@ -1081,7 +1072,7 @@ void OleComponent::SetExtent( const awt::Size& 
aVisAreaSize, sal_Int64 nAspect )
     SIZEL aSize = { aVisAreaSize.Width, aVisAreaSize.Height };
     HRESULT hr;
     {
-        SafeSolarMutexReleaser releaser;
+        SolarMutexReleaser releaser;
         hr = pOleObject->SetExtent(nMSAspect, &aSize);
     }
 
@@ -1117,7 +1108,7 @@ awt::Size OleComponent::GetExtent( sal_Int64 nAspect )
 
             HRESULT hr;
             {
-                SafeSolarMutexReleaser releaser;
+                SolarMutexReleaser releaser;
                 hr = pDataObject->GetData(&aFormat, &aMedium);
             }
 
@@ -1204,7 +1195,7 @@ awt::Size OleComponent::GetCachedExtent( sal_Int64 
nAspect )
 
     HRESULT hr;
     {
-        SafeSolarMutexReleaser releaser;
+        SolarMutexReleaser releaser;
         hr = pViewObject2->GetExtent(nMSAspect, -1, nullptr, &aSize);
     }
 
@@ -1235,7 +1226,7 @@ awt::Size OleComponent::GetRecommendedExtent( sal_Int64 
nAspect )
     SIZEL aSize;
     HRESULT hr;
     {
-        SafeSolarMutexReleaser releaser;
+        SolarMutexReleaser releaser;
         hr = pOleObject->GetExtent(nMSAspect, &aSize);
     }
     if ( FAILED( hr ) )
@@ -1256,7 +1247,7 @@ sal_Int64 OleComponent::GetMiscStatus( sal_Int64 nAspect )
 
     DWORD nResult = 0;
     {
-        SafeSolarMutexReleaser releaser;
+        SolarMutexReleaser releaser;
         pOleObject->GetMiscStatus(static_cast<DWORD>(nAspect), &nResult);
     }
     return static_cast<sal_Int64>(nResult); // first 32 bits are for MS flags
@@ -1272,7 +1263,7 @@ uno::Sequence< sal_Int8 > OleComponent::GetCLSID()
     GUID aCLSID;
     HRESULT hr;
     {
-        SafeSolarMutexReleaser releaser;
+        SolarMutexReleaser releaser;
         hr = pOleObject->GetUserClassID(&aCLSID);
     }
     if ( FAILED( hr ) )
@@ -1295,7 +1286,7 @@ bool OleComponent::IsDirty()
     if ( !pPersistStorage )
         throw io::IOException(); // TODO
 
-    SafeSolarMutexReleaser releaser;
+    SolarMutexReleaser releaser;
     HRESULT hr = pPersistStorage->IsDirty();
     return ( hr != S_FALSE );
 }
@@ -1311,7 +1302,7 @@ void OleComponent::StoreOwnTmpIfNecessary()
     if ( !pPersistStorage )
         throw io::IOException(); // TODO
 
-    SafeSolarMutexReleaser releaser;
+    SolarMutexReleaser releaser;
 
     if ( m_bWorkaroundActive || pPersistStorage->IsDirty() != S_FALSE )
     {
@@ -1573,7 +1564,7 @@ uno::Any SAL_CALL OleComponent::getTransferData( const 
datatransfer::DataFlavor&
 
                 HRESULT hr;
                 {
-                    SafeSolarMutexReleaser releaser;
+                    SolarMutexReleaser releaser;
                     hr = pDataObject->GetData(&aFormat, &aMedium);
                 }
                 if ( SUCCEEDED( hr ) )
diff --git a/fpicker/source/win32/VistaFilePickerImpl.cxx 
b/fpicker/source/win32/VistaFilePickerImpl.cxx
index 741fdadb621a..d1a0cdf8a5a9 100644
--- a/fpicker/source/win32/VistaFilePickerImpl.cxx
+++ b/fpicker/source/win32/VistaFilePickerImpl.cxx
@@ -965,7 +965,6 @@ void VistaFilePickerImpl::impl_sta_ShowDialogModal(Request& 
rRequest)
     {
         // tdf#146007: Make sure we don't hold solar mutex: COM may need to 
forward
         // the execution to the main thread, and holding solar mutex could 
deadlock
-        SolarMutexGuard g; // First acquire, to avoid releaser failure
         SolarMutexReleaser r;
         // show dialog and wait for user decision
         hResult = iDialog->Show(m_hParentWindow ? m_hParentWindow
diff --git a/include/vcl/svapp.hxx b/include/vcl/svapp.hxx
index 398358f1bfad..7aef4926f9c2 100644
--- a/include/vcl/svapp.hxx
+++ b/include/vcl/svapp.hxx
@@ -1448,8 +1448,16 @@ class SolarMutexReleaser
 {
     const sal_uInt32 mnReleased;
 public:
-    SolarMutexReleaser(): mnReleased(Application::ReleaseSolarMutex()) {}
-    ~SolarMutexReleaser() { Application::AcquireSolarMutex( mnReleased ); }
+    SolarMutexReleaser()
+        : mnReleased(
+            Application::GetSolarMutex().IsCurrentThread() ? 
Application::ReleaseSolarMutex() : 0)
+    {
+    }
+    ~SolarMutexReleaser()
+    {
+        if (mnReleased)
+            Application::AcquireSolarMutex(mnReleased);
+    }
 };
 
 VCL_DLLPUBLIC Application* GetpApp();
diff --git a/vcl/source/app/scheduler.cxx b/vcl/source/app/scheduler.cxx
index c0fc79d4ac5c..61b82d21b984 100644
--- a/vcl/source/app/scheduler.cxx
+++ b/vcl/source/app/scheduler.cxx
@@ -285,9 +285,7 @@ Scheduler::IdlesLockGuard::IdlesLockGuard()
         // condition to proceed. Only main thread returning to 
Application::Execute guarantees that
         // the flag really took effect.
         pSVData->m_inExecuteCondtion.reset();
-        std::optional<SolarMutexReleaser> releaser;
-        if (pSVData->mpDefInst->GetYieldMutex()->IsCurrentThread())
-            releaser.emplace();
+        SolarMutexReleaser releaser;
         pSVData->m_inExecuteCondtion.wait();
     }
 }
diff --git a/vcl/source/helper/threadex.cxx b/vcl/source/helper/threadex.cxx
index 1590b91f1167..cbd342bd28df 100644
--- a/vcl/source/helper/threadex.cxx
+++ b/vcl/source/helper/threadex.cxx
@@ -52,7 +52,6 @@ void SolarThreadExecutor::execute()
         m_aStart.reset();
         m_aFinish.reset();
         ImplSVEvent* nEvent = Application::PostUserEvent(LINK(this, 
SolarThreadExecutor, worker));
-        SolarMutexGuard aGuard;
         SolarMutexReleaser aReleaser;
         if (m_aStart.wait() == osl::Condition::result_timeout)
         {
commit 4b3b99960a5fdd545d660d94c12308a93c9da425
Author:     Mike Kaganski <mike.kagan...@collabora.com>
AuthorDate: Sun Apr 7 21:54:15 2024 +0500
Commit:     Mike Kaganski <mike.kagan...@collabora.com>
CommitDate: Thu May 16 16:40:26 2024 +0500

    Reapply "Pass m_pOleComponent to lambda by copy"
    
    This reverts commit 77fe60bbac09b0b4ea0cf930f2260fa4b6496a3d.

diff --git a/embeddedobj/source/msole/oleembed.cxx 
b/embeddedobj/source/msole/oleembed.cxx
index b946fed792c8..657d27710ca3 100644
--- a/embeddedobj/source/msole/oleembed.cxx
+++ b/embeddedobj/source/msole/oleembed.cxx
@@ -620,8 +620,10 @@ uno::Sequence< sal_Int32 > SAL_CALL 
OleEmbeddedObject::getReachableStates()
 
         // the list of states can only be guessed based on standard verbs,
         // since there is no way to detect what additional verbs do
+        // Pass m_pOleComponent to the lambda by copy, to make sure it doesn't 
depend on possible
+        // destruction of 'this', while the lock is unset
         return GetReachableStatesList_Impl(
-            ExecUnlocked([this] { return m_pOleComponent->GetVerbList(); }, 
aGuard));
+            ExecUnlocked([p = m_pOleComponent] { return p->GetVerbList(); }, 
aGuard));
     }
     else
 #endif
commit 48bf656dab6287d8a74d2d5ac5f9fad5c7524c63
Author:     Mike Kaganski <mike.kagan...@collabora.com>
AuthorDate: Sun Apr 7 21:54:15 2024 +0500
Commit:     Mike Kaganski <mike.kagan...@collabora.com>
CommitDate: Thu May 16 16:40:26 2024 +0500

    Reapply "Release the mutex when calling the OLE method"
    
    This reverts commit e40df1a2504935797454eafc08380b787e3629f2.

diff --git a/embeddedobj/source/inc/oleembobj.hxx 
b/embeddedobj/source/inc/oleembobj.hxx
index cf7c5ebe4ab4..983f242308cf 100644
--- a/embeddedobj/source/inc/oleembobj.hxx
+++ b/embeddedobj/source/inc/oleembobj.hxx
@@ -468,4 +468,15 @@ private:
     osl::ResettableMutexGuard& m_guard;
 };
 
+namespace
+{
+#if defined(_WIN32)
+template <class Proc> auto ExecUnlocked(Proc proc, osl::ResettableMutexGuard& 
guard)
+{
+    ClearedMutexArea area(guard);
+    return proc();
+}
+#endif
+}
+
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/embeddedobj/source/msole/oleembed.cxx 
b/embeddedobj/source/msole/oleembed.cxx
index 63d5f700b089..b946fed792c8 100644
--- a/embeddedobj/source/msole/oleembed.cxx
+++ b/embeddedobj/source/msole/oleembed.cxx
@@ -601,7 +601,7 @@ uno::Sequence< sal_Int32 > SAL_CALL 
OleEmbeddedObject::getReachableStates()
     }
     // end wrapping related part ====================
 
-    ::osl::MutexGuard aGuard( m_aMutex );
+    ::osl::ResettableMutexGuard aGuard( m_aMutex );
     if ( m_bDisposed )
         throw lang::DisposedException(); // TODO
 
@@ -620,7 +620,8 @@ uno::Sequence< sal_Int32 > SAL_CALL 
OleEmbeddedObject::getReachableStates()
 
         // the list of states can only be guessed based on standard verbs,
         // since there is no way to detect what additional verbs do
-        return GetReachableStatesList_Impl( m_pOleComponent->GetVerbList() );
+        return GetReachableStatesList_Impl(
+            ExecUnlocked([this] { return m_pOleComponent->GetVerbList(); }, 
aGuard));
     }
     else
 #endif
diff --git a/embeddedobj/source/msole/olepersist.cxx 
b/embeddedobj/source/msole/olepersist.cxx
index 381fc7b0d68c..e6af72fedccf 100644
--- a/embeddedobj/source/msole/olepersist.cxx
+++ b/embeddedobj/source/msole/olepersist.cxx
@@ -58,17 +58,6 @@
 using namespace ::com::sun::star;
 using namespace ::comphelper;
 
-namespace
-{
-#if defined(_WIN32)
-template <class Proc> auto ExecUnlocked(Proc proc, osl::ResettableMutexGuard& 
guard)
-{
-    ClearedMutexArea area(guard);
-    return proc();
-}
-#endif
-}
-
 
 bool KillFile_Impl( const OUString& aURL, const uno::Reference< 
uno::XComponentContext >& xContext )
 {
commit 205c7e9309fe25ac814ac3575129a508a70e75da
Author:     Mike Kaganski <mike.kagan...@collabora.com>
AuthorDate: Sun Apr 7 21:54:15 2024 +0500
Commit:     Mike Kaganski <mike.kagan...@collabora.com>
CommitDate: Thu May 16 16:40:26 2024 +0500

    Reapply "Make sure we hold the solar mutex before trying to release it"
    
    This reverts commit fb7612022c66781d91c782028a0d3affa578700a.

diff --git a/vcl/source/helper/threadex.cxx b/vcl/source/helper/threadex.cxx
index cbd342bd28df..1590b91f1167 100644
--- a/vcl/source/helper/threadex.cxx
+++ b/vcl/source/helper/threadex.cxx
@@ -52,6 +52,7 @@ void SolarThreadExecutor::execute()
         m_aStart.reset();
         m_aFinish.reset();
         ImplSVEvent* nEvent = Application::PostUserEvent(LINK(this, 
SolarThreadExecutor, worker));
+        SolarMutexGuard aGuard;
         SolarMutexReleaser aReleaser;
         if (m_aStart.wait() == osl::Condition::result_timeout)
         {
commit 73bf5dd3a54fe1577d64201515700204af52f07e
Author:     Mike Kaganski <mike.kagan...@collabora.com>
AuthorDate: Sun Apr 7 21:54:15 2024 +0500
Commit:     Mike Kaganski <mike.kagan...@collabora.com>
CommitDate: Thu May 16 16:40:26 2024 +0500

    Reapply "Convert an assert into a warning"
    
    This reverts commit ce79c71585f5fd5b30dc21517b405bcb37f7d142.

diff --git a/vcl/win/window/salframe.cxx b/vcl/win/window/salframe.cxx
index 6ab7bc61d673..91a4e6cc7e04 100644
--- a/vcl/win/window/salframe.cxx
+++ b/vcl/win/window/salframe.cxx
@@ -4106,8 +4106,14 @@ static void ImplHandleMoveMsg(HWND hWnd, LPARAM lParam)
 #ifdef NDEBUG
     (void) lParam;
 #endif
-    assert(IsIconic(hWnd) || (pFrame->maGeometry.x() == 
static_cast<sal_Int16>(LOWORD(lParam))));
-    assert(IsIconic(hWnd) || (pFrame->maGeometry.y() == 
static_cast<sal_Int16>(HIWORD(lParam))));
+    SAL_WARN_IF(!IsIconic(hWnd) && pFrame->maGeometry.x() != 
static_cast<sal_Int16>(LOWORD(lParam)),
+                "vcl",
+                "Unexpected X: " << pFrame->maGeometry.x() << " instead of "
+                                 << static_cast<sal_Int16>(LOWORD(lParam)));
+    SAL_WARN_IF(!IsIconic(hWnd) && pFrame->maGeometry.y() != 
static_cast<sal_Int16>(HIWORD(lParam)),
+                "vcl",
+                "Unexpected Y: " << pFrame->maGeometry.y() << " instead of "
+                                 << static_cast<sal_Int16>(HIWORD(lParam)));
 
     if (GetWindowStyle(hWnd) & WS_VISIBLE)
         pFrame->mbDefPos = false;
commit fe2fe546fe64fa96409042a6dfc2a1fe23b9d112
Author:     Mike Kaganski <mike.kagan...@collabora.com>
AuthorDate: Sun Apr 7 21:54:15 2024 +0500
Commit:     Mike Kaganski <mike.kagan...@collabora.com>
CommitDate: Thu May 16 16:40:26 2024 +0500

    Reapply "Introduce a guard to delay processing of idles"
    
    This reverts commit 330c99dd7525cf1fd20b9b8fe388e02468ee2842.

diff --git a/embeddedobj/source/inc/oleembobj.hxx 
b/embeddedobj/source/inc/oleembobj.hxx
index 274ecfaf8847..cf7c5ebe4ab4 100644
--- a/embeddedobj/source/inc/oleembobj.hxx
+++ b/embeddedobj/source/inc/oleembobj.hxx
@@ -249,7 +249,8 @@ protected:
                             const css::uno::Reference< css::embed::XStorage >& 
xStorage,
                             const OUString& sEntName,
                             const css::uno::Sequence< 
css::beans::PropertyValue >& lObjArgs,
-                            bool bSaveAs );
+                            bool bSaveAs,
+                            osl::ResettableMutexGuard& rGuard);
 #ifdef _WIN32
     /// @throws css::uno::Exception
     void StoreObjectToStream( css::uno::Reference< css::io::XOutputStream > 
const & xOutStream );
diff --git a/embeddedobj/source/msole/olepersist.cxx 
b/embeddedobj/source/msole/olepersist.cxx
index 86403f41bb3e..381fc7b0d68c 100644
--- a/embeddedobj/source/msole/olepersist.cxx
+++ b/embeddedobj/source/msole/olepersist.cxx
@@ -58,6 +58,17 @@
 using namespace ::com::sun::star;
 using namespace ::comphelper;
 
+namespace
+{
+#if defined(_WIN32)
+template <class Proc> auto ExecUnlocked(Proc proc, osl::ResettableMutexGuard& 
guard)
+{
+    ClearedMutexArea area(guard);
+    return proc();
+}
+#endif
+}
+
 
 bool KillFile_Impl( const OUString& aURL, const uno::Reference< 
uno::XComponentContext >& xContext )
 {
@@ -1059,8 +1070,11 @@ void OleEmbeddedObject::StoreToLocation_Impl(
                             const uno::Reference< embed::XStorage >& xStorage,
                             const OUString& sEntName,
                             const uno::Sequence< beans::PropertyValue >& 
lObjArgs,
-                            bool bSaveAs )
+                            bool bSaveAs, osl::ResettableMutexGuard& rGuard)
 {
+#ifndef _WIN32
+    (void)rGuard;
+#endif
     // TODO: use lObjArgs
     // TODO: exchange StoreVisualReplacement by SO file format version?
 
@@ -1110,7 +1124,7 @@ void OleEmbeddedObject::StoreToLocation_Impl(
 #ifdef _WIN32
         // if the object was NOT modified after storing it can be just copied
         // as if it was in loaded state
-      || ( m_pOleComponent && !m_pOleComponent->IsDirty() )
+        || (m_pOleComponent && !ExecUnlocked([this] { return 
m_pOleComponent->IsDirty(); }, rGuard))
 #endif
     )
     {
@@ -1482,13 +1496,13 @@ void SAL_CALL OleEmbeddedObject::storeToEntry( const 
uno::Reference< embed::XSto
     }
     // end wrapping related part ====================
 
-    ::osl::MutexGuard aGuard( m_aMutex );
+    ::osl::ResettableMutexGuard aGuard( m_aMutex );
     if ( m_bDisposed )
         throw lang::DisposedException(); // TODO
 
     VerbExecutionControllerGuard aVerbGuard( m_aVerbExecutionController );
 
-    StoreToLocation_Impl( xStorage, sEntName, lObjArgs, false );
+    StoreToLocation_Impl( xStorage, sEntName, lObjArgs, false, aGuard );
 
     // TODO: should the listener notification be done?
 }
@@ -1509,13 +1523,13 @@ void SAL_CALL OleEmbeddedObject::storeAsEntry( const 
uno::Reference< embed::XSto
     }
     // end wrapping related part ====================
 
-    ::osl::MutexGuard aGuard( m_aMutex );
+    ::osl::ResettableMutexGuard aGuard( m_aMutex );
     if ( m_bDisposed )
         throw lang::DisposedException(); // TODO
 
     VerbExecutionControllerGuard aVerbGuard( m_aVerbExecutionController );
 
-    StoreToLocation_Impl( xStorage, sEntName, lObjArgs, true );
+    StoreToLocation_Impl( xStorage, sEntName, lObjArgs, true, aGuard );
 
     // TODO: should the listener notification be done here or in saveCompleted?
 }
@@ -1691,7 +1705,7 @@ void SAL_CALL OleEmbeddedObject::storeOwn()
     // ask container to store the object, the container has to make decision
     // to do so or not
 
-    osl::ClearableMutexGuard aGuard(m_aMutex);
+    osl::ResettableMutexGuard aGuard(m_aMutex);
     if ( m_bDisposed )
         throw lang::DisposedException(); // TODO
 
@@ -1717,7 +1731,7 @@ void SAL_CALL OleEmbeddedObject::storeOwn()
     bool bStoreLoaded = true;
 
 #ifdef _WIN32
-    if ( m_nObjectState != embed::EmbedStates::LOADED && m_pOleComponent && 
m_pOleComponent->IsDirty() )
+    if ( m_nObjectState != embed::EmbedStates::LOADED && m_pOleComponent && 
ExecUnlocked([this] { return m_pOleComponent->IsDirty(); }, aGuard) )
     {
         bStoreLoaded = false;
 
diff --git a/include/vcl/scheduler.hxx b/include/vcl/scheduler.hxx
index 1b63404139bf..0181e52c33d2 100644
--- a/include/vcl/scheduler.hxx
+++ b/include/vcl/scheduler.hxx
@@ -81,6 +81,13 @@ public:
     static void       SetDeterministicMode(bool bDeterministic);
     /// Return the current state of deterministic mode.
     static bool       GetDeterministicMode();
+
+    // Makes sure that idles are not processed, until the guard is destroyed
+    struct VCL_DLLPUBLIC IdlesLockGuard final
+    {
+        IdlesLockGuard();
+        ~IdlesLockGuard();
+    };
 };
 
 #endif // INCLUDED_VCL_SCHEDULER_HXX
diff --git a/sw/source/core/doc/DocumentLayoutManager.cxx 
b/sw/source/core/doc/DocumentLayoutManager.cxx
index 75c8e86acdb2..425729833754 100644
--- a/sw/source/core/doc/DocumentLayoutManager.cxx
+++ b/sw/source/core/doc/DocumentLayoutManager.cxx
@@ -43,6 +43,7 @@
 #include <svx/svdobj.hxx>
 #include <svx/svdpage.hxx>
 #include <osl/diagnose.h>
+#include <vcl/scheduler.hxx>
 
 using namespace ::com::sun::star;
 
@@ -191,6 +192,9 @@ SwFrameFormat *DocumentLayoutManager::MakeLayoutFormat( 
RndStdIds eRequest, cons
 /// Deletes the denoted format and its content.
 void DocumentLayoutManager::DelLayoutFormat( SwFrameFormat *pFormat )
 {
+    // Do not paint, until the destruction is complete. Paint may access the 
layout and nodes
+    // while it's in inconsistent state, and crash.
+    Scheduler::IdlesLockGuard g;
     // A chain of frames needs to be merged, if necessary,
     // so that the Frame's contents are adjusted accordingly before we destroy 
the Frames.
     const SwFormatChain &rChain = pFormat->GetChain();
diff --git a/vcl/inc/svdata.hxx b/vcl/inc/svdata.hxx
index 06d0aeb9b9af..725f0cda4260 100644
--- a/vcl/inc/svdata.hxx
+++ b/vcl/inc/svdata.hxx
@@ -24,6 +24,7 @@
 
 #include <o3tl/lru_map.hxx>
 #include <o3tl/hash_combine.hxx>
+#include <osl/conditn.hxx>
 #include <tools/fldunit.hxx>
 #include <unotools/options.hxx>
 #include <vcl/bitmapex.hxx>
@@ -383,6 +384,7 @@ struct ImplSchedulerContext
     std::mutex              maMutex;                        ///< the 
"scheduler mutex" (see
                                                             ///< 
vcl/README.scheduler)
     bool                    mbActive = true;                ///< is the 
scheduler active?
+    oslInterlockedCount     mnIdlesLockCount = 0;           ///< temporary 
ignore idles
 };
 
 struct ImplSVData
@@ -424,6 +426,9 @@ struct ImplSVData
     css::uno::Reference<css::datatransfer::clipboard::XClipboard> 
m_xSystemClipboard;
 #endif
 
+    osl::Condition m_inExecuteCondtion; // Set when code returns to 
Application::Execute,
+                                        // i.e. no nested message loops run
+
     Link<LinkParamNone*,void> maDeInitHook;
 
     // LOK & headless backend specific hooks
diff --git a/vcl/source/app/scheduler.cxx b/vcl/source/app/scheduler.cxx
index 7afdfd0846e6..c0fc79d4ac5c 100644
--- a/vcl/source/app/scheduler.cxx
+++ b/vcl/source/app/scheduler.cxx
@@ -272,6 +272,32 @@ bool Scheduler::GetDeterministicMode()
     return g_bDeterministicMode;
 }
 
+Scheduler::IdlesLockGuard::IdlesLockGuard()
+{
+    ImplSVData* pSVData = ImplGetSVData();
+    ImplSchedulerContext& rSchedCtx = pSVData->maSchedCtx;
+    osl_atomic_increment(&rSchedCtx.mnIdlesLockCount);
+    if (!Application::IsMainThread())
+    {
+        // Make sure that main thread has reached the main message loop, so no 
idles are executing.
+        // It is important to ensure this, because e.g. ProcessEventsToIdle 
could be executed in a
+        // nested event loop, while an active processed idle in the main 
thread is waiting for some
+        // condition to proceed. Only main thread returning to 
Application::Execute guarantees that
+        // the flag really took effect.
+        pSVData->m_inExecuteCondtion.reset();
+        std::optional<SolarMutexReleaser> releaser;
+        if (pSVData->mpDefInst->GetYieldMutex()->IsCurrentThread())
+            releaser.emplace();
+        pSVData->m_inExecuteCondtion.wait();
+    }
+}
+
+Scheduler::IdlesLockGuard::~IdlesLockGuard()
+{
+    ImplSchedulerContext& rSchedCtx = ImplGetSVData()->maSchedCtx;
+    osl_atomic_decrement(&rSchedCtx.mnIdlesLockCount);
+}
+
 inline void Scheduler::UpdateSystemTimer( ImplSchedulerContext &rSchedCtx,
                                           const sal_uInt64 nMinPeriod,
                                           const bool bForce, const sal_uInt64 
nTime )
@@ -458,8 +484,10 @@ void Scheduler::CallbackTaskScheduling()
     // Delay invoking tasks with idle priorities as long as there are user 
input or repaint events
     // in the OS event queue. This will often effectively compress such events 
and repaint only
     // once at the end, improving performance in cases such as repeated 
zooming with a complex document.
-    bool bDelayInvoking = bIsHighPriorityIdle &&
-        Application::AnyInput( VclInputFlags::MOUSE | VclInputFlags::KEYBOARD 
| VclInputFlags::PAINT );
+    bool bDelayInvoking
+        = bIsHighPriorityIdle
+          && (rSchedCtx.mnIdlesLockCount > 0
+              || Application::AnyInput(VclInputFlags::MOUSE | 
VclInputFlags::KEYBOARD | VclInputFlags::PAINT));
 
     /*
     * Current policy is that scheduler tasks aren't allowed to throw an 
exception.
diff --git a/vcl/source/app/svapp.cxx b/vcl/source/app/svapp.cxx
index d2c6294917f0..1f7831ea060b 100644
--- a/vcl/source/app/svapp.cxx
+++ b/vcl/source/app/svapp.cxx
@@ -450,7 +450,11 @@ void Application::Execute()
             std::abort();
         }
         while (!pSVData->maAppData.mbAppQuit)
+        {
             Application::Yield();
+            SolarMutexReleaser releaser; // Give a chance for the waiting 
threads to lock the mutex
+            pSVData->m_inExecuteCondtion.set();
+        }
     }
 
     pSVData->maAppData.mbInAppExecute = false;
commit 88c4405daaf80a496b73c1500450eee17ca63590
Author:     Mike Kaganski <mike.kagan...@collabora.com>
AuthorDate: Sun Apr 7 21:54:15 2024 +0500
Commit:     Mike Kaganski <mike.kagan...@collabora.com>
CommitDate: Thu May 16 16:40:26 2024 +0500

    Reapply "Process pending events before ImplDeleteOnDeInit"
    
    This reverts commit a163ba398e760792570f01de7b62d3a3a133931c.

diff --git a/vcl/source/app/svmain.cxx b/vcl/source/app/svmain.cxx
index 7c5505f364cb..a6dc00dcd9e9 100644
--- a/vcl/source/app/svmain.cxx
+++ b/vcl/source/app/svmain.cxx
@@ -440,6 +440,9 @@ void DeInitVCL()
         pSVData->mpBlendFrameCache->m_aLastResult.Clear();
     pSVData->mbDeInit = true;
 
+    // Some events may need to access objects destroyed in ImplDeleteOnDeInit, 
so process them first
+    Scheduler::ProcessEventsToIdle();
+
     vcl::DeleteOnDeinitBase::ImplDeleteOnDeInit();
 
 #if OSL_DEBUG_LEVEL > 0
commit a16c2909e19162201dcde2e88cde7d988df3a851
Author:     Mike Kaganski <mike.kagan...@collabora.com>
AuthorDate: Sun Apr 7 21:54:15 2024 +0500
Commit:     Mike Kaganski <mike.kagan...@collabora.com>
CommitDate: Thu May 16 16:40:25 2024 +0500

    Reapply "tdf#148434 - avoid strange OS/X deadlock around AnyInput."
    
    This reverts commit 0bf6b38c1e3fbfed7512a8677260e485f4fd9d9d.

diff --git a/vcl/source/app/scheduler.cxx b/vcl/source/app/scheduler.cxx
index f83a4bb04d32..7afdfd0846e6 100644
--- a/vcl/source/app/scheduler.cxx
+++ b/vcl/source/app/scheduler.cxx
@@ -426,24 +426,6 @@ void Scheduler::CallbackTaskScheduling()
             break;
     }
 
-// tdf#148435 Apparently calling AnyInput on Mac and filtering for just input, 
gives
-// you window creation / re-sizing events which then trigger idle paint
-// events which then deadlock if called with the scheduler lock.
-// So since this is an optimisation, just don't do this on mac.
-#ifndef MACOSX
-    // Delay invoking tasks with idle priorities as long as there are user 
input or repaint events
-    // in the OS event queue. This will often effectively compress such events 
and repaint only
-    // once at the end, improving performance in cases such as repeated 
zooming with a complex document.
-    if ( pMostUrgent && pMostUrgent->mePriority >= TaskPriority::HIGH_IDLE
-        && Application::AnyInput( VclInputFlags::MOUSE | 
VclInputFlags::KEYBOARD | VclInputFlags::PAINT ))
-    {
-        SAL_INFO( "vcl.schedule", tools::Time::GetSystemTicks()
-            << " idle priority task " << pMostUrgent << " delayed, system 
events pending" );
-        pMostUrgent = nullptr;
-        nMinPeriod = 0;
-    }
-#endif
-
     if (InfiniteTimeoutMs != nMinPeriod)
         SAL_INFO("vcl.schedule",
                  "Calculated minimum timeout as " << nMinPeriod << " of " << 
nTasks << " tasks");
@@ -459,9 +441,6 @@ void Scheduler::CallbackTaskScheduling()
 
     comphelper::ProfileZone aZone( pTask->GetDebugName() );
 
-    // prepare Scheduler object for deletion after handling
-    pTask->SetDeletionFlags();
-
     assert(!pMostUrgent->mbInScheduler);
     pMostUrgent->mbInScheduler = true;
 
@@ -471,8 +450,17 @@ void Scheduler::CallbackTaskScheduling()
     rSchedCtx.mpSchedulerStack = pMostUrgent;
     rSchedCtx.mpSchedulerStackTop = pMostUrgent;
 
+    bool bIsHighPriorityIdle = pMostUrgent->mePriority >= 
TaskPriority::HIGH_IDLE;
+
     // invoke the task
     Unlock();
+
+    // Delay invoking tasks with idle priorities as long as there are user 
input or repaint events
+    // in the OS event queue. This will often effectively compress such events 
and repaint only
+    // once at the end, improving performance in cases such as repeated 
zooming with a complex document.
+    bool bDelayInvoking = bIsHighPriorityIdle &&
+        Application::AnyInput( VclInputFlags::MOUSE | VclInputFlags::KEYBOARD 
| VclInputFlags::PAINT );
+
     /*
     * Current policy is that scheduler tasks aren't allowed to throw an 
exception.
     * Because otherwise the exception is caught somewhere totally unrelated.
@@ -482,7 +470,16 @@ void Scheduler::CallbackTaskScheduling()
     */
     try
     {
-        pTask->Invoke();
+        if (bDelayInvoking)
+            SAL_INFO( "vcl.schedule", tools::Time::GetSystemTicks()
+                      << " idle priority task " << pTask->GetDebugName()
+                      << " delayed, system events pending" );
+        else
+        {
+            // prepare Scheduler object for deletion after handling
+            pTask->SetDeletionFlags();
+            pTask->Invoke();
+        }
     }
     catch (css::uno::Exception&)
     {
commit 32ac96f39a13d18e6d03bab91c2e911b16a0d4cf
Author:     Mike Kaganski <mike.kagan...@collabora.com>
AuthorDate: Sun Apr 7 21:54:14 2024 +0500
Commit:     Mike Kaganski <mike.kagan...@collabora.com>
CommitDate: Thu May 16 16:40:25 2024 +0500

    Reapply "Disallow closing document during generation of preview"
    
    This reverts commit 2bf752ad5912fc478a9bb036556bd7b224f0e19c.

diff --git a/include/sfx2/objsh.hxx b/include/sfx2/objsh.hxx
index 5d98cb0409af..d81727b4634f 100644
--- a/include/sfx2/objsh.hxx
+++ b/include/sfx2/objsh.hxx
@@ -821,6 +821,15 @@ public:
         }
     };
 
+class SfxCloseVetoLock
+{
+public:
+    SfxCloseVetoLock(const SfxObjectShell& rDocShell);
+    ~SfxCloseVetoLock();
+
+private:
+    const SfxObjectShell& m_rDocShell;
+};
 
 typedef rtl::Reference<SfxObjectShell> SfxObjectShellRef;
 
diff --git a/sfx2/source/doc/objcont.cxx b/sfx2/source/doc/objcont.cxx
index caf222c61d2d..a3b5226917d3 100644
--- a/sfx2/source/doc/objcont.cxx
+++ b/sfx2/source/doc/objcont.cxx
@@ -104,6 +104,7 @@ SfxObjectShell::GetPreviewMetaFile( bool bFullContent ) 
const
 
 BitmapEx SfxObjectShell::GetPreviewBitmap() const
 {
+    SfxCloseVetoLock lock(*this);
     ScopedVclPtrInstance< VirtualDevice > pDevice;
     pDevice->SetAntialiasing(AntialiasingFlags::Enable | 
pDevice->GetAntialiasing());
     if(!CreatePreview_Impl(/*bFullContent*/false, pDevice, nullptr))
diff --git a/sfx2/source/doc/objxtor.cxx b/sfx2/source/doc/objxtor.cxx
index ae6f713251ea..94c5cf54f600 100644
--- a/sfx2/source/doc/objxtor.cxx
+++ b/sfx2/source/doc/objxtor.cxx
@@ -28,6 +28,7 @@
 #include <com/sun/star/util/XCloseable.hpp>
 #include <com/sun/star/frame/XComponentLoader.hpp>
 #include <com/sun/star/frame/Desktop.hpp>
+#include <com/sun/star/util/CloseVetoException.hpp>
 #include <com/sun/star/util/XCloseListener.hpp>
 #include <com/sun/star/beans/XPropertySet.hpp>
 #include <com/sun/star/frame/XTitle.hpp>
@@ -135,8 +136,14 @@ public:
 
 } // namespace
 
-void SAL_CALL SfxModelListener_Impl::queryClosing( const 
css::lang::EventObject& , sal_Bool )
+void SAL_CALL SfxModelListener_Impl::queryClosing( const 
css::lang::EventObject& , sal_Bool bDeliverOwnership)
 {
+    if (mpDoc->Get_Impl()->m_nClosingLockLevel)
+    {
+        if (bDeliverOwnership)
+            mpDoc->Get_Impl()->m_bCloseModelScheduled = true;
+        throw util::CloseVetoException("Closing document is blocked", {});
+    }
 }
 
 void SAL_CALL SfxModelListener_Impl::notifyClosing( const 
css::lang::EventObject& )
@@ -301,8 +308,6 @@ SfxObjectShell::~SfxObjectShell()
     if ( pSfxApp && pSfxApp->GetDdeService() )
         pSfxApp->RemoveDdeTopic( this );
 
-    pImpl->pBaseModel.set( nullptr );
-
     // don't call GetStorage() here, in case of Load Failure it's possible 
that a storage was never assigned!
     if ( pMedium && pMedium->HasStorage_Impl() && pMedium->GetStorage( false ) 
== pImpl->m_xDocStorage )
         pMedium->CanDisposeStorage_Impl( false );
@@ -337,6 +342,33 @@ SfxObjectShell::~SfxObjectShell()
     }
 }
 
+SfxCloseVetoLock::SfxCloseVetoLock(const SfxObjectShell& rDocShell)
+    : m_rDocShell(rDocShell)
+{
+    osl_atomic_increment(&m_rDocShell.Get_Impl()->m_nClosingLockLevel);
+}
+
+SfxCloseVetoLock::~SfxCloseVetoLock()
+{
+    if (osl_atomic_decrement(&m_rDocShell.Get_Impl()->m_nClosingLockLevel) == 
0)
+    {
+        if (m_rDocShell.Get_Impl()->m_bCloseModelScheduled)
+        {
+            m_rDocShell.Get_Impl()->m_bCloseModelScheduled = false; // pass 
ownership
+            if (rtl::Reference model = 
static_cast<SfxBaseModel*>(m_rDocShell.GetBaseModel().get()))
+            {
+                try
+                {
+                    model->close(true);
+                }
+                catch (const util::CloseVetoException&)
+                {
+                    DBG_UNHANDLED_EXCEPTION("sfx.doc");
+                }
+            }
+        }
+    }
+}
 
 void SfxObjectShell::Stamp_SetPrintCancelState(bool bState)
 {
diff --git a/sfx2/source/inc/objshimp.hxx b/sfx2/source/inc/objshimp.hxx
index e5f67506cfda..cec0947cc3a9 100644
--- a/sfx2/source/inc/objshimp.hxx
+++ b/sfx2/source/inc/objshimp.hxx
@@ -135,6 +135,9 @@ struct SfxObjectShell_Impl final : public 
::sfx2::IMacroDocumentAccess
     /// Holds Infobars until View is fully loaded
     std::vector<InfobarData> m_aPendingInfobars;
 
+    mutable oslInterlockedCount m_nClosingLockLevel = 0;
+    mutable bool m_bCloseModelScheduled = false;
+
     SfxObjectShell_Impl( SfxObjectShell& _rDocShell );
     virtual ~SfxObjectShell_Impl();
 
commit b6c65fd0965c4169bc997a3495b7f2aea6076f8b
Author:     Mike Kaganski <mike.kagan...@collabora.com>
AuthorDate: Sun Apr 7 21:54:14 2024 +0500
Commit:     Mike Kaganski <mike.kagan...@collabora.com>
CommitDate: Thu May 16 16:40:25 2024 +0500

    Reapply "Use weak reference to SfxObjectShell in SfxEventHint to avoid 
use-after-free"
    
    This reverts commit 51c7be65ddd3de7ba98212e9f23230b4694b5e3e.

diff --git a/basctl/source/basicide/unomodel.cxx 
b/basctl/source/basicide/unomodel.cxx
index 4a9ee759f060..d4ce9544ce2d 100644
--- a/basctl/source/basicide/unomodel.cxx
+++ b/basctl/source/basicide/unomodel.cxx
@@ -123,7 +123,7 @@ com_sun_star_comp_basic_BasicID_get_implementation(
 {
     SolarMutexGuard aGuard;
     basctl::EnsureIde();
-    SfxObjectShell* pShell = new basctl::DocShell();
+    rtl::Reference<SfxObjectShell> pShell = new basctl::DocShell();
     auto pModel = pShell->GetModel();
     pModel->acquire();
     return pModel.get();
diff --git a/basic/source/basmgr/basicmanagerrepository.cxx 
b/basic/source/basmgr/basicmanagerrepository.cxx
index 7a2ed5efd20a..4ece6f6e2c7f 100644
--- a/basic/source/basmgr/basicmanagerrepository.cxx
+++ b/basic/source/basmgr/basicmanagerrepository.cxx
@@ -430,7 +430,7 @@ namespace basic
             OUString aAppBasicDir = SvtPathOptions().GetBasicPath();
 
             // Storage and BaseURL are only needed by binary documents!
-            tools::SvRef<SotStorage> xDummyStor = new SotStorage( OUString() );
+            rtl::Reference<SotStorage> xDummyStor = new SotStorage(OUString());
             _out_rpBasicManager.reset(new BasicManager( *xDummyStor, u"" /* 
TODO/LATER: xStorage */,
                                                                 pAppBasic,
                                                                 &aAppBasicDir, 
true ));
diff --git a/basic/source/basmgr/basmgr.cxx b/basic/source/basmgr/basmgr.cxx
index 72776dd08153..71a937f81b5f 100644
--- a/basic/source/basmgr/basmgr.cxx
+++ b/basic/source/basmgr/basmgr.cxx
@@ -635,7 +635,7 @@ void BasicManager::ImpCreateStdLib( StarBASIC* 
pParentFromStdLib )
 
 void BasicManager::LoadBasicManager( SotStorage& rStorage, std::u16string_view 
rBaseURL )
 {
-    tools::SvRef<SotStorageStream> xManagerStream = rStorage.OpenSotStream( 
szManagerStream, eStreamReadMode );
+    rtl::Reference<SotStorageStream> xManagerStream = rStorage.OpenSotStream( 
szManagerStream, eStreamReadMode );
 
     OUString aStorName( rStorage.GetName() );
     // #i13114 removed, DBG_ASSERT( aStorName.Len(), "No Storage Name!" );
@@ -727,7 +727,7 @@ void BasicManager::LoadBasicManager( SotStorage& rStorage, 
std::u16string_view r
 
 void BasicManager::LoadOldBasicManager( SotStorage& rStorage )
 {
-    tools::SvRef<SotStorageStream> xManagerStream = rStorage.OpenSotStream( 
szOldManagerStream, eStreamReadMode );
+    rtl::Reference<SotStorageStream> xManagerStream = rStorage.OpenSotStream( 
szOldManagerStream, eStreamReadMode );
 
     OUString aStorName( rStorage.GetName() );
     DBG_ASSERT( aStorName.getLength(), "No Storage Name!" );
@@ -780,7 +780,7 @@ void BasicManager::LoadOldBasicManager( SotStorage& 
rStorage )
         aLibRelStorage = aLibRelStorage.smartRel2Abs( aLibRelStorageName, 
bWasAbsolute);
         DBG_ASSERT(!bWasAbsolute, "RelStorageName was absolute!" );
 
-        tools::SvRef<SotStorage> xStorageRef;
+        rtl::Reference<SotStorage> xStorageRef;
         if ( aLibAbsStorage == aCurStorage || aLibRelStorageName == szImbedded 
)
         {
             xStorageRef = &rStorage;
@@ -841,7 +841,7 @@ bool BasicManager::ImpLoadLibrary( BasicLibInfo* pLibInfo, 
SotStorage* pCurStora
     {
         aStorageName = GetStorageName();
     }
-    tools::SvRef<SotStorage> xStorage;
+    rtl::Reference<SotStorage> xStorage;
     // The current must not be opened again...
     if ( pCurStorage )
     {
@@ -864,7 +864,7 @@ bool BasicManager::ImpLoadLibrary( BasicLibInfo* pLibInfo, 
SotStorage* pCurStora
     {
         xStorage = new SotStorage( false, aStorageName, eStorageReadMode );
     }
-    tools::SvRef<SotStorage> xBasicStorage = xStorage->OpenSotStorage( 
szBasicStorage, eStorageReadMode, false );
+    rtl::Reference<SotStorage> xBasicStorage = xStorage->OpenSotStorage( 
szBasicStorage, eStorageReadMode, false );
 
     if ( !xBasicStorage.is() || xBasicStorage->GetError() )
     {
@@ -874,7 +874,7 @@ bool BasicManager::ImpLoadLibrary( BasicLibInfo* pLibInfo, 
SotStorage* pCurStora
     else
     {
         // In the Basic-Storage every lib is in a Stream...
-        tools::SvRef<SotStorageStream> xBasicStream = 
xBasicStorage->OpenSotStream( pLibInfo->GetLibName(), eStreamReadMode );
+        rtl::Reference<SotStorageStream> xBasicStream = 
xBasicStorage->OpenSotStream( pLibInfo->GetLibName(), eStreamReadMode );
         if ( !xBasicStream.is() || xBasicStream->GetError() )
         {
             StringErrorInfo* pErrInf = new StringErrorInfo( 
ERRCODE_BASMGR_LIBLOAD , pLibInfo->GetLibName(), DialogMask::ButtonsOk );
@@ -1096,7 +1096,7 @@ bool BasicManager::RemoveLib( sal_uInt16 nLib, bool 
bDelBasicFromStorage )
     if (bDelBasicFromStorage && !(*itLibInfo)->IsReference() &&
            (!(*itLibInfo)->IsExtern() || 
SotStorage::IsStorageFile((*itLibInfo)->GetStorageName())))
     {
-        tools::SvRef<SotStorage> xStorage;
+        rtl::Reference<SotStorage> xStorage;
         try
         {
             if (!(*itLibInfo)->IsExtern())
@@ -1115,7 +1115,7 @@ bool BasicManager::RemoveLib( sal_uInt16 nLib, bool 
bDelBasicFromStorage )
 
         if (xStorage.is() && xStorage->IsStorage(szBasicStorage))
         {
-            tools::SvRef<SotStorage> xBasicStorage = xStorage->OpenSotStorage
+            rtl::Reference<SotStorage> xBasicStorage = xStorage->OpenSotStorage
                             ( szBasicStorage, StreamMode::STD_READWRITE, false 
);
 
             if ( !xBasicStorage.is() || xBasicStorage->GetError() )
@@ -1289,7 +1289,7 @@ StarBASIC* BasicManager::CreateLib( const OUString& 
rLibName, const OUString& Pa
         {
             try
             {
-                tools::SvRef<SotStorage> xStorage = new SotStorage(false, 
LinkTargetURL, StreamMode::READ | StreamMode::SHARE_DENYWRITE);
+                rtl::Reference<SotStorage> xStorage = new SotStorage(false, 
LinkTargetURL, StreamMode::READ | StreamMode::SHARE_DENYWRITE);
                 if (!xStorage->GetError())
                 {
                     pLib = AddLib(*xStorage, rLibName, true);
diff --git a/basic/source/uno/scriptcont.cxx b/basic/source/uno/scriptcont.cxx
index eea449ac47fc..3956c001ca31 100644
--- a/basic/source/uno/scriptcont.cxx
+++ b/basic/source/uno/scriptcont.cxx
@@ -334,7 +334,7 @@ rtl::Reference<SfxLibraryContainer> 
SfxScriptLibraryContainer::createInstanceImp
 void SfxScriptLibraryContainer::importFromOldStorage( const OUString& aFile )
 {
     // TODO: move loading from old storage to binary filters?
-    auto xStorage = tools::make_ref<SotStorage>( false, aFile );
+    rtl::Reference<SotStorage> xStorage(new SotStorage(false, aFile));
     if( xStorage->GetError() == ERRCODE_NONE )
     {
         auto pBasicManager = std::make_unique<BasicManager> ( *xStorage, aFile 
);
diff --git a/editeng/source/misc/svxacorr.cxx b/editeng/source/misc/svxacorr.cxx
index 054011e2a390..c8ed7a6113b2 100644
--- a/editeng/source/misc/svxacorr.cxx
+++ b/editeng/source/misc/svxacorr.cxx
@@ -2228,7 +2228,7 @@ bool SvxAutoCorrectLanguageLists::IsFileChanged_Imp()
 void SvxAutoCorrectLanguageLists::LoadXMLExceptList_Imp(
                                         std::unique_ptr<SvStringsISortDtor>& 
rpLst,
                                         const OUString& sStrmName,
-                                        tools::SvRef<SotStorage>& rStg)
+                                        rtl::Reference<SotStorage>& rStg)
 {
     if( rpLst )
         rpLst->clear();
@@ -2238,7 +2238,7 @@ void SvxAutoCorrectLanguageLists::LoadXMLExceptList_Imp(
     {
         if( rStg.is() && rStg->IsStream( sStrmName ) )
         {
-            tools::SvRef<SotStorageStream> xStrm = rStg->OpenSotStream( 
sStrmName,
+            rtl::Reference<SotStorageStream> xStrm = rStg->OpenSotStream( 
sStrmName,
                 ( StreamMode::READ | StreamMode::SHARE_DENYWRITE | 
StreamMode::NOCREATE ) );
             if( ERRCODE_NONE != xStrm->GetError())
             {
@@ -2299,7 +2299,7 @@ void SvxAutoCorrectLanguageLists::LoadXMLExceptList_Imp(
 void SvxAutoCorrectLanguageLists::SaveExceptList_Imp(
                             const SvStringsISortDtor& rLst,
                             const OUString& sStrmName,
-                            tools::SvRef<SotStorage> const &rStg,
+                            rtl::Reference<SotStorage> const &rStg,
                             bool bConvert )
 {
     if( !rStg.is() )
@@ -2312,7 +2312,7 @@ void SvxAutoCorrectLanguageLists::SaveExceptList_Imp(
     }
     else
     {
-        tools::SvRef<SotStorageStream> xStrm = rStg->OpenSotStream( sStrmName,
+        rtl::Reference<SotStorageStream> xStrm = rStg->OpenSotStream( 
sStrmName,
                 ( StreamMode::READ | StreamMode::WRITE | 
StreamMode::SHARE_DENYWRITE ) );
         if( xStrm.is() )
         {
@@ -2431,7 +2431,7 @@ bool 
SvxAutoCorrectLanguageLists::AddToCplSttExceptList(const OUString& rNew)
     if( !rNew.isEmpty() && GetCplSttExceptList()->insert( rNew ).second )
     {
         MakeUserStorage_Impl();
-        tools::SvRef<SotStorage> xStg = new SotStorage( sUserAutoCorrFile, 
StreamMode::READWRITE );
+        rtl::Reference<SotStorage> xStg = new SotStorage(sUserAutoCorrFile, 
StreamMode::READWRITE);
 
         SaveExceptList_Imp( *pCplStt_ExcptLst, pXMLImplCplStt_ExcptLstStr, 
xStg );
 
@@ -2451,7 +2451,7 @@ bool 
SvxAutoCorrectLanguageLists::AddToWordStartExceptList(const OUString& rNew)
     if( !rNew.isEmpty() && GetWordStartExceptList()->insert( rNew ).second )
     {
         MakeUserStorage_Impl();
-        tools::SvRef<SotStorage> xStg = new SotStorage( sUserAutoCorrFile, 
StreamMode::READWRITE );
+        rtl::Reference<SotStorage> xStg = new SotStorage(sUserAutoCorrFile, 
StreamMode::READWRITE);
 
         SaveExceptList_Imp( *pWordStart_ExcptLst, 
pXMLImplWordStart_ExcptLstStr, xStg );
 
@@ -2469,7 +2469,7 @@ SvStringsISortDtor* 
SvxAutoCorrectLanguageLists::LoadCplSttExceptList()
 {
     try
     {
-        tools::SvRef<SotStorage> xStg = new SotStorage( sShareAutoCorrFile, 
StreamMode::READ | StreamMode::SHARE_DENYNONE );
+        rtl::Reference<SotStorage> xStg = new SotStorage( sShareAutoCorrFile, 
StreamMode::READ | StreamMode::SHARE_DENYNONE );
         if( xStg.is() && xStg->IsContained( pXMLImplCplStt_ExcptLstStr ) )
             LoadXMLExceptList_Imp( pCplStt_ExcptLst, 
pXMLImplCplStt_ExcptLstStr, xStg );
     }
@@ -2482,7 +2482,7 @@ SvStringsISortDtor* 
SvxAutoCorrectLanguageLists::LoadCplSttExceptList()
 void SvxAutoCorrectLanguageLists::SaveCplSttExceptList()
 {
     MakeUserStorage_Impl();
-    tools::SvRef<SotStorage> xStg = new SotStorage( sUserAutoCorrFile, 
StreamMode::READWRITE );
+    rtl::Reference<SotStorage> xStg = new SotStorage(sUserAutoCorrFile, 
StreamMode::READWRITE);
 
     SaveExceptList_Imp( *pCplStt_ExcptLst, pXMLImplCplStt_ExcptLstStr, xStg );
 
@@ -2498,7 +2498,7 @@ SvStringsISortDtor* 
SvxAutoCorrectLanguageLists::LoadWordStartExceptList()
 {
     try
     {
-        tools::SvRef<SotStorage> xStg = new SotStorage( sShareAutoCorrFile, 
StreamMode::READ | StreamMode::SHARE_DENYNONE );
+        rtl::Reference<SotStorage> xStg = new SotStorage( sShareAutoCorrFile, 
StreamMode::READ | StreamMode::SHARE_DENYNONE );
         if( xStg.is() && xStg->IsContained( pXMLImplWordStart_ExcptLstStr ) )
             LoadXMLExceptList_Imp( pWordStart_ExcptLst, 
pXMLImplWordStart_ExcptLstStr, xStg );
     }
@@ -2512,7 +2512,7 @@ SvStringsISortDtor* 
SvxAutoCorrectLanguageLists::LoadWordStartExceptList()
 void SvxAutoCorrectLanguageLists::SaveWordStartExceptList()
 {
     MakeUserStorage_Impl();
-    tools::SvRef<SotStorage> xStg = new SotStorage( sUserAutoCorrFile, 
StreamMode::READWRITE );
+    rtl::Reference<SotStorage> xStg = new SotStorage(sUserAutoCorrFile, 
StreamMode::READWRITE);
 
     SaveExceptList_Imp( *pWordStart_ExcptLst, pXMLImplWordStart_ExcptLstStr, 
xStg );
 
@@ -2542,7 +2542,7 @@ void SvxAutoCorrectLanguageLists::RemoveStream_Imp( const 
OUString& rName )
 {
     if( sShareAutoCorrFile != sUserAutoCorrFile )
     {
-        tools::SvRef<SotStorage> xStg = new SotStorage( sUserAutoCorrFile, 
StreamMode::READWRITE );
+        rtl::Reference<SotStorage> xStg = new SotStorage(sUserAutoCorrFile, 
StreamMode::READWRITE);
         if( xStg.is() && ERRCODE_NONE == xStg->GetError() &&
             xStg->IsStream( rName ) )
         {
@@ -2604,8 +2604,8 @@ void SvxAutoCorrectLanguageLists::MakeUserStorage_Impl()
     }
     if (bConvert && !bError)
     {
-        tools::SvRef<SotStorage> xSrcStg = new SotStorage( aDest.GetMainURL( 
INetURLObject::DecodeMechanism::ToIUri ), StreamMode::READ );
-        tools::SvRef<SotStorage> xDstStg = new SotStorage( sUserAutoCorrFile, 
StreamMode::WRITE );
+        rtl::Reference<SotStorage> xSrcStg = new SotStorage( aDest.GetMainURL( 
INetURLObject::DecodeMechanism::ToIUri ), StreamMode::READ );
+        rtl::Reference<SotStorage> xDstStg = new SotStorage(sUserAutoCorrFile, 
StreamMode::WRITE);
 
         if( xSrcStg.is() && xDstStg.is() )
         {
@@ -2653,7 +2653,7 @@ bool SvxAutoCorrectLanguageLists::MakeBlocklist_Imp( 
SotStorage& rStg )
     bool bRet = true, bRemove = !pAutocorr_List || pAutocorr_List->empty();
     if( !bRemove )
     {
-        tools::SvRef<SotStorageStream> refList = rStg.OpenSotStream( 
pXMLImplAutocorr_ListStr,
+        rtl::Reference<SotStorageStream> refList = rStg.OpenSotStream( 
pXMLImplAutocorr_ListStr,
                     ( StreamMode::READ | StreamMode::WRITE | 
StreamMode::SHARE_DENYWRITE ) );
         if( refList.is() )
         {
@@ -2704,7 +2704,7 @@ bool SvxAutoCorrectLanguageLists::MakeCombinedChanges( 
std::vector<SvxAutocorrWo
     GetAutocorrWordList();
 
     MakeUserStorage_Impl();
-    tools::SvRef<SotStorage> xStorage = new SotStorage( sUserAutoCorrFile, 
StreamMode::READWRITE );
+    rtl::Reference<SotStorage> xStorage = new SotStorage(sUserAutoCorrFile, 
StreamMode::READWRITE);
 
     bool bRet = xStorage.is() && ERRCODE_NONE == xStorage->GetError();
 
@@ -2773,7 +2773,7 @@ bool SvxAutoCorrectLanguageLists::PutText( const 
OUString& rShort, const OUStrin
     GetAutocorrWordList();
 
     MakeUserStorage_Impl();
-    tools::SvRef<SotStorage> xStg = new SotStorage( sUserAutoCorrFile, 
StreamMode::READWRITE );
+    rtl::Reference<SotStorage> xStg = new SotStorage(sUserAutoCorrFile, 
StreamMode::READWRITE);
 
     bool bRet = xStg.is() && ERRCODE_NONE == xStg->GetError();
 
@@ -2831,7 +2831,7 @@ void SvxAutoCorrectLanguageLists::PutText( const 
OUString& rShort,
         {
             if( pAutocorr_List->Insert( SvxAutocorrWord(rShort, sLong, false) 
) )
             {
-                tools::SvRef<SotStorage> xStor = new SotStorage( 
sUserAutoCorrFile, StreamMode::READWRITE );
+                rtl::Reference<SotStorage> xStor = new SotStorage( 
sUserAutoCorrFile, StreamMode::READWRITE );
                 MakeBlocklist_Imp( *xStor );
             }
         }
diff --git a/filter/source/msfilter/msdffimp.cxx 
b/filter/source/msfilter/msdffimp.cxx
index cdcad2778f6f..bfd141b4dc73 100644
--- a/filter/source/msfilter/msdffimp.cxx
+++ b/filter/source/msfilter/msdffimp.cxx
@@ -6756,7 +6756,7 @@ void SvxMSDffManager::ProcessClientAnchor2( SvStream& /* 
rSt */, DffRecordHeader
     // will be overridden by SJ in Draw
 }
 
-bool SvxMSDffManager::GetOLEStorageName( sal_uInt32, OUString&, 
tools::SvRef<SotStorage>&, uno::Reference < embed::XStorage >& ) const
+bool SvxMSDffManager::GetOLEStorageName( sal_uInt32, OUString&, 
rtl::Reference<SotStorage>&, uno::Reference < embed::XStorage >& ) const
 {
     return false;
 }
@@ -6775,7 +6775,7 @@ rtl::Reference<SdrObject> SvxMSDffManager::ImportOLE( 
sal_uInt32 nOLEId,
 {
     rtl::Reference<SdrObject> pRet;
     OUString sStorageName;
-    tools::SvRef<SotStorage> xSrcStg;
+    rtl::Reference<SotStorage> xSrcStg;
     ErrCode nError = ERRCODE_NONE;
     uno::Reference < embed::XStorage > xDstStg;
     if( GetOLEStorageName( nOLEId, sStorageName, xSrcStg, xDstStg ))
@@ -6797,7 +6797,7 @@ rtl::Reference<SdrObject> SvxMSDffManager::ImportOLE( 
sal_uInt32 nOLEId,
 
 bool SvxMSDffManager::MakeContentStream( SotStorage * pStor, const GDIMetaFile 
& rMtf )
 {
-    tools::SvRef<SotStorageStream> xStm = 
pStor->OpenSotStream(SVEXT_PERSIST_STREAM);
+    rtl::Reference<SotStorageStream> xStm = 
pStor->OpenSotStream(SVEXT_PERSIST_STREAM);
     xStm->SetVersion( pStor->GetVersion() );
     xStm->SetBufferSize( 8192 );
 
@@ -6937,10 +6937,10 @@ const ClsIDs aClsIDs[] = {
 
 
 bool SvxMSDffManager::ConvertToOle2( SvStream& rStm, sal_uInt32 nReadLen,
-                    const GDIMetaFile * pMtf, const tools::SvRef<SotStorage>& 
rDest )
+                    const GDIMetaFile * pMtf, const 
rtl::Reference<SotStorage>& rDest )
 {
     bool bMtfRead = false;
-    tools::SvRef<SotStorageStream> xOle10Stm = rDest->OpenSotStream( 
"Ole10Native",
+    rtl::Reference<SotStorageStream> xOle10Stm = rDest->OpenSotStream( 
"Ole10Native",
                                                     StreamMode::WRITE| 
StreamMode::SHARE_DENYALL );
     if( xOle10Stm->GetError() )
         return false;
@@ -6986,7 +6986,7 @@ bool SvxMSDffManager::ConvertToOle2( SvStream& rStm, 
sal_uInt32 nReadLen,
                 // write to ole10 stream
                 xOle10Stm->WriteUInt32( nDataLen );
                 xOle10Stm->WriteBytes(pData.get(), nDataLen);
-                xOle10Stm = tools::SvRef<SotStorageStream>();
+                xOle10Stm.clear();
 
                 // set the compobj stream
                 const ClsIDs* pIds;
@@ -7104,7 +7104,7 @@ OUString SvxMSDffManager::GetFilterNameFromClassID( const 
SvGlobalName& aGlobNam
 
 void SvxMSDffManager::ExtractOwnStream(SotStorage& rSrcStg, SvMemoryStream& 
rMemStream)
 {
-    tools::SvRef<SotStorageStream> xStr
+    rtl::Reference<SotStorageStream> xStr
         = rSrcStg.OpenSotStream("package_stream", StreamMode::STD_READ);
     xStr->ReadStream(rMemStream);
 }
@@ -7177,7 +7177,7 @@ css::uno::Reference < css::embed::XEmbeddedObject >  
SvxMSDffManager::CheckForCo
         }
         else
         {
-            tools::SvRef<SotStorage> xStorage = new SotStorage( false, 
aMemStream );
+            rtl::Reference<SotStorage> xStorage = new SotStorage(false, 
aMemStream);
             rSrcStg.CopyTo( xStorage.get() );
             xStorage->Commit();
             xStorage.clear();
@@ -7291,7 +7291,7 @@ css::uno::Reference < css::embed::XEmbeddedObject >  
SvxMSDffManager::CheckForCo
 rtl::Reference<SdrOle2Obj> SvxMSDffManager::CreateSdrOLEFromStorage(
     SdrModel& rSdrModel,
     const OUString& rStorageName,
-    tools::SvRef<SotStorage> const & rSrcStorage,
+    rtl::Reference<SotStorage> const & rSrcStorage,
     const uno::Reference < embed::XStorage >& xDestStorage,
     const Graphic& rGrf,
     const tools::Rectangle& rBoundRect,
@@ -7314,12 +7314,12 @@ rtl::Reference<SdrOle2Obj> 
SvxMSDffManager::CreateSdrOLEFromStorage(
         OUString aDstStgName = MSO_OLE_Obj + OUString::number( ++nMSOleObjCntr 
);
 
         {
-            tools::SvRef<SotStorage> xObjStg = rSrcStorage->OpenSotStorage( 
rStorageName );
+            rtl::Reference<SotStorage> xObjStg = 
rSrcStorage->OpenSotStorage(rStorageName);
             if( xObjStg.is()  )
             {
                 {
                     sal_uInt8 aTestA[10];   // exist the CompObj-Stream ?
-                    tools::SvRef<SotStorageStream> xSrcTst = 
xObjStg->OpenSotStream( "CompObj" );
+                    rtl::Reference<SotStorageStream> xSrcTst = 
xObjStg->OpenSotStream("CompObj");
                     bValidStorage = xSrcTst.is() && sizeof( aTestA ) ==
                                     xSrcTst->ReadBytes(aTestA, sizeof(aTestA));
                     if( !bValidStorage )
@@ -7340,7 +7340,7 @@ rtl::Reference<SdrOle2Obj> 
SvxMSDffManager::CreateSdrOLEFromStorage(
                         // is a kind of embedded objects in Word documents
                         // TODO/LATER: should the caller be notified if the 
aspect changes in future?
 
-                        tools::SvRef<SotStorageStream> xObjInfoSrc = 
xObjStg->OpenSotStream(
+                        rtl::Reference<SotStorageStream> xObjInfoSrc = 
xObjStg->OpenSotStream(
                             "ObjInfo", StreamMode::STD_READ );
                         if ( xObjInfoSrc.is() && !xObjInfoSrc->GetError() )
                         {
@@ -7382,11 +7382,11 @@ rtl::Reference<SdrOle2Obj> 
SvxMSDffManager::CreateSdrOLEFromStorage(
         if( bValidStorage )
         {
             // object is not an own object
-            tools::SvRef<SotStorage> xObjStor = SotStorage::OpenOLEStorage( 
xDestStorage, aDstStgName, StreamMode::READWRITE );
+            rtl::Reference<SotStorage> xObjStor = SotStorage::OpenOLEStorage( 
xDestStorage, aDstStgName, StreamMode::READWRITE );
 
             if ( xObjStor.is() )
             {
-                tools::SvRef<SotStorage> xSrcStor = 
rSrcStorage->OpenSotStorage( rStorageName, StreamMode::READ );
+                rtl::Reference<SotStorage> xSrcStor = 
rSrcStorage->OpenSotStorage( rStorageName, StreamMode::READ );
                 xSrcStor->CopyTo( xObjStor.get() );
 
                 if( !xObjStor->GetError() )
@@ -7413,7 +7413,7 @@ rtl::Reference<SdrOle2Obj> 
SvxMSDffManager::CreateSdrOLEFromStorage(
             else
             {
                 // or is it an OLE-1 Stream in the DataStream?
-                tools::SvRef<SotStorage> xObjStor = 
SotStorage::OpenOLEStorage( xDestStorage, aDstStgName );
+                rtl::Reference<SotStorage> xObjStor = 
SotStorage::OpenOLEStorage( xDestStorage, aDstStgName );
                 //TODO/MBA: remove metafile conversion from ConvertToOle2
                 //when is this code used?!
                 GDIMetaFile aMtf;
diff --git a/filter/source/msfilter/msoleexp.cxx 
b/filter/source/msfilter/msoleexp.cxx
index cd9f521da605..3229bf57ec54 100644
--- a/filter/source/msfilter/msoleexp.cxx
+++ b/filter/source/msfilter/msoleexp.cxx
@@ -203,7 +203,7 @@ void SvxMSExportOLEObjects::ExportOLEObject( 
svt::EmbeddedObjectRef const & rObj
             }
             catch( const uno::Exception& ) {} // #TODO really handle 
exceptions - interactionalhandler etc. ?
 
-            tools::SvRef<SotStorage> xOLEStor = new SotStorage( pStream, true 
);
+            rtl::Reference<SotStorage> xOLEStor = new SotStorage( pStream, 
true );
             xOLEStor->CopyTo( &rDestStg );
             rDestStg.Commit();
         }
@@ -224,7 +224,7 @@ void SvxMSExportOLEObjects::ExportOLEObject( 
svt::EmbeddedObjectRef const & rObj
             rDestStg.SetClass( aEmbName,
                                 SotClipboardFormatId::EMBEDDED_OBJ_OLE,
                                 GetStorageType( aEmbName ) );
-            tools::SvRef<SotStorageStream> xExtStm = rDestStg.OpenSotStream(
+            rtl::Reference<SotStorageStream> xExtStm = rDestStg.OpenSotStream(
                                             "properties_stream");
 
             bool bExtentSuccess = false;
@@ -279,7 +279,7 @@ void SvxMSExportOLEObjects::ExportOLEObject( 
svt::EmbeddedObjectRef const & rObj
 
             if ( bExtentSuccess )
             {
-                tools::SvRef<SotStorageStream> xEmbStm = 
rDestStg.OpenSotStream(
+                rtl::Reference<SotStorageStream> xEmbStm = 
rDestStg.OpenSotStream(
                                                 "package_stream");
                 if( !xEmbStm->GetError() )
                 {
@@ -326,7 +326,7 @@ void SvxMSExportOLEObjects::ExportOLEObject( 
svt::EmbeddedObjectRef const & rObj
             catch ( const uno::Exception& )
             {}
 
-            tools::SvRef<SotStorage> xOLEStor = SotStorage::OpenOLEStorage( 
xStor, aTempName, StreamMode::STD_READ );
+            rtl::Reference<SotStorage> xOLEStor = SotStorage::OpenOLEStorage( 
xStor, aTempName, StreamMode::STD_READ );
             xOLEStor->CopyTo( &rDestStg );
             rDestStg.Commit();
         }
diff --git a/filter/source/msfilter/rtfutil.cxx 
b/filter/source/msfilter/rtfutil.cxx
index 75f453c347c9..f9881c86a2b9 100644
--- a/filter/source/msfilter/rtfutil.cxx
+++ b/filter/source/msfilter/rtfutil.cxx
@@ -26,7 +26,7 @@ namespace
 void WrapOle1InOle2(SvStream& rOle1, sal_uInt32 nOle1Size, SvStream& rOle2,
                     const OString& rClassName)
 {
-    tools::SvRef<SotStorage> pStorage = new SotStorage(rOle2);
+    rtl::Reference<SotStorage> pStorage = new SotStorage(rOle2);
     OString aAnsiUserType;
     SvGlobalName aName;
     if (rClassName == "PBrush")
@@ -46,7 +46,7 @@ void WrapOle1InOle2(SvStream& rOle1, sal_uInt32 nOle1Size, 
SvStream& rOle2,
     pStorage->SetClass(aName, SotClipboardFormatId::NONE, "");
 
     // [MS-OLEDS] 2.3.7 CompObjHeader
-    tools::SvRef<SotStorageStream> pCompObj = 
pStorage->OpenSotStream("CompObj");
+    rtl::Reference<SotStorageStream> pCompObj = 
pStorage->OpenSotStream("CompObj");
     // Reserved1
     pCompObj->WriteUInt32(0xfffe0001);
     // Version
@@ -80,7 +80,7 @@ void WrapOle1InOle2(SvStream& rOle1, sal_uInt32 nOle1Size, 
SvStream& rOle2,
     pCompObj.clear();
 
     // [MS-OLEDS] 2.3.6 OLENativeStream
-    tools::SvRef<SotStorageStream> pOleNative = 
pStorage->OpenSotStream("Ole10Native");
+    rtl::Reference<SotStorageStream> pOleNative = 
pStorage->OpenSotStream("Ole10Native");
     // NativeDataSize
     pOleNative->WriteUInt32(nOle1Size);
     pOleNative->WriteStream(rOle1, nOle1Size);
diff --git a/filter/source/msfilter/svdfppt.cxx 
b/filter/source/msfilter/svdfppt.cxx
index cb6381fb6fd3..5f74aa47eea2 100644
--- a/filter/source/msfilter/svdfppt.cxx
+++ b/filter/source/msfilter/svdfppt.cxx
@@ -1677,7 +1677,7 @@ SdrPowerPointImport::~SdrPowerPointImport()
     m_pNotePages.reset();
 }
 
-bool PPTConvertOCXControls::ReadOCXStream( tools::SvRef<SotStorage>& rSrc,
+bool PPTConvertOCXControls::ReadOCXStream( rtl::Reference<SotStorage>& rSrc,
         css::uno::Reference< css::drawing::XShape > *pShapeRef )
 {
     bool bRes = false;
@@ -1856,14 +1856,14 @@ rtl::Reference<SdrObject> 
SdrPowerPointImport::ImportOLE( sal_uInt32 nOLEId,
             {
                 pDest->Seek(0);
                 Storage* pObjStor = new Storage(*pDest, false);
-                tools::SvRef<SotStorage> xObjStor( new SotStorage( pObjStor ) 
);
+                rtl::Reference<SotStorage> xObjStor(new SotStorage(pObjStor));
                 if ( xObjStor.is() && !xObjStor->GetError() )
                 {
                     if ( xObjStor->GetClassName() == SvGlobalName() )
                     {
                         xObjStor->SetClass( SvGlobalName( 
pObjStor->GetClassId() ), pObjStor->GetFormat(), pObjStor->GetUserName() );
                     }
-                    tools::SvRef<SotStorageStream> xSrcTst = 
xObjStor->OpenSotStream( "Ole" );
+                    rtl::Reference<SotStorageStream> xSrcTst = 
xObjStor->OpenSotStream("Ole");
                     if ( xSrcTst.is() )
                     {
                         sal_uInt8 aTestA[ 10 ];
@@ -1914,7 +1914,7 @@ rtl::Reference<SdrObject> SdrPowerPointImport::ImportOLE( 
sal_uInt32 nOLEId,
                                 const css::uno::Reference < 
css::embed::XStorage >& rStorage = rOe.pShell->GetStorage();
                                 if (rStorage.is())
                                 {
-                                    tools::SvRef<SotStorage> xTarget = 
SotStorage::OpenOLEStorage(rStorage, aNm, StreamMode::READWRITE);
+                                    rtl::Reference<SotStorage> xTarget = 
SotStorage::OpenOLEStorage(rStorage, aNm, StreamMode::READWRITE);
                                     if (xObjStor.is() && xTarget.is())
                                     {
                                         xObjStor->CopyTo(xTarget.get());
@@ -2035,16 +2035,16 @@ void SdrPowerPointImport::SeekOle( SfxObjectShell* 
pShell, sal_uInt32 nFilterOpt
                     std::unique_ptr<SvMemoryStream> pBas = ImportExOleObjStg( 
nPersistPtr, nOleId );
                     if ( pBas )
                     {
-                        tools::SvRef<SotStorage> xSource( new SotStorage( 
pBas.release(), true ) );
-                        tools::SvRef<SotStorage> xDest( new SotStorage( new 
SvMemoryStream(), true ) );
+                        rtl::Reference<SotStorage> xSource(new 
SotStorage(pBas.release(), true));
+                        rtl::Reference<SotStorage> xDest( new SotStorage( new 
SvMemoryStream(), true ) );
                         if ( xSource.is() && xDest.is() )
                         {
                             // is this a visual basic storage ?
-                            tools::SvRef<SotStorage> xSubStorage = 
xSource->OpenSotStorage( "VBA",
+                            rtl::Reference<SotStorage> xSubStorage = 
xSource->OpenSotStorage( "VBA",
                                 StreamMode::READWRITE | StreamMode::NOCREATE | 
StreamMode::SHARE_DENYALL );
                             if( xSubStorage.is() && ( ERRCODE_NONE == 
xSubStorage->GetError() ) )
                             {
-                                tools::SvRef<SotStorage> xMacros = 
xDest->OpenSotStorage( "MACROS" );
+                                rtl::Reference<SotStorage> xMacros = 
xDest->OpenSotStorage( "MACROS" );
                                 if ( xMacros.is() )
                                 {
                                     SvStorageInfoList aList;
@@ -2063,13 +2063,13 @@ void SdrPowerPointImport::SeekOle( SfxObjectShell* 
pShell, sal_uInt32 nFilterOpt
                                         uno::Reference < embed::XStorage > 
xDoc( pShell->GetStorage() );
                                         if ( xDoc.is() )
                                         {
-                                            tools::SvRef<SotStorage> xVBA = 
SotStorage::OpenOLEStorage( xDoc, SvxImportMSVBasic::GetMSBasicStorageName() );
+                                            rtl::Reference<SotStorage> xVBA = 
SotStorage::OpenOLEStorage( xDoc, SvxImportMSVBasic::GetMSBasicStorageName() );
                                             if ( xVBA.is() && ( 
xVBA->GetError() == ERRCODE_NONE ) )
                                             {
-                                                tools::SvRef<SotStorage> 
xSubVBA = xVBA->OpenSotStorage( "_MS_VBA_Overhead" );
+                                                rtl::Reference<SotStorage> 
xSubVBA = xVBA->OpenSotStorage( "_MS_VBA_Overhead" );
                                                 if ( xSubVBA.is() && ( 
xSubVBA->GetError() == ERRCODE_NONE ) )
                                                 {
-                                                    
tools::SvRef<SotStorageStream> xOriginal = xSubVBA->OpenSotStream( 
"_MS_VBA_Overhead2" );
+                                                    
rtl::Reference<SotStorageStream> xOriginal = xSubVBA->OpenSotStream( 
"_MS_VBA_Overhead2" );
                                                     if ( xOriginal.is() && ( 
xOriginal->GetError() == ERRCODE_NONE ) )
                                                     {
                                                         if ( nPersistPtr && ( 
nPersistPtr < m_nPersistPtrCnt ) )
diff --git a/filter/source/msfilter/svxmsbas2.cxx 
b/filter/source/msfilter/svxmsbas2.cxx
index 8a4ce43f1914..71ac50fd3928 100644
--- a/filter/source/msfilter/svxmsbas2.cxx
+++ b/filter/source/msfilter/svxmsbas2.cxx
@@ -33,7 +33,7 @@ ErrCode SvxImportMSVBasic::SaveOrDelMSVBAStorage( bool 
bSaveInto,
     ErrCode nRet = ERRCODE_NONE;
     uno::Reference < embed::XStorage > xSrcRoot( rDocSh.GetStorage() );
     OUString aDstStgName( GetMSBasicStorageName() );
-    tools::SvRef<SotStorage> xVBAStg( SotStorage::OpenOLEStorage( xSrcRoot, 
aDstStgName,
+    rtl::Reference<SotStorage> xVBAStg( SotStorage::OpenOLEStorage( xSrcRoot, 
aDstStgName,
                                 StreamMode::READWRITE | StreamMode::NOCREATE | 
StreamMode::SHARE_DENYALL ) );
     if( xVBAStg.is() && !xVBAStg->GetError() )
     {
@@ -45,8 +45,8 @@ ErrCode SvxImportMSVBasic::SaveOrDelMSVBAStorage( bool 
bSaveInto,
             if( pBasicMan && pBasicMan->IsBasicModified() )
                 nRet = ERRCODE_SVX_MODIFIED_VBASIC_STORAGE;
 #endif
-            tools::SvRef<SotStorage> xSrc = SotStorage::OpenOLEStorage( 
xSrcRoot, aDstStgName, StreamMode::STD_READ );
-            tools::SvRef<SotStorage> xDst = xRoot->OpenSotStorage( 
rStorageName, StreamMode::READWRITE | StreamMode::TRUNC );
+            rtl::Reference<SotStorage> xSrc = SotStorage::OpenOLEStorage( 
xSrcRoot, aDstStgName, StreamMode::STD_READ );
+            rtl::Reference<SotStorage> xDst = xRoot->OpenSotStorage( 
rStorageName, StreamMode::READWRITE | StreamMode::TRUNC );
             xSrc->CopyTo( xDst.get() );
             xDst->Commit();
             ErrCode nError = xDst->GetError();
@@ -65,7 +65,7 @@ ErrCode SvxImportMSVBasic::SaveOrDelMSVBAStorage( bool 
bSaveInto,
 ErrCode SvxImportMSVBasic::GetSaveWarningOfMSVBAStorage( SfxObjectShell 
&rDocSh)
 {
     uno::Reference < embed::XStorage > xSrcRoot( rDocSh.GetStorage() );
-    tools::SvRef<SotStorage> xVBAStg( SotStorage::OpenOLEStorage( xSrcRoot, 
GetMSBasicStorageName(),
+    rtl::Reference<SotStorage> xVBAStg( SotStorage::OpenOLEStorage( xSrcRoot, 
GetMSBasicStorageName(),
                     StreamMode::READ | StreamMode::NOCREATE | 
StreamMode::SHARE_DENYALL ));
     return ( xVBAStg.is() && !xVBAStg->GetError() )
                     ? ERRCODE_SVX_VBASIC_STORAGE_EXIST
diff --git a/include/editeng/svxacorr.hxx b/include/editeng/svxacorr.hxx
index f71af87bc5b8..d8d0c3ec499c 100644
--- a/include/editeng/svxacorr.hxx
+++ b/include/editeng/svxacorr.hxx
@@ -24,6 +24,7 @@
 #include <o3tl/typed_flags_set.hxx>
 #include <o3tl/string_view.hxx>
 #include <i18nlangtag/languagetag.hxx>
+#include <rtl/ref.hxx>
 #include <tools/time.hxx>
 #include <tools/date.hxx>
 #include <editeng/swafopt.hxx>
@@ -199,10 +200,10 @@ class EDITENG_DLLPUBLIC SvxAutoCorrectLanguageLists
     bool IsFileChanged_Imp();
     void LoadXMLExceptList_Imp( std::unique_ptr<SvStringsISortDtor>& rpLst,
                                 const OUString& sStrmName,
-                                tools::SvRef<SotStorage>& rStg);
+                                rtl::Reference<SotStorage>& rStg);
     static void SaveExceptList_Imp( const SvStringsISortDtor& rLst,
                                 const OUString& sStrmName,
-                                tools::SvRef<SotStorage> const & rStg,
+                                rtl::Reference<SotStorage> const & rStg,
                                 bool bConvert = false);
 
     bool MakeBlocklist_Imp( SotStorage& rStg );
diff --git a/include/filter/msfilter/msdffimp.hxx 
b/include/filter/msfilter/msdffimp.hxx
index 7f2c5a2763d0..7be2fe6eef87 100644
--- a/include/filter/msfilter/msdffimp.hxx
+++ b/include/filter/msfilter/msdffimp.hxx
@@ -535,7 +535,7 @@ protected:
 // the following method needs to be overridden for the import of OLE objects
     virtual bool GetOLEStorageName( sal_uInt32 nOLEId,
                                       OUString& rStorageName,
-                                      tools::SvRef<SotStorage>& rSrcStorage,
+                                      rtl::Reference<SotStorage>& rSrcStorage,
                                       css::uno::Reference < 
css::embed::XStorage >& xDestStg
                                     ) const;
 
@@ -630,7 +630,7 @@ public:
     static bool         ConvertToOle2( SvStream& rStm,
                                        sal_uInt32 nLen,
                                        const GDIMetaFile*,
-                                       const tools::SvRef<SotStorage> & rDest 
);
+                                       const rtl::Reference<SotStorage> & 
rDest );
 
     void SetModel(SdrModel* pModel, tools::Long nApplicationScale);
     SdrModel*  GetModel() const { return pSdrModel; }
@@ -718,7 +718,7 @@ public:
     static rtl::Reference<SdrOle2Obj> CreateSdrOLEFromStorage(
         SdrModel& rSdrModel,
         const OUString& rStorageName,
-        tools::SvRef<SotStorage> const & rSrcStorage,
+        rtl::Reference<SotStorage> const & rSrcStorage,
         const css::uno::Reference < css::embed::XStorage >& xDestStg,
         const Graphic& rGraf,
         const tools::Rectangle& rBoundRect,
diff --git a/include/filter/msfilter/svdfppt.hxx 
b/include/filter/msfilter/svdfppt.hxx
index 9c515615e0af..4affb12a4649 100644
--- a/include/filter/msfilter/svdfppt.hxx
+++ b/include/filter/msfilter/svdfppt.hxx
@@ -655,7 +655,7 @@ public:
                                 SvxMSDffSolverContainer* pSolverContainer,
                                 std::vector<rtl::Reference<SdrObject>>& 
rBackgroundColoredObjects
                             );
-    virtual bool ReadFormControl( tools::SvRef<SotStorage>& rSrc1, 
css::uno::Reference< css::form::XFormComponent > & rFormComp ) const = 0;
+    virtual bool ReadFormControl( rtl::Reference<SotStorage>& rSrc1, 
css::uno::Reference< css::form::XFormComponent > & rFormComp ) const = 0;
 };
 
 struct PPTTextParagraphStyleAtomInterpreter
@@ -1285,7 +1285,7 @@ public:
         ePageKind               ( ePKind ),
         mpPPTImporter           ( pPPTImporter )
     {};
-    bool ReadOCXStream( tools::SvRef<SotStorage>& rSrc1,
+    bool ReadOCXStream( rtl::Reference<SotStorage>& rSrc1,
         css::uno::Reference<css::drawing::XShape > *pShapeRef );
     virtual bool InsertControl(
         const css::uno::Reference< css::form::XFormComponent > &rFComp,
diff --git a/include/filter/msfilter/svxmsbas.hxx 
b/include/filter/msfilter/svxmsbas.hxx
index 304191ab7950..7d4578e98037 100644
--- a/include/filter/msfilter/svxmsbas.hxx
+++ b/include/filter/msfilter/svxmsbas.hxx
@@ -64,7 +64,7 @@ public:
     static OUString GetMSBasicStorageName();
 
 private:
-    tools::SvRef<SotStorage> xRoot;
+    rtl::Reference<SotStorage> xRoot;
     SfxObjectShell& rDocSh;
 };
 
diff --git a/include/oox/ole/olehelper.hxx b/include/oox/ole/olehelper.hxx
index a2810460bcd3..256896193903 100644
--- a/include/oox/ole/olehelper.hxx
+++ b/include/oox/ole/olehelper.hxx
@@ -25,9 +25,9 @@
 #include <oox/dllapi.h>
 #include <oox/helper/binarystreambase.hxx>
 #include <oox/helper/graphichelper.hxx>
+#include <rtl/ref.hxx>
 #include <rtl/ustring.hxx>
 #include <sal/types.h>
-#include <tools/ref.hxx>
 #include <memory>
 
 namespace com::sun::star {
@@ -183,10 +183,10 @@ protected:
 public:
     MSConvertOCXControls( const css::uno::Reference< css::frame::XModel >& 
rxModel );
     virtual ~MSConvertOCXControls() override;
-    bool ReadOCXStorage( tools::SvRef<SotStorage> const & rSrc1, 
css::uno::Reference< css::form::XFormComponent > & rxFormComp );
-    bool ReadOCXCtlsStream(tools::SvRef<SotStorageStream> const & rSrc1, 
css::uno::Reference< css::form::XFormComponent > & rxFormComp,
+    bool ReadOCXStorage( rtl::Reference<SotStorage> const & rSrc1, 
css::uno::Reference< css::form::XFormComponent > & rxFormComp );
+    bool ReadOCXCtlsStream(rtl::Reference<SotStorageStream> const & rSrc1, 
css::uno::Reference< css::form::XFormComponent > & rxFormComp,
                                    sal_Int32 nPos, sal_Int32 nSize );
-    static bool WriteOCXStream( const css::uno::Reference< css::frame::XModel 
>& rxModel, tools::SvRef<SotStorage> const &rSrc1, const css::uno::Reference< 
css::awt::XControlModel > &rControlModel, const css::awt::Size& rSize,OUString 
&rName);
+    static bool WriteOCXStream( const css::uno::Reference< css::frame::XModel 
>& rxModel, rtl::Reference<SotStorage> const &rSrc1, const css::uno::Reference< 
css::awt::XControlModel > &rControlModel, const css::awt::Size& rSize,OUString 
&rName);
     static bool WriteOCXExcelKludgeStream( const css::uno::Reference< 
css::frame::XModel >& rxModel, const css::uno::Reference< 
css::io::XOutputStream >& xOutStrm, const css::uno::Reference< 
css::awt::XControlModel > &rControlModel, const css::awt::Size& rSize,OUString 
&rName);
 };
 
diff --git a/include/sfx2/event.hxx b/include/sfx2/event.hxx
index 021472830ab8..d94e839b5618 100644
--- a/include/sfx2/event.hxx
+++ b/include/sfx2/event.hxx
@@ -23,10 +23,12 @@
 
 #include <ostream>
 
+#include <unotools/weakref.hxx>
 #include <sfx2/dllapi.h>
 #include <svl/hint.hxx>
 #include <unotools/eventcfg.hxx>
 #include <rtl/ustring.hxx>
+#include <sfx2/objsh.hxx>
 
 #include <com/sun/star/uno/Sequence.hxx>
 #include <com/sun/star/frame/XController2.hpp>
@@ -111,8 +113,6 @@ inline std::basic_ostream<charT, traits> & operator <<(
     }
 }
 
-class SfxObjectShell;
-
 enum class SfxEventHintId {
     NONE = 0,
     ActivateDoc,
@@ -191,7 +191,7 @@ inline std::basic_ostream<charT, traits> & operator <<(
 
 class SFX2_DLLPUBLIC SfxEventHint : public SfxHint
 {
-    SfxObjectShell*     pObjShell;
+    unotools::WeakReference<SfxObjectShell> pObjShell;
     OUString            aEventName;
     SfxEventHintId      nEventId;
 
@@ -208,8 +208,7 @@ public:
     const OUString&     GetEventName() const
                         { return aEventName; }
 
-    SfxObjectShell*     GetObjShell() const
-                        { return pObjShell; }
+    rtl::Reference<SfxObjectShell> GetObjShell() const { return 
pObjShell.get(); }
 };
 
 
diff --git a/include/sfx2/objsh.hxx b/include/sfx2/objsh.hxx
index 79f22c978dcb..5d98cb0409af 100644
--- a/include/sfx2/objsh.hxx
+++ b/include/sfx2/objsh.hxx
@@ -173,7 +173,7 @@ template<class T> bool checkSfxObjectShell(const 
SfxObjectShell* pShell)
 }
 
 class SFX2_DLLPUBLIC SfxObjectShell :
-    public SfxShell, virtual public SotObject,
+    public SfxShell, public SotObject,
     public ::comphelper::IEmbeddedHelper
 {
 friend struct ModifyBlocker_Impl;
@@ -822,7 +822,7 @@ public:
     };
 
 
-typedef tools::SvRef<SfxObjectShell> SfxObjectShellRef;
+typedef rtl::Reference<SfxObjectShell> SfxObjectShellRef;
 
 class SfxObjectShellLock
 {
diff --git a/include/sot/object.hxx b/include/sot/object.hxx
index 1657986c8490..6cd3634bd557 100644
--- a/include/sot/object.hxx
+++ b/include/sot/object.hxx
@@ -22,10 +22,11 @@
 
 #include <sal/config.h>
 
-#include <tools/ref.hxx>
+#include <cppuhelper/weak.hxx>
+#include <rtl/ref.hxx>
 #include <sot/sotdllapi.h>
 
-class SOT_DLLPUBLIC SotObject : virtual public SvRefBase
+class SOT_DLLPUBLIC SotObject : public cppu::OWeakObject
 {
     sal_uInt16 nOwnerLockCount;
     bool bInClose; // TRUE, in DoClose
@@ -42,6 +43,7 @@ public:
     void OwnerLock(bool bLock);
     bool DoClose();
     bool IsInClose() const { return bInClose; }
+    oslInterlockedCount GetRefCount() const { return m_refCount; }
 
 private:
     SotObject& operator=(const SotObject&) = delete;
diff --git a/include/sot/storage.hxx b/include/sot/storage.hxx
index ac11340ac26d..2a49b4a39475 100644
--- a/include/sot/storage.hxx
+++ b/include/sot/storage.hxx
@@ -142,9 +142,9 @@ public:
 
     // create stream with connection to Storage,
     // more or less a Parent-Child relationship
-    tools::SvRef<SotStorageStream> OpenSotStream(const OUString & rEleName,
+    rtl::Reference<SotStorageStream> OpenSotStream(const OUString & rEleName,
                                      StreamMode = StreamMode::STD_READWRITE);
-    SotStorage* OpenSotStorage(const OUString & rEleName,
+    rtl::Reference<SotStorage> OpenSotStorage(const OUString & rEleName,
                                StreamMode = StreamMode::STD_READWRITE,
                                bool transacted = true);
 
@@ -161,7 +161,7 @@ public:
     static bool IsOLEStorage(const OUString & rFileName);
     static bool IsOLEStorage(SvStream* pStream);
 
-    static SotStorage* 
OpenOLEStorage(css::uno::Reference<css::embed::XStorage> const & xStorage,
+    static rtl::Reference<SotStorage> 
OpenOLEStorage(css::uno::Reference<css::embed::XStorage> const & xStorage,
                                       OUString const & rEleName, StreamMode = 
StreamMode::STD_READWRITE);
     static SotClipboardFormatId 
GetFormatID(css::uno::Reference<css::embed::XStorage> const & xStorage);
     static sal_Int32 GetVersion(css::uno::Reference<css::embed::XStorage> 
const & xStorage);
diff --git a/include/svx/gallerybinaryengine.hxx 
b/include/svx/gallerybinaryengine.hxx
index 5e89b5dd756d..2f9c7ce00508 100644
--- a/include/svx/gallerybinaryengine.hxx
+++ b/include/svx/gallerybinaryengine.hxx
@@ -47,7 +47,7 @@ class GalleryThemeEntry;
 class SVXCORE_DLLPUBLIC GalleryBinaryEngine final : public GalleryFileStorage
 {
 private:
-    tools::SvRef<SotStorage> m_aSvDrawStorageRef;
+    rtl::Reference<SotStorage> m_aSvDrawStorageRef;
     const GalleryBinaryStorageLocations& maGalleryStorageLocations;
     GalleryObjectCollection& mrGalleryObjectCollection;
     bool mbReadOnly;
@@ -69,7 +69,7 @@ public:
     void setDestDir(const OUString& rDestDir, bool bRelative);
 
     SAL_DLLPRIVATE void ImplCreateSvDrawStorage();
-    SAL_DLLPRIVATE const tools::SvRef<SotStorage>& GetSvDrawStorage() const;
+    SAL_DLLPRIVATE const rtl::Reference<SotStorage>& GetSvDrawStorage() const;
 
     const INetURLObject& getThemeURL() const { return 
maGalleryStorageLocations.getThemeURL(); }
 
diff --git a/oox/source/ole/olehelper.cxx b/oox/source/ole/olehelper.cxx
index d953cf17f9da..e5a81320156b 100644
--- a/oox/source/ole/olehelper.cxx
+++ b/oox/source/ole/olehelper.cxx
@@ -437,7 +437,7 @@ MSConvertOCXControls::importControlFromStream( 
::oox::BinaryInputStream& rInStrm
 }
 
 bool
-MSConvertOCXControls::ReadOCXCtlsStream( tools::SvRef<SotStorageStream> const 
& rSrc1, Reference< XFormComponent > & rxFormComp,
+MSConvertOCXControls::ReadOCXCtlsStream( rtl::Reference<SotStorageStream> 
const & rSrc1, Reference< XFormComponent > & rxFormComp,
                                    sal_Int32 nPos,
                                    sal_Int32 nStreamSize)
 {
@@ -486,18 +486,18 @@ bool MSConvertOCXControls::importControlFromStream( 
::oox::BinaryInputStream& rI
     return rxFormComp.is();
 }
 
-bool MSConvertOCXControls::ReadOCXStorage( tools::SvRef<SotStorage> const & 
xOleStg,
+bool MSConvertOCXControls::ReadOCXStorage( rtl::Reference<SotStorage> const & 
xOleStg,
                                   Reference< XFormComponent > & rxFormComp )
 {
     if ( xOleStg.is() )
     {
-        tools::SvRef<SotStorageStream> pNameStream = 
xOleStg->OpenSotStream("OCXNAME", StreamMode::READ);
+        rtl::Reference<SotStorageStream> pNameStream = 
xOleStg->OpenSotStream("OCXNAME", StreamMode::READ);
         BinaryXInputStream aNameStream( Reference< XInputStream >( new 
utl::OSeekableInputStreamWrapper( *pNameStream ) ), true );
 
-        tools::SvRef<SotStorageStream> pContents = 
xOleStg->OpenSotStream("contents", StreamMode::READ);
+        rtl::Reference<SotStorageStream> pContents = 
xOleStg->OpenSotStream("contents", StreamMode::READ);
         BinaryXInputStream aInStrm(  Reference< XInputStream >( new 
utl::OSeekableInputStreamWrapper( *pContents ) ), true );
 
-        tools::SvRef<SotStorageStream> pClsStrm = 
xOleStg->OpenSotStream("CompObj", StreamMode::READ);
+        rtl::Reference<SotStorageStream> pClsStrm = 
xOleStg->OpenSotStream("CompObj", StreamMode::READ);
         BinaryXInputStream aClsStrm( Reference< XInputStream >( new 
utl::OSeekableInputStreamWrapper(*pClsStrm ) ), true );
         aClsStrm.skip(12);
 
@@ -533,7 +533,7 @@ bool MSConvertOCXControls::WriteOCXExcelKludgeStream( const 
css::uno::Reference<
     return true;
 }
 
-bool MSConvertOCXControls::WriteOCXStream( const Reference< XModel >& rxModel, 
tools::SvRef<SotStorage> const &xOleStg,
+bool MSConvertOCXControls::WriteOCXStream( const Reference< XModel >& rxModel, 
rtl::Reference<SotStorage> const &xOleStg,
     const Reference< XControlModel > &rxControlModel,
     const css::awt::Size& rSize, OUString &rName)
 {
@@ -550,17 +550,17 @@ bool MSConvertOCXControls::WriteOCXStream( const 
Reference< XModel >& rxModel, t
     rName = exportHelper.getTypeName();
     xOleStg->SetClass( aName, SotClipboardFormatId::EMBEDDED_OBJ_OLE, 
sFullName);
     {
-        tools::SvRef<SotStorageStream> pNameStream = 
xOleStg->OpenSotStream("OCXNAME");
+        rtl::Reference<SotStorageStream> pNameStream = 
xOleStg->OpenSotStream("OCXNAME");
         Reference< XOutputStream > xOut = new 
utl::OSeekableOutputStreamWrapper( *pNameStream );
         exportHelper.exportName( xOut );
     }
     {
-        tools::SvRef<SotStorageStream> pObjStream = 
xOleStg->OpenSotStream("CompObj");
+        rtl::Reference<SotStorageStream> pObjStream = 
xOleStg->OpenSotStream("CompObj");
         Reference< XOutputStream > xOut = new 
utl::OSeekableOutputStreamWrapper( *pObjStream );
         exportHelper.exportCompObj( xOut );
     }
     {
-        tools::SvRef<SotStorageStream> pContents = 
xOleStg->OpenSotStream("contents");
+        rtl::Reference<SotStorageStream> pContents = 
xOleStg->OpenSotStream("contents");
         Reference< XOutputStream > xOut = new 
utl::OSeekableOutputStreamWrapper( *pContents );
         exportHelper.exportControl( xOut, rSize );
     }
diff --git a/oox/source/ole/vbaexport.cxx b/oox/source/ole/vbaexport.cxx
index 5e0fc8ae2a71..9c3bac21cbe9 100644
--- a/oox/source/ole/vbaexport.cxx
+++ b/oox/source/ole/vbaexport.cxx
@@ -1040,12 +1040,12 @@ void VbaExport::exportVBA(SotStorage* pRootStorage)
     getCorrectExportOrder(xNameContainer, aLibraryMap);
 
     // start here with the VBA export
-    tools::SvRef<SotStorage> xVBAStream = pRootStorage->OpenSotStorage("VBA", 
StreamMode::READWRITE);
-    tools::SvRef<SotStorageStream> pDirStream = 
xVBAStream->OpenSotStream("dir", StreamMode::READWRITE);
+    rtl::Reference<SotStorage> xVBAStream = 
pRootStorage->OpenSotStorage("VBA", StreamMode::READWRITE);
+    rtl::Reference<SotStorageStream> pDirStream = 
xVBAStream->OpenSotStream("dir", StreamMode::READWRITE);
 
-    tools::SvRef<SotStorageStream> pVBAProjectStream = 
xVBAStream->OpenSotStream("_VBA_PROJECT", StreamMode::READWRITE);
-    tools::SvRef<SotStorageStream> pPROJECTStream = 
pRootStorage->OpenSotStream("PROJECT", StreamMode::READWRITE);
-    tools::SvRef<SotStorageStream> pPROJECTwmStream = 
pRootStorage->OpenSotStream("PROJECTwm", StreamMode::READWRITE);
+    rtl::Reference<SotStorageStream> pVBAProjectStream = 
xVBAStream->OpenSotStream("_VBA_PROJECT", StreamMode::READWRITE);
+    rtl::Reference<SotStorageStream> pPROJECTStream = 
pRootStorage->OpenSotStream("PROJECT", StreamMode::READWRITE);
+    rtl::Reference<SotStorageStream> pPROJECTwmStream = 
pRootStorage->OpenSotStream("PROJECTwm", StreamMode::READWRITE);
 
     const rtl_TextEncoding eTextEncoding = getVBATextEncoding();
 
@@ -1106,7 +1106,7 @@ void VbaExport::exportVBA(SotStorage* pRootStorage)
     for (sal_Int32 i = 0; i < n; ++i)
     {
         const OUString& rModuleName = aElementNames[aLibraryMap[i]];
-        tools::SvRef<SotStorageStream> pModuleStream = 
xVBAStream->OpenSotStream(rModuleName, StreamMode::READWRITE);
+        rtl::Reference<SotStorageStream> pModuleStream = 
xVBAStream->OpenSotStream(rModuleName, StreamMode::READWRITE);
         css::uno::Any aCode = xNameContainer->getByName(rModuleName);
         css::script::ModuleInfo aModuleInfo = 
xModuleInfo->getModuleInfo(rModuleName);
         OUString aSourceCode;
diff --git a/sc/inc/global.hxx b/sc/inc/global.hxx
index 1e42e6060d0e..20ee9e78fcd7 100644
--- a/sc/inc/global.hxx
+++ b/sc/inc/global.hxx
@@ -23,7 +23,7 @@
 #include "typedstrdata.hxx"
 #include <i18nlangtag/lang.h>
 #include <svx/svdtypes.hxx>
-#include <tools/ref.hxx>
+#include <rtl/ref.hxx>
 #include <sal/types.h>
 #include <com/sun/star/i18n/CollatorOptions.hpp>
 #include "scdllapi.h"
@@ -589,7 +589,7 @@ public:
     /// Vertical pixel per twips factor.
     SC_DLLPUBLIC static double              nScreenPPTY;
 
-    static tools::SvRef<ScDocShell>   xDrawClipDocShellRef;
+    static rtl::Reference<ScDocShell> xDrawClipDocShellRef;
 
     static sal_uInt16           nDefFontHeight;
     SC_DLLPUBLIC static sal_uInt16           nStdRowHeight;
diff --git a/sc/inc/rangelst.hxx b/sc/inc/rangelst.hxx
index efbc697b4ca4..f1618b06c9d9 100644
--- a/sc/inc/rangelst.hxx
+++ b/sc/inc/rangelst.hxx
@@ -23,6 +23,7 @@
 #include "address.hxx"
 #include <ostream>
 #include <vector>
+#include <tools/ref.hxx>
 #include <sal/types.h>
 
 class ScDocument;
diff --git a/sc/inc/tablink.hxx b/sc/inc/tablink.hxx
index 6233ddc912fd..ae235812795e 100644
--- a/sc/inc/tablink.hxx
+++ b/sc/inc/tablink.hxx
@@ -76,8 +76,7 @@ class SfxFilter;
 class SC_DLLPUBLIC ScDocumentLoader
 {
 private:
-    ScDocShell*         pDocShell;
-    SfxObjectShellRef   aRef;
+    rtl::Reference<ScDocShell> pDocShell;
     SfxMedium*          pMedium;
 
 public:
@@ -85,9 +84,11 @@ public:
                      sal_uInt32 nRekCnt = 0, weld::Window* pInteractionParent 
= nullptr,
                      css::uno::Reference<css::io::XInputStream> xInputStream
                      = css::uno::Reference<css::io::XInputStream>());
+    ScDocumentLoader(const ScDocumentLoader&) = delete;
+    ScDocumentLoader(ScDocumentLoader&&) = delete;
     ~ScDocumentLoader();
     ScDocument*         GetDocument();
-    ScDocShell*         GetDocShell()       { return pDocShell; }
+    ScDocShell*         GetDocShell()       { return pDocShell.get(); }
     bool                IsError() const;
     OUString       GetTitle() const;
 
diff --git a/sc/source/core/data/global.cxx b/sc/source/core/data/global.cxx
index 27c5a51a46c1..f48970703699 100644
--- a/sc/source/core/data/global.cxx
+++ b/sc/source/core/data/global.cxx
@@ -76,7 +76,7 @@
 #include <sharedstringpoolpurge.hxx>
 #include <formulaopt.hxx>
 
-tools::SvRef<ScDocShell>  ScGlobal::xDrawClipDocShellRef;
+rtl::Reference<ScDocShell> ScGlobal::xDrawClipDocShellRef;
 std::unique_ptr<SvxSearchItem> ScGlobal::xSearchItem;
 std::unique_ptr<ScAutoFormat> ScGlobal::xAutoFormat;
 std::atomic<LegacyFuncCollection*> ScGlobal::pLegacyFuncCollection(nullptr);
diff --git a/sc/source/filter/excel/excel.cxx b/sc/source/filter/excel/excel.cxx
index 2236c433400d..4e0961aa9ed9 100644
--- a/sc/source/filter/excel/excel.cxx
+++ b/sc/source/filter/excel/excel.cxx
@@ -61,13 +61,13 @@ static void lcl_getListOfStreams(SotStorage * pStorage, 
comphelper::SequenceAsHa
         OUString sStreamFullName = sPrefix.size() ? OUString::Concat(sPrefix) 
+ "/" + aElement.GetName() : aElement.GetName();
         if (aElement.IsStorage())
         {
-            tools::SvRef<SotStorage> xSubStorage = 
pStorage->OpenSotStorage(aElement.GetName(), StreamMode::STD_READ | 
StreamMode::SHARE_DENYALL);
+            rtl::Reference<SotStorage> xSubStorage = 
pStorage->OpenSotStorage(aElement.GetName(), StreamMode::STD_READ | 
StreamMode::SHARE_DENYALL);
             lcl_getListOfStreams(xSubStorage.get(), aStreamsData, 
sStreamFullName);
         }
         else
         {
             // Read stream
-            tools::SvRef<SotStorageStream> rStream = 
pStorage->OpenSotStream(aElement.GetName(), StreamMode::READ | 
StreamMode::SHARE_DENYALL);
+            rtl::Reference<SotStorageStream> rStream = 
pStorage->OpenSotStream(aElement.GetName(), StreamMode::READ | 
StreamMode::SHARE_DENYALL);
             if (rStream.is())
             {
                 sal_Int32 nStreamSize = rStream->GetSize();
@@ -81,9 +81,9 @@ static void lcl_getListOfStreams(SotStorage * pStorage, 
comphelper::SequenceAsHa
     }
 }
 
-static tools::SvRef<SotStorage> lcl_DRMDecrypt(const SfxMedium& rMedium, const 
tools::SvRef<SotStorage>& rStorage, std::shared_ptr<SvStream>& rNewStorageStrm)
+static rtl::Reference<SotStorage> lcl_DRMDecrypt(const SfxMedium& rMedium, 
const rtl::Reference<SotStorage>& rStorage, std::shared_ptr<SvStream>& 
rNewStorageStrm)
 {
-    tools::SvRef<SotStorage> aNewStorage;
+    rtl::Reference<SotStorage> aNewStorage;
 
     // We have DRM encrypted storage. We should try to decrypt it first, if we 
can
     uno::Sequence< uno::Any > aArguments;
@@ -109,7 +109,7 @@ static tools::SvRef<SotStorage> lcl_DRMDecrypt(const 
SfxMedium& rMedium, const t
             return aNewStorage;
         }
 
-        tools::SvRef<SotStorageStream> rContentStream = 
rStorage->OpenSotStream("      DRMContent", StreamMode::READ | 
StreamMode::SHARE_DENYALL);
+        rtl::Reference<SotStorageStream> rContentStream = 
rStorage->OpenSotStream("    DRMContent", StreamMode::READ | 
StreamMode::SHARE_DENYALL);
         if (!rContentStream.is())
         {
             return aNewStorage;
@@ -166,8 +166,8 @@ ErrCode ScFormatFilterPluginImpl::ScImportExcel( SfxMedium& 
rMedium, ScDocument*
     XclBiff eBiff = EXC_BIFF_UNKNOWN;   // The BIFF version of the main stream.
 
     // try to open an OLE storage
-    tools::SvRef<SotStorage> xRootStrg;
-    tools::SvRef<SotStorageStream> xStrgStrm;
+    rtl::Reference<SotStorage> xRootStrg;
+    rtl::Reference<SotStorageStream> xStrgStrm;
     std::shared_ptr<SvStream> aNewStorageStrm;
     if( SotStorage::IsStorageFile( pMedStrm ) )
     {
@@ -180,18 +180,18 @@ ErrCode ScFormatFilterPluginImpl::ScImportExcel( 
SfxMedium& rMedium, ScDocument*
     if( xRootStrg.is() )
     {
         // Check if there is DRM encryption in storage
-        tools::SvRef<SotStorageStream> xDRMStrm = 
ScfTools::OpenStorageStreamRead(xRootStrg, " DRMContent");
+        rtl::Reference<SotStorageStream> xDRMStrm = 
ScfTools::OpenStorageStreamRead(xRootStrg, "       DRMContent");
         if (xDRMStrm.is())
         {
             xRootStrg = lcl_DRMDecrypt(rMedium, xRootStrg, aNewStorageStrm);
         }
 
         // try to open the "Book" stream
-        tools::SvRef<SotStorageStream> xBookStrm = 
ScfTools::OpenStorageStreamRead( xRootStrg, EXC_STREAM_BOOK );
+        rtl::Reference<SotStorageStream> xBookStrm = 
ScfTools::OpenStorageStreamRead( xRootStrg, EXC_STREAM_BOOK );
         XclBiff eBookBiff = xBookStrm.is() ?  XclImpStream::DetectBiffVersion( 
*xBookStrm ) : EXC_BIFF_UNKNOWN;
 
         // try to open the "Workbook" stream
-        tools::SvRef<SotStorageStream> xWorkbookStrm = 
ScfTools::OpenStorageStreamRead( xRootStrg, EXC_STREAM_WORKBOOK );
+        rtl::Reference<SotStorageStream> xWorkbookStrm = 
ScfTools::OpenStorageStreamRead( xRootStrg, EXC_STREAM_WORKBOOK );
-e 
... etc. - the rest is truncated

Reply via email to