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 ++++++++++-----------
 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                           |    4 +-
 include/sot/object.hxx                           |    6 ++-
 include/sot/storage.hxx                          |    6 +--
 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                               |    5 +--
 sc/source/core/data/global.cxx                   |    2 -
 sc/source/filter/excel/excel.cxx                 |   30 +++++++++----------
 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/oleprops.cxx                     |    4 +-
 sfx2/source/inc/objshimp.hxx                     |    1 
 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/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/inc/galleryfilestorage.hxx                   |    4 +-
 svx/source/gallery2/galleryfilestorage.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/inc/WPXSvInputStream.hxx           |    2 -
 writerperfect/source/common/WPXSvInputStream.cxx |   28 ++++++++---------
 142 files changed, 491 insertions(+), 508 deletions(-)

New commits:
commit 1ac5353bbb25bd9ff0ab0e157b3dbd0da325480a
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 04:43:28 2024 +0100

    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 180bf3d17fa7..64938be7754b 100644
--- a/basctl/source/basicide/unomodel.cxx
+++ b/basctl/source/basicide/unomodel.cxx
@@ -173,7 +173,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 bf0c6ee53a46..525482d8dbae 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 03998f30ca9b..72b6f0de5ddf 100644
--- a/basic/source/basmgr/basmgr.cxx
+++ b/basic/source/basmgr/basmgr.cxx
@@ -632,7 +632,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!" );
@@ -724,7 +724,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!" );
@@ -777,7 +777,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;
@@ -838,7 +838,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 )
     {
@@ -861,7 +861,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() )
     {
@@ -871,7 +871,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() )
         {
             ErrCodeMsg aErrInf( ERRCODE_BASMGR_LIBLOAD , 
pLibInfo->GetLibName(), DialogMask::ButtonsOk );
@@ -1093,7 +1093,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())
@@ -1112,7 +1112,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() )
@@ -1286,7 +1286,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 f534856dc11b..2a5a1249a017 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 b723c7958843..401dc9d8ff17 100644
--- a/editeng/source/misc/svxacorr.cxx
+++ b/editeng/source/misc/svxacorr.cxx
@@ -2280,7 +2280,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();
@@ -2290,7 +2290,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())
             {
@@ -2351,7 +2351,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() )
@@ -2364,7 +2364,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() )
         {
@@ -2483,7 +2483,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 );
 
@@ -2503,7 +2503,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 );
 
@@ -2521,7 +2521,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 );
     }
@@ -2534,7 +2534,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 );
 
@@ -2550,7 +2550,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 );
     }
@@ -2564,7 +2564,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 );
 
@@ -2594,7 +2594,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 ) )
         {
@@ -2656,8 +2656,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() )
         {
@@ -2705,7 +2705,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() )
         {
@@ -2756,7 +2756,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();
 
@@ -2825,7 +2825,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();
 
@@ -2883,7 +2883,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 ccd0f53f4313..702e14d64d89 100644
--- a/filter/source/msfilter/msdffimp.cxx
+++ b/filter/source/msfilter/msdffimp.cxx
@@ -6770,7 +6770,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;
 }
@@ -6789,7 +6789,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 ))
@@ -6811,7 +6811,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 );
 
@@ -6951,10 +6951,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;
@@ -7000,7 +7000,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;
@@ -7118,7 +7118,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);
 }
@@ -7191,7 +7191,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();
@@ -7305,7 +7305,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,
@@ -7328,12 +7328,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 )
@@ -7354,7 +7354,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() )
                         {
@@ -7396,11 +7396,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() )
@@ -7427,7 +7427,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 679082080c3c..5e0729be55bd 100644
--- a/filter/source/msfilter/msoleexp.cxx
+++ b/filter/source/msfilter/msoleexp.cxx
@@ -168,7 +168,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();
         }
@@ -189,7 +189,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;
@@ -244,7 +244,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() )
                 {
@@ -291,7 +291,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 108b52daf0c0..a46bf99b730b 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 0a93095d4ddf..42b31c99af02 100644
--- a/filter/source/msfilter/svdfppt.cxx
+++ b/filter/source/msfilter/svdfppt.cxx
@@ -1671,7 +1671,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;
@@ -1850,14 +1850,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 ];
@@ -1908,7 +1908,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());
@@ -2029,16 +2029,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;
@@ -2057,13 +2057,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 4bc359ddf8f8..d85a3d242783 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>
@@ -200,10 +201,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 c48280c7a4a4..3b51d5b71424 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 55a648a70354..5c77580e4507 100644
--- a/include/filter/msfilter/svdfppt.hxx
+++ b/include/filter/msfilter/svdfppt.hxx
@@ -655,7 +655,7 @@ public:
                                 const sal_uInt32* pTableArry,
                                 SvxMSDffSolverContainer* pSolverContainer
                             );
-    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 1024dd5b2423..0e4fd8977a7d 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;
 
@@ -210,8 +210,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 3175cb283642..c961394db08c 100644
--- a/include/sfx2/objsh.hxx
+++ b/include/sfx2/objsh.hxx
@@ -176,7 +176,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;
@@ -837,7 +837,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/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 c59895246a09..44657ebc3408 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 1539858434bc..535c919f5fe2 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 <com/sun/star/sheet/CellFlags.hpp>
@@ -591,7 +591,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 63eba3b2efa9..56bd64881835 100644
--- a/sc/inc/tablink.hxx
+++ b/sc/inc/tablink.hxx
@@ -73,8 +73,7 @@ class SfxFilter;
 class SC_DLLPUBLIC ScDocumentLoader
 {
 private:
-    ScDocShell*         pDocShell;
-    SfxObjectShellRef   aRef;
+    rtl::Reference<ScDocShell> pDocShell;
     SfxMedium*          pMedium;
 
 public:
@@ -84,7 +83,7 @@ public:
                      = css::uno::Reference<css::io::XInputStream>());
     ~ScDocumentLoader();
     ScDocument*         GetDocument();
-    ScDocShell*         GetDocShell()       { return pDocShell; }
+    ScDocShell*         GetDocShell()       { return pDocShell.get(); }
     bool                IsError() const;
 
     void                ReleaseDocRef();    // without calling DoClose
diff --git a/sc/source/core/data/global.cxx b/sc/source/core/data/global.cxx
index 8fcdaf5782c2..9443d6e0324a 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 862e85d08545..82dca81539ae 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;
@@ -168,8 +168,8 @@ ErrCode ScFormatFilterPluginImpl::ScImportExcel( SfxMedium& 
rMedium, ScDocument*
     bool bUnableToDecryptContent = false;
 
     // 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 ) )
     {
@@ -182,7 +182,7 @@ 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())
         {
             auto pDecryptedStorage = lcl_DRMDecrypt(rMedium, xRootStrg, 
aNewStorageStrm);
@@ -195,11 +195,11 @@ ErrCode ScFormatFilterPluginImpl::ScImportExcel( 
SfxMedium& rMedium, ScDocument*
         }
 
         // 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
@@ -297,7 +297,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
@@ -316,7 +316,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?
@@ -354,12 +354,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);
+        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
@@ -384,7 +384,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 2df1ec268324..12ed36cebd48 100644
--- a/sc/source/filter/excel/excimp8.cxx
+++ b/sc/source/filter/excel/excimp8.cxx
@@ -322,7 +322,7 @@ void ImportExcel8::Feat()
 void ImportExcel8::ReadBasic()
 {
     ScDocShell* pShell = GetDocShell();
-    tools::SvRef<SotStorage> xRootStrg = GetRootStorage();
+    rtl::Reference<SotStorage> xRootStrg = GetRootStorage();
     if( !pShell || !xRootStrg.is() )
         return;
 
@@ -335,7 +335,7 @@ void ImportExcel8::ReadBasic()
               officecfg::Office::Calc::Filter::Import::VBA::Executable::get() )
         {
             // 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;
@@ -412,7 +412,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( 
static_cast<cppu::OWeakObject*>(pShell->GetModel()), uno::UNO_QUERY_THROW );
diff --git a/sc/source/filter/excel/expop2.cxx 
b/sc/source/filter/excel/expop2.cxx
index 936a09b2f3ed..1c9ea2ae0b72 100644
--- a/sc/source/filter/excel/expop2.cxx
+++ b/sc/source/filter/excel/expop2.cxx
@@ -71,7 +71,7 @@ ErrCode ExportBiff5::Write()
     ScDocShell* 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;
@@ -91,7 +91,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 c1959767d97d..3d3b4c0b922b 100644
--- a/sc/source/filter/excel/xeroot.cxx
+++ b/sc/source/filter/excel/xeroot.cxx
@@ -55,7 +55,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 a70e4e08bd71..32cecf352de3 100644
--- a/sc/source/filter/excel/xestream.cxx
+++ b/sc/source/filter/excel/xestream.cxx
@@ -1035,7 +1035,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();
 
     auto& rGraphicExportCache = drawingml::GraphicExportCache::get();
@@ -1113,7 +1113,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 c6d0a972b78b..5072d23a76e5 100644
--- a/sc/source/filter/excel/xiescher.cxx
+++ b/sc/source/filter/excel/xiescher.cxx
@@ -3534,7 +3534,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 12b86f679604..7e67ad4e8ae2 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 97e9cbffb385..836c08b3f5e4 100644
--- a/sc/source/filter/excel/xlroot.cxx
+++ b/sc/source/filter/excel/xlroot.cxx
@@ -85,7 +85,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 ),
@@ -258,30 +258,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 860ecf71c454..bf2b784a994b 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 5e5b8dd6c5b9..7e8a07bce8c8 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 3a0a606113a7..728c71b7223f 100644
--- a/sc/source/filter/inc/xcl97esc.hxx
+++ b/sc/source/filter/inc/xcl97esc.hxx
@@ -102,7 +102,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 cf09c161fe11..f43e91eba729 100644
--- a/sc/source/filter/inc/xiescher.hxx
+++ b/sc/source/filter/inc/xiescher.hxx
@@ -1030,7 +1030,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 8d9c54928218..aa86378416b4 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>
@@ -76,7 +76,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).
@@ -110,7 +110,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();
 };
@@ -184,18 +184,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 9c22af8a6420..936e00f6a221 100644
--- a/sc/source/filter/xcl97/XclExpChangeTrack.cxx
+++ b/sc/source/filter/xcl97/XclExpChangeTrack.cxx
@@ -1648,7 +1648,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() )
     {
@@ -1671,7 +1671,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 83e3c74cf216..8e68a312d17b 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 8abe7dc68372..c29eba781b3f 100644
--- a/sc/source/filter/xcl97/xcl97rec.cxx
+++ b/sc/source/filter/xcl97/xcl97rec.cxx
@@ -1015,7 +1015,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 3257a6cae4d3..76c221dd18d3 100644
--- a/sc/source/ui/app/drwtrans.cxx
+++ b/sc/source/ui/app/drwtrans.cxx
@@ -677,13 +677,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());
 
     auto pPool = rDestDoc.GetStyleSheetPool();
     pPool->CopyStyleFrom(m_pModel->GetStyleSheetPool(), 
ScResId(STR_STYLENAME_STANDARD), SfxStyleFamily::Frame);
@@ -717,18 +716,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);
 }
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
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 5f0599c888b3..d604963fd451 100644
--- a/sc/source/ui/app/transobj.cxx
+++ b/sc/source/ui/app/transobj.cxx
@@ -668,12 +668,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 );
 
@@ -683,7 +682,7 @@ void ScTransferObj::InitDocShell(bool bLimitToPageSize)
     m_pDoc->GetName( m_aBlock.aStart.Tab(), aTabName );
     rDestDoc.RenameTab( 0, aTabName );
 
-    pDocSh->MakeDrawLayer();
+    m_aDocShellRef->MakeDrawLayer();
 
     rDestDoc.CopyStdStylesFrom(*m_pDoc);
 
@@ -758,7 +757,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 );
@@ -799,14 +798,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 1ebd60763654..e1226c01579d 100644
--- a/sc/source/ui/docshell/arealink.cxx
+++ b/sc/source/ui/docshell/arealink.cxx
@@ -254,8 +254,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();
@@ -480,7 +479,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 a9419dbb5b57..9bf7c1beb7b2 100644
--- a/sc/source/ui/docshell/docsh4.cxx
+++ b/sc/source/ui/docshell/docsh4.cxx
@@ -713,8 +713,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 );
                 ErrCodeMsg nErr = pOtherDocSh->GetErrorCode();
                 if (nErr)
diff --git a/sc/source/ui/docshell/externalrefmgr.cxx 
b/sc/source/ui/docshell/externalrefmgr.cxx
index f5b3dbd26fa1..3e66cfe68016 100644
--- a/sc/source/ui/docshell/externalrefmgr.cxx
+++ b/sc/source/ui/docshell/externalrefmgr.cxx
@@ -2607,8 +2607,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();
@@ -2629,9 +2628,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
@@ -2640,7 +2639,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 )
@@ -3315,8 +3314,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:
diff --git a/sc/source/ui/docshell/tablink.cxx 
b/sc/source/ui/docshell/tablink.cxx
index 5a20b1c94075..04f21b9c05b6 100644
--- a/sc/source/ui/docshell/tablink.cxx
+++ b/sc/source/ui/docshell/tablink.cxx
@@ -177,8 +177,7 @@ bool ScTableLink::Refresh(const OUString& rNewFile, const 
OUString& rNewFilter,
         pMed->UseInteractionHandler(true);    // enable the filter options 
dialog
 
     // aRef->DoClose() will be called 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);
 
     // options might have been set
@@ -358,7 +357,7 @@ bool ScTableLink::Refresh(const OUString& rNewFile, const 
OUString& rNewFilter,
 
     //  clean up
 
-    aRef->DoClose();
+    pSrcShell->DoClose();
 
     //  Undo
 
@@ -491,8 +490,7 @@ ScDocumentLoader::ScDocumentLoader(const OUString& 
rFileName,
                                    OUString& rFilterName, OUString& rOptions,
                                    sal_uInt32 nRekCnt, weld::Window* 
pInteractionParent,
                                    css::uno::Reference<css::io::XInputStream> 
xInputStream)
-    : pDocShell(nullptr)
-    , pMedium(nullptr)
+    : pMedium(nullptr)
 {
     if ( rFilterName.isEmpty() )
         GetFilterName(rFileName, rFilterName, rOptions, true, 
pInteractionParent != nullptr);
@@ -506,7 +504,6 @@ ScDocumentLoader::ScDocumentLoader(const OUString& 
rFileName,
         return ;
 
     pDocShell = new ScDocShell( SfxModelFlags::EMBEDDED_OBJECT | 
SfxModelFlags::DISABLE_EMBEDDED_SCRIPTS );
-    aRef = pDocShell;
 
     ScDocument& rDoc = pDocShell->GetDocument();
     ScExtDocOptions*    pExtDocOpt = rDoc.GetExtDocOptions();
@@ -526,22 +523,21 @@ ScDocumentLoader::ScDocumentLoader(const OUString& 
rFileName,
 
 ScDocumentLoader::~ScDocumentLoader()
 {
-    if ( aRef.is() )
-        aRef->DoClose();
+    if (pDocShell)
+        pDocShell->DoClose();
     else
         delete pMedium;
 }
 
 void ScDocumentLoader::ReleaseDocRef()
 {
-    if ( aRef.is() )
+    if (pDocShell)
     {
         //  release reference without calling DoClose - caller must
         //  have another reference to the doc and call DoClose later
 
-        pDocShell = nullptr;
         pMedium = nullptr;
-        aRef.clear();
+        pDocShell.clear();
     }
 }
 
diff --git a/sc/source/ui/inc/docsh.hxx b/sc/source/ui/inc/docsh.hxx
index e60b9f2b94e8..2f78bd16ef18 100644
--- a/sc/source/ui/inc/docsh.hxx
+++ b/sc/source/ui/inc/docsh.hxx
@@ -445,7 +445,7 @@ private:
 
 void UpdateAcceptChangesDialog();
 
-typedef tools::SvRef<ScDocShell> ScDocShellRef;
+typedef rtl::Reference<ScDocShell> ScDocShellRef;
 
 /** Create before modifications of the document and destroy thereafter.
     Call SetDocumentModified() at an instance of this class instead of at
diff --git a/sc/source/ui/inc/drwtrans.hxx b/sc/source/ui/inc/drwtrans.hxx
index 71213739dd2b..2e485db9a2af 100644
--- a/sc/source/ui/inc/drwtrans.hxx
+++ b/sc/source/ui/inc/drwtrans.hxx
@@ -40,7 +40,7 @@ private:
     std::unique_ptr<SdrModel>       m_pModel;
     TransferableDataHelper          m_aOleData;
     TransferableObjectDescriptor    m_aObjDesc;
-    SfxObjectShellRef               m_aDocShellRef;
+    rtl::Reference<ScDocShell>      m_aDocShellRef;
     SfxObjectShellRef               m_aDrawPersistRef;
 
                                     // extracted from model in ctor:
diff --git a/sc/source/ui/inc/instbdlg.hxx b/sc/source/ui/inc/instbdlg.hxx
index 9698229b69cc..81e48e3f0990 100644
--- a/sc/source/ui/inc/instbdlg.hxx
+++ b/sc/source/ui/inc/instbdlg.hxx
@@ -44,7 +44,7 @@ public:
 
     const OUString* GetFirstTable( sal_uInt16* pN );
     const OUString* GetNextTable( sal_uInt16* pN );
-    ScDocShell*     GetDocShellTables() { return pDocShTables; }
+    ScDocShell*     GetDocShellTables() { return pDocShTables.get(); }
     bool        IsTableBefore() const { return m_xBtnBefore->get_active(); }
     SCTAB           GetTableCount() const { return nTableCount;}
 
@@ -52,9 +52,8 @@ private:
     Timer                   aBrowseTimer;
     ScViewData&             rViewData;
     ScDocument&             rDoc;
-    ScDocShell*             pDocShTables;
+    rtl::Reference<ScDocShell> pDocShTables;
     std::unique_ptr<sfx2::DocumentInserter> pDocInserter;
-    SfxObjectShellRef       aDocShTablesRef;
 
     bool                bMustClose;
     sal_uInt16          nSelTabIndex;   // for GetFirstTable() / GetNextTable()
diff --git a/sc/source/ui/inc/linkarea.hxx b/sc/source/ui/inc/linkarea.hxx
index a209d3b8a39f..bde58e601b3b 100644
--- a/sc/source/ui/inc/linkarea.hxx
+++ b/sc/source/ui/inc/linkarea.hxx
@@ -31,9 +31,8 @@ class SvtURLBox;
 class ScLinkedAreaDlg : public weld::GenericDialogController
 {
 private:
-    ScDocShell*             m_pSourceShell;
+    rtl::Reference<ScDocShell> m_pSourceShell;
     std::unique_ptr<sfx2::DocumentInserter> m_xDocInserter;
-    SfxObjectShellRef   aSourceRef;
 
     std::unique_ptr<SvtURLBox> m_xCbUrl;
     std::unique_ptr<weld::Button> m_xBtnBrowse;
diff --git a/sc/source/ui/inc/transobj.hxx b/sc/source/ui/inc/transobj.hxx
index 1c81610e6fc9..9b160e127cc9 100644
--- a/sc/source/ui/inc/transobj.hxx
+++ b/sc/source/ui/inc/transobj.hxx
@@ -44,7 +44,7 @@ private:
     ScRange                         m_aBlock;
     SCROW                           m_nNonFiltered;       // non-filtered rows
     TransferableObjectDescriptor    m_aObjDesc;
-    SfxObjectShellRef               m_aDocShellRef;
+    rtl::Reference<ScDocShell>      m_aDocShellRef;
     SfxObjectShellRef               m_aDrawPersistRef;
     rtl::Reference<ScCellRangesBase> m_xDragSourceRanges;
     SCCOL                           m_nDragHandleX;
diff --git a/sc/source/ui/miscdlgs/instbdlg.cxx 
b/sc/source/ui/miscdlgs/instbdlg.cxx
index e34b22a6f336..ae81f4e3c17b 100644
--- a/sc/source/ui/miscdlgs/instbdlg.cxx
+++ b/sc/source/ui/miscdlgs/instbdlg.cxx
@@ -38,7 +38,6 @@ ScInsertTableDlg::ScInsertTableDlg(weld::Window* pParent, 
ScViewData& rData, SCT
     , aBrowseTimer("ScInsertTableDlg aBrowseTimer")
     , rViewData(rData)
     , rDoc(rData.GetDocument())
-    , pDocShTables(nullptr)
     , bMustClose(false)
     , nSelTabIndex(0)
     , nTableCount(nTabCount)
-e 
... etc. - the rest is truncated

Reply via email to