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             |   14 
 embeddedobj/source/msole/olecomponent.cxx        |  535 +++++++++++++----------
 embeddedobj/source/msole/olecomponent.hxx        |   12 
 embeddedobj/source/msole/oleembed.cxx            |   21 
 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 
 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 
 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/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/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 
 writerperfect/source/common/WPXSvInputStream.cxx |   30 -
 150 files changed, 903 insertions(+), 744 deletions(-)

New commits:
commit 7675663a767702b9c020a0a287782a330570bede
Author:     Mike Kaganski <mike.kagan...@collabora.com>
AuthorDate: Sun Mar 10 20:44:46 2024 +0600
Commit:     Mike Kaganski <mike.kagan...@collabora.com>
CommitDate: Mon Mar 11 12:50:02 2024 +0600

    Disallow closing document during generation of preview
    
    Using an external (Java) script that opens a document, and then quickly
    closes it, it may happen that the idle that generates a preview is still
    active, when the control thread calls XCloseable::close, which destroys
    still used document. This leads to use-after-free.
    
    This uses the close listener in SfxObjectShell, to veto closing document
    that is still in use by the preview generation code. After completion,
    when the caller of close() transferred ownership, then the call to close
    is repeated.
    
    Change-Id: I39691f61ad5141d7e8ee54723d5aef87f0bc01dc
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/164632
    Tested-by: Jenkins
    Reviewed-by: Mike Kaganski <mike.kagan...@collabora.com>

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 1230a418f7ae6e83d97da1eb945366aff57e3fa2
Author:     Mike Kaganski <mike.kagan...@collabora.com>
AuthorDate: Wed Mar 6 16:06:40 2024 +0600
Commit:     Mike Kaganski <mike.kagan...@collabora.com>
CommitDate: Mon Mar 11 11:51:07 2024 +0600

    Use weak reference to SfxObjectShell in SfxEventHint to avoid use-after-free
    
    The events may be processed after the shell has been destroyed. This is
    happening reliably after commit e2bfc34d146806a8f96be0cd2323d716f12cba4e
    (Reimplement OleComponentNative_Impl to use IGlobalInterfaceTable,
    2024-03-11) when controlling LibreOffice from external Java scripts; but
    obviously, it could happen before as well.
    
    Now SotObject inherits from cppu::OWeakObject, instead of SvRefBase.
    
    Change-Id: I73a3531499a3068c801c98f40de39bdf8ad90b2b
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/164458
    Tested-by: Mike Kaganski <mike.kagan...@collabora.com>
    Reviewed-by: Mike Kaganski <mike.kagan...@collabora.com>

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 );
         XclBiff eWorkbookBiff = xWorkbookStrm.is() ?  
XclImpStream::DetectBiffVersion( *xWorkbookStrm ) : EXC_BIFF_UNKNOWN;
 
         // decide which stream to use
@@ -286,7 +286,7 @@ static ErrCode lcl_ExportExcelBiff( SfxMedium& rMedium, 
ScDocument *pDocument,
     }
 
     // try to open an OLE storage
-    tools::SvRef<SotStorage> xRootStrg = new SotStorage( pMedStrm, false );
+    rtl::Reference<SotStorage> xRootStrg = new SotStorage(pMedStrm, false);
     if( xRootStrg->GetError() ) return SCERR_IMPORT_OPEN;
 
     // create BIFF dependent strings
@@ -305,7 +305,7 @@ static ErrCode lcl_ExportExcelBiff( SfxMedium& rMedium, 
ScDocument *pDocument,
     }
 
     // open the "Book"/"Workbook" stream
-    tools::SvRef<SotStorageStream> xStrgStrm = 
ScfTools::OpenStorageStreamWrite( xRootStrg, aStrmName );
+    rtl::Reference<SotStorageStream> xStrgStrm = 
ScfTools::OpenStorageStreamWrite( xRootStrg, aStrmName );
     if( !xStrgStrm.is() || xStrgStrm->GetError() ) return SCERR_IMPORT_OPEN;
 
     xStrgStrm->SetBufferSize( 0x8000 );     // still needed?
@@ -343,12 +343,12 @@ static ErrCode lcl_ExportExcelBiff( SfxMedium& rMedium, 
ScDocument *pDocument,
         uno::Reference<io::XInputStream > xInputStream(new 
utl::OSeekableInputStreamWrapper(pMedStrm, false));
         uno::Sequence<beans::NamedValue> aStreams = 
xPackageEncryption->encrypt(xInputStream);
 
-        tools::SvRef<SotStorage> xEncryptedRootStrg = new 
SotStorage(pOriginalMediaStrm, false);
-        for (const beans::NamedValue & aStreamData : std::as_const(aStreams))
+        rtl::Reference<SotStorage> xEncryptedRootStrg = new 
SotStorage(pOriginalMediaStrm, false);
+        for (const beans::NamedValue& aStreamData : aStreams)
         {
             // To avoid long paths split and open substorages recursively
             // Splitting paths manually, since comphelper::string::split is 
trimming special characters like 
-            tools::SvRef<SotStorage> pStorage = xEncryptedRootStrg.get();
+            rtl::Reference<SotStorage> pStorage = xEncryptedRootStrg;
             OUString sFileName;
             sal_Int32 idx = 0;
             do
@@ -373,7 +373,7 @@ static ErrCode lcl_ExportExcelBiff( SfxMedium& rMedium, 
ScDocument *pDocument,
                 break;
             }
 
-            tools::SvRef<SotStorageStream> pStream = 
pStorage->OpenSotStream(sFileName);
+            rtl::Reference<SotStorageStream> pStream = 
pStorage->OpenSotStream(sFileName);
             if (!pStream)
             {
                 eRet = ERRCODE_IO_GENERAL;
diff --git a/sc/source/filter/excel/excimp8.cxx 
b/sc/source/filter/excel/excimp8.cxx
index d5db209a119a..42698fcb6c42 100644
--- a/sc/source/filter/excel/excimp8.cxx
+++ b/sc/source/filter/excel/excimp8.cxx
@@ -318,7 +318,7 @@ void ImportExcel8::Feat()
 void ImportExcel8::ReadBasic()
 {
     SfxObjectShell* pShell = GetDocShell();
-    tools::SvRef<SotStorage> xRootStrg = GetRootStorage();
+    rtl::Reference<SotStorage> xRootStrg = GetRootStorage();
     const SvtFilterOptions& rFilterOpt = SvtFilterOptions::Get();
     if( !pShell || !xRootStrg.is() )
         return;
@@ -332,7 +332,7 @@ void ImportExcel8::ReadBasic()
             rFilterOpt.IsLoadExcelBasicExecutable() )
         {
             // see if we have the XCB stream
-            tools::SvRef<SotStorageStream> xXCB = xRootStrg->OpenSotStream( 
"XCB", StreamMode::STD_READ );
+            rtl::Reference<SotStorageStream> xXCB = xRootStrg->OpenSotStream( 
"XCB", StreamMode::STD_READ );
             if ( xXCB.is()|| ERRCODE_NONE == xXCB->GetError() )
             {
                 ScCTBWrapper wrapper;
@@ -409,7 +409,7 @@ void ImportExcel8::PostDocLoad()
         return;
 
     // BIFF5+ without storage is possible
-    tools::SvRef<SotStorage> xRootStrg = GetRootStorage();
+    rtl::Reference<SotStorage> xRootStrg = GetRootStorage();
     if( xRootStrg.is() ) try
     {
         uno::Reference< document::XDocumentPropertiesSupplier > xDPS( 
pShell->GetModel(), uno::UNO_QUERY_THROW );
diff --git a/sc/source/filter/excel/expop2.cxx 
b/sc/source/filter/excel/expop2.cxx
index ee8ba0fff2b9..078a6f3a62d2 100644
--- a/sc/source/filter/excel/expop2.cxx
+++ b/sc/source/filter/excel/expop2.cxx
@@ -72,7 +72,7 @@ ErrCode ExportBiff5::Write()
     SfxObjectShell* pDocShell = GetDocShell();
     OSL_ENSURE( pDocShell, "ExportBiff5::Write - no document shell" );
 
-    tools::SvRef<SotStorage> xRootStrg = GetRootStorage();
+    rtl::Reference<SotStorage> xRootStrg = GetRootStorage();
     OSL_ENSURE( xRootStrg.is(), "ExportBiff5::Write - no root storage" );
 
     VBAExportMode eVbaExportMode = VBAExportMode::NONE;
@@ -93,7 +93,7 @@ ErrCode ExportBiff5::Write()
         VbaExport aExport(pDocShell->GetModel());
         if (aExport.containsVBAProject())
         {
-            tools::SvRef<SotStorage> xVBARoot = 
xRootStrg->OpenSotStorage("_VBA_PROJECT_CUR");
+            rtl::Reference<SotStorage> xVBARoot = 
xRootStrg->OpenSotStorage("_VBA_PROJECT_CUR");
             aExport.exportVBA( xVBARoot.get() );
         }
     }
diff --git a/sc/source/filter/excel/xepivot.cxx 
b/sc/source/filter/excel/xepivot.cxx
index a8aab5b694d3..c03c35b2cdb5 100644
--- a/sc/source/filter/excel/xepivot.cxx
+++ b/sc/source/filter/excel/xepivot.cxx
@@ -810,8 +810,8 @@ void XclExpPivotCache::WriteDConName( XclExpStream& rStrm ) 
const
 
 void XclExpPivotCache::WriteCacheStream()
 {
-    tools::SvRef<SotStorage> xSvStrg = OpenStorage( EXC_STORAGE_PTCACHE );
-    tools::SvRef<SotStorageStream> xSvStrm = OpenStream( xSvStrg, 
ScfTools::GetHexStr( maPCInfo.mnStrmId ) );
+    rtl::Reference<SotStorage> xSvStrg = OpenStorage(EXC_STORAGE_PTCACHE);
+    rtl::Reference<SotStorageStream> xSvStrm = OpenStream( xSvStrg, 
ScfTools::GetHexStr( maPCInfo.mnStrmId ) );
     if( !xSvStrm.is() )
         return;
 
diff --git a/sc/source/filter/excel/xeroot.cxx 
b/sc/source/filter/excel/xeroot.cxx
index 807a24aae60b..3d49def71ef5 100644
--- a/sc/source/filter/excel/xeroot.cxx
+++ b/sc/source/filter/excel/xeroot.cxx
@@ -54,7 +54,7 @@ using namespace ::com::sun::star;
 // Global data ================================================================
 
 XclExpRootData::XclExpRootData( XclBiff eBiff, SfxMedium& rMedium,
-        const tools::SvRef<SotStorage>& xRootStrg, ScDocument& rDoc, 
rtl_TextEncoding eTextEnc ) :
+        const rtl::Reference<SotStorage>& xRootStrg, ScDocument& rDoc, 
rtl_TextEncoding eTextEnc ) :
     XclRootData( eBiff, rMedium, xRootStrg, rDoc, eTextEnc, true )
 {
     mbRelUrl = mrMedium.IsRemote()
diff --git a/sc/source/filter/excel/xestream.cxx 
b/sc/source/filter/excel/xestream.cxx
index 75c241493b07..76d3da1c028d 100644
--- a/sc/source/filter/excel/xestream.cxx
+++ b/sc/source/filter/excel/xestream.cxx
@@ -1023,7 +1023,7 @@ bool XclExpXmlStream::exportDocument()
     // NOTE: Don't use SotStorage or SvStream any more, and never call
     // SfxMedium::GetOutStream() anywhere in the xlsx export filter code!
     // Instead, write via XOutputStream instance.
-    tools::SvRef<SotStorage> rStorage = static_cast<SotStorage*>(nullptr);
+    rtl::Reference<SotStorage> rStorage;
     drawingml::DrawingML::ResetMlCounters();
     drawingml::DrawingML::PushExportGraphics();
 
@@ -1098,7 +1098,7 @@ bool XclExpXmlStream::exportDocument()
         if (aExport.containsVBAProject())
         {
             SvMemoryStream aVbaStream(4096, 4096);
-            tools::SvRef<SotStorage> pVBAStorage(new SotStorage(aVbaStream));
+            rtl::Reference<SotStorage> pVBAStorage(new SotStorage(aVbaStream));
             aExport.exportVBA( pVBAStorage.get() );
             aVbaStream.Seek(0);
             css::uno::Reference<css::io::XInputStream> xVBAStream(
diff --git a/sc/source/filter/excel/xiescher.cxx 
b/sc/source/filter/excel/xiescher.cxx
index 2838347b3f14..5a732750105f 100644
--- a/sc/source/filter/excel/xiescher.cxx
+++ b/sc/source/filter/excel/xiescher.cxx
@@ -3513,7 +3513,7 @@ rtl::Reference<SdrObject> 
XclImpDffConverter::CreateSdrObject( const XclImpPictu
         else
         {
             SfxObjectShell* pDocShell = GetDocShell();
-            tools::SvRef<SotStorage> xSrcStrg = GetRootStorage();
+            rtl::Reference<SotStorage> xSrcStrg = GetRootStorage();
             OUString aStrgName = rPicObj.GetOleStorageName();
             if( pDocShell && xSrcStrg.is() && (!aStrgName.isEmpty()) )
             {
diff --git a/sc/source/filter/excel/xipivot.cxx 
b/sc/source/filter/excel/xipivot.cxx
index d8d4eaa631c4..fa9b0f38a8b9 100644
--- a/sc/source/filter/excel/xipivot.cxx
+++ b/sc/source/filter/excel/xipivot.cxx
@@ -714,8 +714,8 @@ void XclImpPivotCache::ReadPivotCacheStream( const 
XclImpStream& rStrm )
     }
 
     // open pivot cache storage stream
-    tools::SvRef<SotStorage> xSvStrg = OpenStorage( EXC_STORAGE_PTCACHE );
-    tools::SvRef<SotStorageStream> xSvStrm = OpenStream( xSvStrg, 
ScfTools::GetHexStr( mnStrmId ) );
+    rtl::Reference<SotStorage> xSvStrg = OpenStorage(EXC_STORAGE_PTCACHE);
+    rtl::Reference<SotStorageStream> xSvStrm = OpenStream(xSvStrg, 
ScfTools::GetHexStr(mnStrmId));
     if( !xSvStrm.is() )
         return;
 
diff --git a/sc/source/filter/excel/xiroot.cxx 
b/sc/source/filter/excel/xiroot.cxx
index 673414745017..d8959affe5fd 100644
--- a/sc/source/filter/excel/xiroot.cxx
+++ b/sc/source/filter/excel/xiroot.cxx
@@ -42,7 +42,7 @@
 // Global data ================================================================
 
 XclImpRootData::XclImpRootData( XclBiff eBiff, SfxMedium& rMedium,
-        const tools::SvRef<SotStorage>& xRootStrg, ScDocument& rDoc, 
rtl_TextEncoding eTextEnc ) :
+        const rtl::Reference<SotStorage>& xRootStrg, ScDocument& rDoc, 
rtl_TextEncoding eTextEnc ) :
     XclRootData( eBiff, rMedium, xRootStrg, rDoc, eTextEnc, false ),
     mxDocImport(std::make_shared<ScDocumentImport>(rDoc)),
     mbHasCodePage( false ),
diff --git a/sc/source/filter/excel/xlroot.cxx 
b/sc/source/filter/excel/xlroot.cxx
index 47f5ff7806c1..16968980eab1 100644
--- a/sc/source/filter/excel/xlroot.cxx
+++ b/sc/source/filter/excel/xlroot.cxx
@@ -82,7 +82,7 @@ XclDebugObjCounter::~XclDebugObjCounter()
 #endif
 
 XclRootData::XclRootData( XclBiff eBiff, SfxMedium& rMedium,
-        tools::SvRef<SotStorage> xRootStrg, ScDocument& rDoc, rtl_TextEncoding 
eTextEnc, bool bExport ) :
+        rtl::Reference<SotStorage> xRootStrg, ScDocument& rDoc, 
rtl_TextEncoding eTextEnc, bool bExport ) :
     meBiff( eBiff ),
     meOutput( EXC_OUTPUT_BINARY ),
     mrMedium( rMedium ),
@@ -255,30 +255,30 @@ uno::Sequence< beans::NamedValue > 
XclRoot::RequestEncryptionData( ::comphelper:
 
 bool XclRoot::HasVbaStorage() const
 {
-    tools::SvRef<SotStorage> xRootStrg = GetRootStorage();
+    rtl::Reference<SotStorage> xRootStrg = GetRootStorage();
     return xRootStrg.is() && xRootStrg->IsContained( EXC_STORAGE_VBA_PROJECT );
 }
 
-tools::SvRef<SotStorage> XclRoot::OpenStorage( tools::SvRef<SotStorage> const 
& xStrg, const OUString& rStrgName ) const
+rtl::Reference<SotStorage> XclRoot::OpenStorage( rtl::Reference<SotStorage> 
const & xStrg, const OUString& rStrgName ) const
 {
     return mrData.mbExport ?
         ScfTools::OpenStorageWrite( xStrg, rStrgName ) :
         ScfTools::OpenStorageRead( xStrg, rStrgName );
 }
 
-tools::SvRef<SotStorage> XclRoot::OpenStorage( const OUString& rStrgName ) 
const
+rtl::Reference<SotStorage> XclRoot::OpenStorage(const OUString& rStrgName) 
const
 {
     return OpenStorage( GetRootStorage(), rStrgName );
 }
 
-tools::SvRef<SotStorageStream> XclRoot::OpenStream( tools::SvRef<SotStorage> 
const & xStrg, const OUString& rStrmName ) const
+rtl::Reference<SotStorageStream> XclRoot::OpenStream( 
rtl::Reference<SotStorage> const & xStrg, const OUString& rStrmName ) const
 {
     return mrData.mbExport ?
         ScfTools::OpenStorageStreamWrite( xStrg, rStrmName ) :
         ScfTools::OpenStorageStreamRead( xStrg, rStrmName );
 }
 
-tools::SvRef<SotStorageStream> XclRoot::OpenStream( const OUString& rStrmName 
) const
+rtl::Reference<SotStorageStream> XclRoot::OpenStream(const OUString& 
rStrmName) const
 {
     return OpenStream( GetRootStorage(), rStrmName );
 }
diff --git a/sc/source/filter/ftools/ftools.cxx 
b/sc/source/filter/ftools/ftools.cxx
index 5c219e3c3d3a..7ecb9f4dcca8 100644
--- a/sc/source/filter/ftools/ftools.cxx
+++ b/sc/source/filter/ftools/ftools.cxx
@@ -162,34 +162,34 @@ OUString ScfTools::ConvertToScDefinedName(const OUString& 
rName )
 
 // *** streams and storages *** -----------------------------------------------
 
-tools::SvRef<SotStorage> ScfTools::OpenStorageRead( tools::SvRef<SotStorage> 
const & xStrg, const OUString& rStrgName )
+rtl::Reference<SotStorage> ScfTools::OpenStorageRead( 
rtl::Reference<SotStorage> const & xStrg, const OUString& rStrgName )
 {
-    tools::SvRef<SotStorage> xSubStrg;
+    rtl::Reference<SotStorage> xSubStrg;
     if( xStrg.is() && xStrg->IsContained( rStrgName ) )
         xSubStrg = xStrg->OpenSotStorage( rStrgName, StreamMode::STD_READ );
     return xSubStrg;
 }
 
-tools::SvRef<SotStorage> ScfTools::OpenStorageWrite( tools::SvRef<SotStorage> 
const & xStrg, const OUString& rStrgName )
+rtl::Reference<SotStorage> ScfTools::OpenStorageWrite( 
rtl::Reference<SotStorage> const & xStrg, const OUString& rStrgName )
 {
-    tools::SvRef<SotStorage> xSubStrg;
+    rtl::Reference<SotStorage> xSubStrg;
     if( xStrg.is() )
         xSubStrg = xStrg->OpenSotStorage( rStrgName, StreamMode::STD_WRITE );
     return xSubStrg;
 }
 
-tools::SvRef<SotStorageStream> ScfTools::OpenStorageStreamRead( 
tools::SvRef<SotStorage> const & xStrg, const OUString& rStrmName )
+rtl::Reference<SotStorageStream> ScfTools::OpenStorageStreamRead( 
rtl::Reference<SotStorage> const & xStrg, const OUString& rStrmName )
 {
-    tools::SvRef<SotStorageStream> xStrm;
+    rtl::Reference<SotStorageStream> xStrm;
     if( xStrg.is() && xStrg->IsContained( rStrmName ) && xStrg->IsStream( 
rStrmName ) )
         xStrm = xStrg->OpenSotStream( rStrmName, StreamMode::STD_READ );
     return xStrm;
 }
 
-tools::SvRef<SotStorageStream> ScfTools::OpenStorageStreamWrite( 
tools::SvRef<SotStorage> const & xStrg, const OUString& rStrmName )
+rtl::Reference<SotStorageStream> ScfTools::OpenStorageStreamWrite( 
rtl::Reference<SotStorage> const & xStrg, const OUString& rStrmName )
 {
     OSL_ENSURE( !xStrg.is() || !xStrg->IsContained( rStrmName ), 
"ScfTools::OpenStorageStreamWrite - stream exists already" );
-    tools::SvRef<SotStorageStream> xStrm;
+    rtl::Reference<SotStorageStream> xStrm;
     if( xStrg.is() )
         xStrm = xStrg->OpenSotStream( rStrmName, StreamMode::STD_WRITE | 
StreamMode::TRUNC );
     return xStrm;
diff --git a/sc/source/filter/inc/XclImpChangeTrack.hxx 
b/sc/source/filter/inc/XclImpChangeTrack.hxx
index 532cc9e32464..e37586ca2276 100644
--- a/sc/source/filter/inc/XclImpChangeTrack.hxx
+++ b/sc/source/filter/inc/XclImpChangeTrack.hxx
@@ -52,7 +52,7 @@ private:
     OUString                    sOldUsername;
 
     std::unique_ptr<ScChangeTrack> pChangeTrack;
-    tools::SvRef<SotStorageStream> xInStrm;        // input stream
+    rtl::Reference<SotStorageStream> xInStrm; // input stream
     std::unique_ptr<XclImpStream>  pStrm;          // stream import class
     sal_uInt16                  nTabIdCount;
     bool                        bGlobExit;      // global exit loop
diff --git a/sc/source/filter/inc/ftools.hxx b/sc/source/filter/inc/ftools.hxx
index 1366a5197511..9fc866ed924a 100644
--- a/sc/source/filter/inc/ftools.hxx
+++ b/sc/source/filter/inc/ftools.hxx
@@ -24,7 +24,7 @@
 #include <limits>
 #include <string_view>
 
-#include <tools/ref.hxx>
+#include <rtl/ref.hxx>
 #include <filter.hxx>
 
 // Common macros ==============================================================
@@ -152,14 +152,14 @@ public:
 // *** streams and storages *** -----------------------------------------------
 
     /** Tries to open an existing storage with the specified name in the 
passed storage (read-only). */
-    static tools::SvRef<SotStorage> OpenStorageRead( tools::SvRef<SotStorage> 
const & xStrg, const OUString& rStrgName );
+    static rtl::Reference<SotStorage> OpenStorageRead( 
rtl::Reference<SotStorage> const & xStrg, const OUString& rStrgName );
     /** Creates and opens a storage with the specified name in the passed 
storage (read/write). */
-    static tools::SvRef<SotStorage> OpenStorageWrite( tools::SvRef<SotStorage> 
const & xStrg, const OUString& rStrgName );
+    static rtl::Reference<SotStorage> OpenStorageWrite( 
rtl::Reference<SotStorage> const & xStrg, const OUString& rStrgName );
 
     /** Tries to open an existing stream with the specified name in the passed 
storage (read-only). */
-    static tools::SvRef<SotStorageStream> OpenStorageStreamRead( 
tools::SvRef<SotStorage> const & xStrg, const OUString& rStrmName );
+    static rtl::Reference<SotStorageStream> OpenStorageStreamRead( 
rtl::Reference<SotStorage> const & xStrg, const OUString& rStrmName );
     /** Creates and opens a stream with the specified name in the passed 
storage (read/write). */
-    static tools::SvRef<SotStorageStream> OpenStorageStreamWrite( 
tools::SvRef<SotStorage> const & xStrg, const OUString& rStrmName );
+    static rtl::Reference<SotStorageStream> OpenStorageStreamWrite( 
rtl::Reference<SotStorage> const & xStrg, const OUString& rStrmName );
 
 // *** item handling *** ------------------------------------------------------
 
diff --git a/sc/source/filter/inc/xcl97esc.hxx 
b/sc/source/filter/inc/xcl97esc.hxx
index d7724d758981..b391e65543c7 100644
--- a/sc/source/filter/inc/xcl97esc.hxx
+++ b/sc/source/filter/inc/xcl97esc.hxx
@@ -101,7 +101,7 @@ public:
                             const tools::Rectangle* pChildAnchor );
 
 private:
-    tools::SvRef<SotStorageStream>  mxCtlsStrm;         /// The 'Ctls' stream.
+    rtl::Reference<SotStorageStream> mxCtlsStrm; /// The 'Ctls' stream.
     /** Creates a TBX form control OBJ record from the passed form control. */
     std::unique_ptr<XclExpTbxControlObj> CreateTBXCtrlObj(
                             css::uno::Reference< css::drawing::XShape > const 
& xShape,
diff --git a/sc/source/filter/inc/xeroot.hxx b/sc/source/filter/inc/xeroot.hxx
index 421a389a3737..f2c1984506c3 100644
--- a/sc/source/filter/inc/xeroot.hxx
+++ b/sc/source/filter/inc/xeroot.hxx
@@ -104,7 +104,7 @@ struct XclExpRootData : public XclRootData
     OStringBuffer       maStringBuf;        /// buffer to avoid massive 
OUString allocations
 
     explicit            XclExpRootData( XclBiff eBiff, SfxMedium& rMedium,
-                            const tools::SvRef<SotStorage>& xRootStrg, 
ScDocument& rDoc, rtl_TextEncoding eTextEnc );
+                            const rtl::Reference<SotStorage>& xRootStrg, 
ScDocument& rDoc, rtl_TextEncoding eTextEnc );
     virtual             ~XclExpRootData() override;
 };
 
diff --git a/sc/source/filter/inc/xiescher.hxx 
b/sc/source/filter/inc/xiescher.hxx
index 2079d68f11c1..eae971ad59c5 100644
--- a/sc/source/filter/inc/xiescher.hxx
+++ b/sc/source/filter/inc/xiescher.hxx
@@ -1026,7 +1026,7 @@ private:
     typedef std::shared_ptr< ScfProgressBar >     ScfProgressBarRef;
     typedef std::shared_ptr< XclImpDffConvData >  XclImpDffConvDataRef;
 
-    tools::SvRef<SotStorageStream> mxCtlsStrm;         /// The 'Ctls' stream 
for OCX form controls.
+    rtl::Reference<SotStorageStream> mxCtlsStrm; /// The 'Ctls' stream for OCX 
form controls.
     ScfProgressBarRef   mxProgress;         /// The progress bar used in 
ProcessObj().
     std::vector< XclImpDffConvDataRef >
                         maDataStack;     /// Stack for registered drawing 
managers.
diff --git a/sc/source/filter/inc/xiroot.hxx b/sc/source/filter/inc/xiroot.hxx
index d290a01db01d..cea998e5c4ac 100644
--- a/sc/source/filter/inc/xiroot.hxx
+++ b/sc/source/filter/inc/xiroot.hxx
@@ -120,7 +120,7 @@ struct XclImpRootData : public XclRootData
     bool                mbHasBasic;         /// true = document contains VB 
project.
 
     explicit            XclImpRootData( XclBiff eBiff, SfxMedium& rMedium,
-                            const tools::SvRef<SotStorage>& xRootStrg, 
ScDocument& rDoc, rtl_TextEncoding eTextEnc );
+                            const rtl::Reference<SotStorage>& xRootStrg, 
ScDocument& rDoc, rtl_TextEncoding eTextEnc );
     virtual             ~XclImpRootData() override;
 };
 
diff --git a/sc/source/filter/inc/xlroot.hxx b/sc/source/filter/inc/xlroot.hxx
index 10b4554e155f..031036c511c3 100644
--- a/sc/source/filter/inc/xlroot.hxx
+++ b/sc/source/filter/inc/xlroot.hxx
@@ -20,7 +20,7 @@
 #pragma once
 
 #include <i18nlangtag/lang.h>
-#include <tools/ref.hxx>
+#include <rtl/ref.hxx>
 #include <tools/long.hxx>
 #include "xlconst.hxx"
 #include <memory>
@@ -75,7 +75,7 @@ struct XclRootData
     XclBiff             meBiff;             /// Current BIFF version.
     XclOutput           meOutput;           /// Current Output format.
     SfxMedium&          mrMedium;           /// The medium to import from.
-    tools::SvRef<SotStorage>       mxRootStrg;         /// The root OLE 
storage of imported/exported file.
+    rtl::Reference<SotStorage> mxRootStrg;  /// The root OLE storage of 
imported/exported file.
     ScDocument&         mrDoc;              /// The source or destination 
document.
     OUString            maDocUrl;           /// Document URL of 
imported/exported file.
     OUString            maBasePath;         /// Base path of imported/exported 
file (path of maDocUrl).
@@ -109,7 +109,7 @@ struct XclRootData
     const bool          mbExport;           /// false = Import, true = Export.
 
     explicit            XclRootData( XclBiff eBiff, SfxMedium& rMedium,
-                            tools::SvRef<SotStorage> xRootStrg, ScDocument& 
rDoc,
+                            rtl::Reference<SotStorage> xRootStrg, ScDocument& 
rDoc,
                             rtl_TextEncoding eTextEnc, bool bExport );
     virtual             ~XclRootData();
 };
@@ -183,18 +183,18 @@ public:
 
     /** Returns the OLE2 root storage of the imported/exported file.
         @return  Pointer to root storage or 0, if the file is a simple stream. 
*/
-    const tools::SvRef<SotStorage>& GetRootStorage() const { return 
mrData.mxRootStrg; }
+    const rtl::Reference<SotStorage>& GetRootStorage() const { return 
mrData.mxRootStrg; }
     /** Returns true, if the document contains a VBA storage. */
     bool                HasVbaStorage() const;
 
     /** Tries to open a storage as child of the specified storage for reading 
or writing. */
-    tools::SvRef<SotStorage>       OpenStorage( tools::SvRef<SotStorage> const 
& xStrg, const OUString& rStrgName ) const;
+    rtl::Reference<SotStorage>       OpenStorage( rtl::Reference<SotStorage> 
const & xStrg, const OUString& rStrgName ) const;
     /** Tries to open a storage as child of the root storage for reading or 
writing. */
-    tools::SvRef<SotStorage>       OpenStorage( const OUString& rStrgName ) 
const;
+    rtl::Reference<SotStorage> OpenStorage(const OUString& rStrgName) const;
     /** Tries to open a new stream in the specified storage for reading or 
writing. */
-    tools::SvRef<SotStorageStream> OpenStream( tools::SvRef<SotStorage> const 
& xStrg, const OUString& rStrmName ) const;
+    rtl::Reference<SotStorageStream> OpenStream( rtl::Reference<SotStorage> 
const & xStrg, const OUString& rStrmName ) const;
     /** Tries to open a new stream in the root storage for reading or writing. 
*/
-    tools::SvRef<SotStorageStream> OpenStream( const OUString& rStrmName ) 
const;
+    rtl::Reference<SotStorageStream> OpenStream(const OUString& rStrmName) 
const;
 
     /** Returns reference to the destination document (import) or source 
document (export). */
     ScDocument& GetDoc() const;
diff --git a/sc/source/filter/xcl97/XclExpChangeTrack.cxx 
b/sc/source/filter/xcl97/XclExpChangeTrack.cxx
index 57fd143bf659..f6319f534527 100644
--- a/sc/source/filter/xcl97/XclExpChangeTrack.cxx
+++ b/sc/source/filter/xcl97/XclExpChangeTrack.cxx
@@ -1567,7 +1567,7 @@ void XclExpChangeTrack::PushActionRecord( const 
ScChangeAction& rAction )
 bool XclExpChangeTrack::WriteUserNamesStream()
 {
     bool bRet = false;
-    tools::SvRef<SotStorageStream> xSvStrm = OpenStream( EXC_STREAM_USERNAMES 
);
+    rtl::Reference<SotStorageStream> xSvStrm = 
OpenStream(EXC_STREAM_USERNAMES);
     OSL_ENSURE( xSvStrm.is(), "XclExpChangeTrack::WriteUserNamesStream - no 
stream" );
     if( xSvStrm.is() )
     {
@@ -1590,7 +1590,7 @@ void XclExpChangeTrack::Write()
     if( !WriteUserNamesStream() )
         return;
 
-    tools::SvRef<SotStorageStream> xSvStrm = OpenStream( EXC_STREAM_REVLOG );
+    rtl::Reference<SotStorageStream> xSvStrm = OpenStream(EXC_STREAM_REVLOG);
     OSL_ENSURE( xSvStrm.is(), "XclExpChangeTrack::Write - no stream" );
     if( xSvStrm.is() )
     {
diff --git a/sc/source/filter/xcl97/XclImpChangeTrack.cxx 
b/sc/source/filter/xcl97/XclImpChangeTrack.cxx
index 6bbd4c2e18aa..909ed9002828 100644
--- a/sc/source/filter/xcl97/XclImpChangeTrack.cxx
+++ b/sc/source/filter/xcl97/XclImpChangeTrack.cxx
@@ -44,7 +44,7 @@ XclImpChangeTrack::XclImpChangeTrack( const XclImpRoot& 
rRoot, const XclImpStrea
     // Verify that the User Names stream exists before going any further. 
Excel adds both
     // "Revision Log" and "User Names" streams when Change Tracking is active 
but the Revision log
     // remains if Change Tracking is turned off.
-    tools::SvRef<SotStorageStream> xUserStrm = OpenStream( 
EXC_STREAM_USERNAMES );
+    rtl::Reference<SotStorageStream> xUserStrm = OpenStream( 
EXC_STREAM_USERNAMES );
     if( !xUserStrm.is() )
         return;
 
diff --git a/sc/source/filter/xcl97/xcl97rec.cxx 
b/sc/source/filter/xcl97/xcl97rec.cxx
index 8f930c80993b..7d6f14761550 100644
--- a/sc/source/filter/xcl97/xcl97rec.cxx
+++ b/sc/source/filter/xcl97/xcl97rec.cxx
@@ -1016,7 +1016,7 @@ void XclObjOle::WriteSubRecs( XclExpStream& rStrm )
     sal_uInt32          nPictureId = 
sal_uInt32(reinterpret_cast<sal_uIntPtr>(this) >> 2);
     o3tl::sprintf( aBuf, "%08X", static_cast< unsigned int >( nPictureId ) );
     aStorageName += OUString::createFromAscii(aBuf);
-    tools::SvRef<SotStorage>    xOleStg = pRootStorage->OpenSotStorage( 
aStorageName );
+    rtl::Reference<SotStorage> xOleStg = 
pRootStorage->OpenSotStorage(aStorageName);
     if( !xOleStg.is() )
         return;
 
diff --git a/sc/source/ui/app/drwtrans.cxx b/sc/source/ui/app/drwtrans.cxx
index ed83dc893ae5..9777c01f57da 100644
--- a/sc/source/ui/app/drwtrans.cxx
+++ b/sc/source/ui/app/drwtrans.cxx
@@ -669,13 +669,12 @@ void ScDrawTransferObj::InitDocShell()
     if ( m_aDocShellRef.is() )
         return;
 
-    ScDocShell* pDocSh = new ScDocShell;
-    m_aDocShellRef = pDocSh;      // ref must be there before InitNew
+    m_aDocShellRef = new ScDocShell;      // ref must be there before InitNew
 
-    pDocSh->DoInitNew();
+    m_aDocShellRef->DoInitNew();
 
-    ScDocument& rDestDoc = pDocSh->GetDocument();
-    rDestDoc.InitDrawLayer( pDocSh );
+    ScDocument& rDestDoc = m_aDocShellRef->GetDocument();
+    rDestDoc.InitDrawLayer(m_aDocShellRef.get());
 
     SdrModel* pDestModel = rDestDoc.GetDrawLayer();
     // #i71538# use complete SdrViews
@@ -705,18 +704,18 @@ void ScDrawTransferObj::InitDocShell()
     }
 
     tools::Rectangle aDestArea( Point(), m_aSrcSize );
-    pDocSh->SetVisArea( aDestArea );
+    m_aDocShellRef->SetVisArea(aDestArea);
 
     ScViewOptions aViewOpt( rDestDoc.GetViewOptions() );
     aViewOpt.SetOption( VOPT_GRID, false );
     rDestDoc.SetViewOptions( aViewOpt );
 
-    ScViewData aViewData( *pDocSh, nullptr );
+    ScViewData aViewData(*m_aDocShellRef, nullptr);
     aViewData.SetTabNo( 0 );
     aViewData.SetScreen( aDestArea );
     aViewData.SetCurX( 0 );
     aViewData.SetCurY( 0 );
-    pDocSh->UpdateOle(aViewData, true);
+    m_aDocShellRef->UpdateOle(aViewData, true);
 }
 
 const css::uno::Sequence< sal_Int8 >& ScDrawTransferObj::getUnoTunnelId()
diff --git a/sc/source/ui/app/seltrans.cxx b/sc/source/ui/app/seltrans.cxx
index 7122afab9bdb..3c0345e9466b 100644
--- a/sc/source/ui/app/seltrans.cxx
+++ b/sc/source/ui/app/seltrans.cxx
@@ -293,8 +293,7 @@ void ScSelectionTransferObj::CreateCellData()
                 // SetDragHandlePos is not used - there is no mouse position
                 //? pTransferObj->SetVisibleTab( nTab );
 
-                SfxObjectShellRef aPersistRef( aDragShellRef.get() );
-                pTransferObj->SetDrawPersist( aPersistRef );    // keep 
persist for ole objects alive
+                pTransferObj->SetDrawPersist(aDragShellRef); // keep persist 
for ole objects alive
 
                 pTransferObj->SetDragSource( pDocSh, aNewMark );
 
@@ -340,8 +339,7 @@ void ScSelectionTransferObj::CreateDrawData()
 
             rtl::Reference<ScDrawTransferObj> pTransferObj = new 
ScDrawTransferObj( std::move(pModel), pDocSh, std::move(aObjDesc) );
 
-            SfxObjectShellRef aPersistRef( aDragShellRef.get() );
-            pTransferObj->SetDrawPersist( aPersistRef );    // keep persist 
for ole objects alive
+            pTransferObj->SetDrawPersist(aDragShellRef); // keep persist for 
ole objects alive
             pTransferObj->SetDragSource( pDrawView );       // copies selection
 
             mxDrawData = pTransferObj;
diff --git a/sc/source/ui/app/transobj.cxx b/sc/source/ui/app/transobj.cxx
index 9f6729d46a43..5c7b379016e7 100644
--- a/sc/source/ui/app/transobj.cxx
+++ b/sc/source/ui/app/transobj.cxx
@@ -664,12 +664,11 @@ void ScTransferObj::InitDocShell(bool bLimitToPageSize)
     if ( m_aDocShellRef.is() )
         return;
 
-    ScDocShell* pDocSh = new ScDocShell;
-    m_aDocShellRef = pDocSh;      // ref must be there before InitNew
+    m_aDocShellRef = new ScDocShell; // ref must be there before InitNew
 
-    pDocSh->DoInitNew();
+    m_aDocShellRef->DoInitNew();
 
-    ScDocument& rDestDoc = pDocSh->GetDocument();
+    ScDocument& rDestDoc = m_aDocShellRef->GetDocument();
     ScMarkData aDestMark(rDestDoc.GetSheetLimits());
     aDestMark.SelectTable( 0, true );
 
@@ -719,7 +718,7 @@ void ScTransferObj::InitDocShell(bool bLimitToPageSize)
     }
 
     if (m_pDoc->GetDrawLayer() || m_pDoc->HasNotes())
-        pDocSh->MakeDrawLayer();
+        m_aDocShellRef->MakeDrawLayer();
 
     //  cell range is copied to the original position, but on the first sheet
     //  -> bCutMode must be set
@@ -755,7 +754,7 @@ void ScTransferObj::InitDocShell(bool bLimitToPageSize)
         pDestPool->CopyStyleFrom( pStylePool, aStyleName, SfxStyleFamily::Page 
);
     }
 
-    ScViewData aViewData( *pDocSh, nullptr );
+    ScViewData aViewData(*m_aDocShellRef, nullptr);
     aViewData.SetScreen( nStartX,nStartY, nEndX,nEndY );
     aViewData.SetCurX( nStartX );
     aViewData.SetCurY( nStartY );
@@ -796,14 +795,14 @@ void ScTransferObj::InitDocShell(bool bLimitToPageSize)
     nSizeX = o3tl::convert(nSizeX, o3tl::Length::twip, o3tl::Length::mm100);
     nSizeY = o3tl::convert(nSizeY, o3tl::Length::twip, o3tl::Length::mm100);
 
-//      pDocSh->SetVisAreaSize( Size(nSizeX,nSizeY) );
+//      m_aDocShellRef->SetVisAreaSize( Size(nSizeX,nSizeY) );
 
     tools::Rectangle aNewArea( Point(nPosX,nPosY), Size(nSizeX,nSizeY) );
     //TODO/LATER: why twice?!
-    //pDocSh->SvInPlaceObject::SetVisArea( aNewArea );
-    pDocSh->SetVisArea( aNewArea );
+    //m_aDocShellRef->SvInPlaceObject::SetVisArea( aNewArea );
+    m_aDocShellRef->SetVisArea(aNewArea);
 
-    pDocSh->UpdateOle(aViewData, true);
+    m_aDocShellRef->UpdateOle(aViewData, true);
 
     //! SetDocumentModified?
     if ( rDestDoc.IsChartListenerCollectionNeedsUpdate() )
diff --git a/sc/source/ui/docshell/arealink.cxx 
b/sc/source/ui/docshell/arealink.cxx
index 64fb1e963aaf..b80013539ae9 100644
--- a/sc/source/ui/docshell/arealink.cxx
+++ b/sc/source/ui/docshell/arealink.cxx
@@ -249,8 +249,7 @@ bool ScAreaLink::Refresh( const OUString& rNewFile, const 
OUString& rNewFilter,
     SfxMedium* pMed = ScDocumentLoader::CreateMedium( aNewUrl, pFilter, 
aOptions);
 
     // aRef->DoClose() will be closed explicitly, but it is still more safe to 
use SfxObjectShellLock here
-    ScDocShell* pSrcShell = new ScDocShell(SfxModelFlags::EMBEDDED_OBJECT | 
SfxModelFlags::DISABLE_EMBEDDED_SCRIPTS);
-    SfxObjectShellLock aRef = pSrcShell;
+    rtl::Reference<ScDocShell> pSrcShell = new 
ScDocShell(SfxModelFlags::EMBEDDED_OBJECT | 
SfxModelFlags::DISABLE_EMBEDDED_SCRIPTS);
     pSrcShell->DoLoad(pMed);
 
     ScDocument& rSrcDoc = pSrcShell->GetDocument();
@@ -475,7 +474,7 @@ bool ScAreaLink::Refresh( const OUString& rNewFile, const 
OUString& rNewFilter,
 
     //  clean up
 
-    aRef->DoClose();
+    pSrcShell->DoClose();
 
     rDoc.SetInLinkUpdate( false );
 
diff --git a/sc/source/ui/docshell/docsh4.cxx b/sc/source/ui/docshell/docsh4.cxx
index 9adcb466c6bc..271309f7064c 100644
--- a/sc/source/ui/docshell/docsh4.cxx
+++ b/sc/source/ui/docshell/docsh4.cxx
@@ -817,8 +817,7 @@ void ScDocShell::Execute( SfxRequest& rReq )
                 SfxErrorContext aEc( ERRCTX_SFX_OPENDOC, pMed->GetName() );
 
                 // pOtherDocSh->DoClose() will be called explicitly later, but 
it is still more safe to use SfxObjectShellLock here
-                ScDocShell* pOtherDocSh = new ScDocShell;
-                SfxObjectShellLock aDocShTablesRef = pOtherDocSh;
+                rtl::Reference<ScDocShell> pOtherDocSh = new ScDocShell;
                 pOtherDocSh->DoLoad( pMed );
                 ErrCode nErr = pOtherDocSh->GetErrorCode();
                 if (nErr)
diff --git a/sc/source/ui/docshell/externalrefmgr.cxx 
b/sc/source/ui/docshell/externalrefmgr.cxx
index 933cc1ad5b11..095786e6d9c6 100644
--- a/sc/source/ui/docshell/externalrefmgr.cxx
+++ b/sc/source/ui/docshell/externalrefmgr.cxx
@@ -2592,8 +2592,7 @@ SfxObjectShellRef 
ScExternalRefManager::loadSrcDocument(sal_uInt16 nFileId, OUSt
     // To load encrypted documents with password, user interaction needs to be 
enabled.
     pMedium->UseInteractionHandler(mbUserInteractionEnabled);
 
-    ScDocShell* pNewShell = new ScDocShell(SfxModelFlags::EXTERNAL_LINK);
-    SfxObjectShellRef aRef = pNewShell;
+    rtl::Reference<ScDocShell> pNewShell = new 
ScDocShell(SfxModelFlags::EXTERNAL_LINK);
 
     // increment the recursive link count of the source document.
     ScExtDocOptions* pExtOpt = mrDoc.GetExtDocOptions();
@@ -2614,9 +2613,9 @@ SfxObjectShellRef 
ScExternalRefManager::loadSrcDocument(sal_uInt16 nFileId, OUSt
 
     if (!pNewShell->DoLoad(pMedium.release()))
     {
-        aRef->DoClose();
-        aRef.clear();
-        return aRef;
+        pNewShell->DoClose();
+        pNewShell.clear();
+        return pNewShell;
     }
 
     // with UseInteractionHandler, options may be set by dialog during DoLoad
@@ -2625,7 +2624,7 @@ SfxObjectShellRef 
ScExternalRefManager::loadSrcDocument(sal_uInt16 nFileId, OUSt
         aOptions = aNew;
     setFilterData(nFileId, rFilter, aOptions);    // update the filter data, 
including the new options
 
-    return aRef;
+    return pNewShell;
 }
 
 ScDocument& ScExternalRefManager::cacheNewDocShell( sal_uInt16 nFileId, 
SrcShell& rSrcShell )
@@ -3302,8 +3301,8 @@ void ScExternalRefManager::Notify( SfxBroadcaster&, const 
SfxHint& rHint )
         case SfxEventHintId::SaveDocDone:
         case SfxEventHintId::SaveAsDocDone:
             {
-                SfxObjectShell* pObjShell = static_cast<const SfxEventHint&>( 
rHint ).GetObjShell();
-                transformUnsavedRefToSavedRef(pObjShell);
+                rtl::Reference<SfxObjectShell> pObjShell = static_cast<const 
SfxEventHint&>( rHint ).GetObjShell();
+                transformUnsavedRefToSavedRef(pObjShell.get());
             }
             break;
         default:
-e 
... etc. - the rest is truncated

Reply via email to