basctl/source/dlged/dlgedmod.cxx                                  |    5 
 basctl/source/dlged/dlgedpage.cxx                                 |    6 
 basctl/source/inc/dlgedmod.hxx                                    |    2 
 basctl/source/inc/dlgedpage.hxx                                   |    2 
 chart2/source/controller/drawinglayer/ViewElementListProvider.cxx |    6 
 cui/source/tabpages/tpline.cxx                                    |   12 -
 filter/source/msfilter/svdfppt.cxx                                |    4 
 include/filter/msfilter/svdfppt.hxx                               |    2 
 include/svx/connctrl.hxx                                          |    2 
 include/svx/dlgctl3d.hxx                                          |    2 
 include/svx/fmmodel.hxx                                           |    6 
 include/svx/fmpage.hxx                                            |    2 
 include/svx/svdmodel.hxx                                          |   10 
 include/svx/svdpage.hxx                                           |    6 
 include/svx/svdundo.hxx                                           |    7 
 reportdesign/inc/RptModel.hxx                                     |    4 
 reportdesign/inc/RptPage.hxx                                      |    2 
 reportdesign/source/core/sdr/RptModel.cxx                         |   12 -
 reportdesign/source/core/sdr/RptPage.cxx                          |    4 
 sc/inc/drawpage.hxx                                               |    2 
 sc/inc/drwlayer.hxx                                               |    2 
 sc/source/core/data/drawpage.cxx                                  |    4 
 sc/source/core/data/drwlayer.cxx                                  |    6 
 sc/source/ui/view/notemark.cxx                                    |    4 
 sd/inc/drawdoc.hxx                                                |    8 
 sd/inc/sdpage.hxx                                                 |    2 
 sd/inc/undo/undoobjects.hxx                                       |    9 
 sd/qa/unit/uimpress.cxx                                           |    9 
 sd/source/core/drawdoc.cxx                                        |    4 
 sd/source/core/drawdoc2.cxx                                       |   60 ++---
 sd/source/core/drawdoc3.cxx                                       |   40 +--
 sd/source/core/sdpage2.cxx                                        |    4 
 sd/source/core/stlfamily.cxx                                      |   16 -
 sd/source/core/undo/undoobjects.cxx                               |   34 +--
 sd/source/filter/ppt/pptin.cxx                                    |   51 ++---
 sd/source/ui/dlg/animobjs.cxx                                     |    4 
 sd/source/ui/func/fuexpand.cxx                                    |    8 
 sd/source/ui/func/fusumry.cxx                                     |    8 
 sd/source/ui/inc/DrawController.hxx                               |    4 
 sd/source/ui/sidebar/DocumentHelper.cxx                           |   20 -
 sd/source/ui/unoidl/DrawController.cxx                            |   11 -
 sd/source/ui/unoidl/unomodel.cxx                                  |   34 +--
 sd/source/ui/view/outlview.cxx                                    |   10 
 sd/source/ui/view/sdview3.cxx                                     |    4 
 svx/inc/sdr/contact/objectcontactofobjlistpainter.hxx             |    8 
 svx/source/dialog/connctrl.cxx                                    |   28 +-
 svx/source/dialog/dlgctl3d.cxx                                    |   15 -
 svx/source/dialog/graphctl.cxx                                    |    4 
 svx/source/form/fmmodel.cxx                                       |   10 
 svx/source/form/fmpage.cxx                                        |    6 
 svx/source/gallery2/galobj.cxx                                    |    2 
 svx/source/sdr/contact/objectcontactofobjlistpainter.cxx          |    2 
 svx/source/svdraw/svdmodel.cxx                                    |   62 ++----
 svx/source/svdraw/svdpage.cxx                                     |    5 
 svx/source/svdraw/svdundo.cxx                                     |  102 
++++------
 svx/source/svdraw/svdxcgv.cxx                                     |    4 
 svx/source/unodraw/unomod.cxx                                     |    4 
 svx/source/unogallery/unogaltheme.cxx                             |    4 
 sw/inc/dpage.hxx                                                  |    2 
 sw/inc/drawdoc.hxx                                                |    2 
 sw/source/core/doc/DocumentDrawModelManager.cxx                   |    6 
 sw/source/core/doc/doclay.cxx                                     |    5 
 sw/source/core/draw/dpage.cxx                                     |    4 
 sw/source/core/draw/drawdoc.cxx                                   |    4 
 sw/source/core/frmedt/feshview.cxx                                |    5 
 65 files changed, 354 insertions(+), 384 deletions(-)

New commits:
commit 1d2773807f6d2deb1181ef4b14fbdd49ca1d5c14
Author:     Noel <noel.gran...@collabora.co.uk>
AuthorDate: Fri Mar 5 10:47:16 2021 +0200
Commit:     Noel Grandin <noel.gran...@collabora.co.uk>
CommitDate: Sun Mar 7 13:24:51 2021 +0100

    ref-count SdrPage
    
    which simplifies ownership handling, particularly with regard to
    undo/redo
    
    Change-Id: Ie8f300ebfdae6db3c06f78a87e35a07497d6b825
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/111991
    Tested-by: Jenkins
    Reviewed-by: Noel Grandin <noel.gran...@collabora.co.uk>

diff --git a/basctl/source/dlged/dlgedmod.cxx b/basctl/source/dlged/dlgedmod.cxx
index 291b5b9136f5..3bc5453ad66f 100644
--- a/basctl/source/dlged/dlgedmod.cxx
+++ b/basctl/source/dlged/dlgedmod.cxx
@@ -29,7 +29,10 @@ DlgEdModel::DlgEdModel()
 
 DlgEdModel::~DlgEdModel() {}
 
-SdrPage* DlgEdModel::AllocPage(bool bMasterPage) { return new DlgEdPage(*this, 
bMasterPage); }
+rtl::Reference<SdrPage> DlgEdModel::AllocPage(bool bMasterPage)
+{
+    return new DlgEdPage(*this, bMasterPage);
+}
 
 } // namespace basctl
 
diff --git a/basctl/source/dlged/dlgedpage.cxx 
b/basctl/source/dlged/dlgedpage.cxx
index d422f757868c..760f88527609 100644
--- a/basctl/source/dlged/dlgedpage.cxx
+++ b/basctl/source/dlged/dlgedpage.cxx
@@ -38,13 +38,13 @@ DlgEdPage::~DlgEdPage()
     ClearSdrObjList();
 }
 
-SdrPage* DlgEdPage::CloneSdrPage(SdrModel& rTargetModel) const
+rtl::Reference<SdrPage> DlgEdPage::CloneSdrPage(SdrModel& rTargetModel) const
 {
     DlgEdModel& rDlgEdModel(static_cast< DlgEdModel& >(rTargetModel));
-    DlgEdPage* pClonedDlgEdPage(
+    rtl::Reference<DlgEdPage> pClonedDlgEdPage =
         new DlgEdPage(
             rDlgEdModel,
-            IsMasterPage()));
+            IsMasterPage());
     pClonedDlgEdPage->SdrPage::lateInit(*this);
     return pClonedDlgEdPage;
 }
diff --git a/basctl/source/inc/dlgedmod.hxx b/basctl/source/inc/dlgedmod.hxx
index 2960135e0d3d..24a97905b1d3 100644
--- a/basctl/source/inc/dlgedmod.hxx
+++ b/basctl/source/inc/dlgedmod.hxx
@@ -37,7 +37,7 @@ public:
     DlgEdModel();
     virtual ~DlgEdModel() override;
 
-    virtual SdrPage* AllocPage(bool bMasterPage) override;
+    virtual rtl::Reference<SdrPage> AllocPage(bool bMasterPage) override;
 };
 
 } // namespace basctl
diff --git a/basctl/source/inc/dlgedpage.hxx b/basctl/source/inc/dlgedpage.hxx
index 7b8d48faa331..91efc12edd70 100644
--- a/basctl/source/inc/dlgedpage.hxx
+++ b/basctl/source/inc/dlgedpage.hxx
@@ -39,7 +39,7 @@ public:
     explicit DlgEdPage(DlgEdModel& rModel, bool bMasterPage = false);
     virtual ~DlgEdPage() override;
 
-    virtual SdrPage* CloneSdrPage(SdrModel& rTargetModel) const override;
+    virtual rtl::Reference<SdrPage> CloneSdrPage(SdrModel& rTargetModel) const 
override;
 
     void SetDlgEdForm(DlgEdForm* pForm) { pDlgEdForm = pForm; }
     DlgEdForm* GetDlgEdForm() const { return pDlgEdForm; }
diff --git a/chart2/source/controller/drawinglayer/ViewElementListProvider.cxx 
b/chart2/source/controller/drawinglayer/ViewElementListProvider.cxx
index 9259ce921819..f4f5ecfbd9d6 100644
--- a/chart2/source/controller/drawinglayer/ViewElementListProvider.cxx
+++ b/chart2/source/controller/drawinglayer/ViewElementListProvider.cxx
@@ -152,12 +152,12 @@ Graphic ViewElementListProvider::GetSymbolGraphic( 
sal_Int32 nStandardSymbol, co
         new SdrModel());
 
     pModel->GetItemPool().FreezeIdRanges();
-    SdrPage* pPage = new SdrPage( *pModel, false );
+    rtl::Reference<SdrPage> pPage = new SdrPage( *pModel, false );
     pPage->SetSize(Size(1000,1000));
-    pModel->InsertPage( pPage, 0 );
+    pModel->InsertPage( pPage.get(), 0 );
     std::unique_ptr<SdrView> pView(new SdrView(*pModel, pVDev));
     pView->hideMarkHandles();
-    SdrPageView* pPageView = pView->ShowSdrPage(pPage);
+    SdrPageView* pPageView = pView->ShowSdrPage(pPage.get());
 
     // directly clone to target SdrModel
     pObj = pObj->CloneSdrObject(*pModel);
diff --git a/cui/source/tabpages/tpline.cxx b/cui/source/tabpages/tpline.cxx
index aba1fafc6735..2a9fc74cc429 100644
--- a/cui/source/tabpages/tpline.cxx
+++ b/cui/source/tabpages/tpline.cxx
@@ -820,13 +820,13 @@ void SvxLineTabPage::Reset( const SfxItemSet* rAttrs )
         std::unique_ptr<SdrModel> pModel(
             new SdrModel(nullptr, nullptr, true));
         pModel->GetItemPool().FreezeIdRanges();
-        SdrPage* pPage = new SdrPage( *pModel, false );
+        rtl::Reference<SdrPage> pPage = new SdrPage( *pModel, false );
         pPage->SetSize(Size(1000,1000));
-        pModel->InsertPage( pPage, 0 );
+        pModel->InsertPage( pPage.get(), 0 );
         {
         std::unique_ptr<SdrView> pView(new SdrView( *pModel, pVDev ));
         pView->hideMarkHandles();
-        pView->ShowSdrPage(pPage);
+        pView->ShowSdrPage(pPage.get());
         size_t nSymTmp = static_cast<size_t>(nSymType);
         if(m_pSymbolList)
         {
@@ -1437,14 +1437,14 @@ IMPL_LINK_NOARG(SvxLineTabPage, MenuCreateHdl_Impl, 
weld::ToggleButton&, void)
         new SdrModel(nullptr, nullptr, true));
     pModel->GetItemPool().FreezeIdRanges();
     // Page
-    SdrPage* pPage = new SdrPage( *pModel, false );
+    rtl::Reference<SdrPage> pPage = new SdrPage( *pModel, false );
     pPage->SetSize(Size(1000,1000));
-    pModel->InsertPage( pPage, 0 );
+    pModel->InsertPage( pPage.get(), 0 );
     {
         // 3D View
         std::unique_ptr<SdrView> pView(new SdrView( *pModel, pVDev ));
         pView->hideMarkHandles();
-        pView->ShowSdrPage(pPage);
+        pView->ShowSdrPage(pPage.get());
 
         // Generate invisible square to give all symbols a
         // bitmap size, which is independent from specific glyph
diff --git a/filter/source/msfilter/svdfppt.cxx 
b/filter/source/msfilter/svdfppt.cxx
index 497a47c1d695..462cefb3738b 100644
--- a/filter/source/msfilter/svdfppt.cxx
+++ b/filter/source/msfilter/svdfppt.cxx
@@ -2678,9 +2678,9 @@ bool SdrPowerPointImport::SeekToShape( SvStream& rSt, 
SvxMSDffClientData* pClien
     return bRet;
 }
 
-SdrPage* SdrPowerPointImport::MakeBlankPage( bool bMaster ) const
+rtl::Reference<SdrPage> SdrPowerPointImport::MakeBlankPage( bool bMaster ) 
const
 {
-    SdrPage* pRet = pSdrModel->AllocPage( bMaster );
+    rtl::Reference<SdrPage> pRet = pSdrModel->AllocPage( bMaster );
     pRet->SetSize( GetPageSize() );
 
     return pRet;
diff --git a/include/filter/msfilter/svdfppt.hxx 
b/include/filter/msfilter/svdfppt.hxx
index 02c60d37004e..628288088ecc 100644
--- a/include/filter/msfilter/svdfppt.hxx
+++ b/include/filter/msfilter/svdfppt.hxx
@@ -601,7 +601,7 @@ protected:
                                 const int _nCalledByGroup
                             ) const override;
     std::unique_ptr<SvMemoryStream> ImportExOleObjStg( sal_uInt32 nPersistPtr, 
sal_uInt32& nOleId ) const;
-    SdrPage*                MakeBlankPage(bool bMaster) const;
+    rtl::Reference<SdrPage> MakeBlankPage(bool bMaster) const;
     bool                    ReadFontCollection();
     PptSlidePersistList*    GetPageList(PptPageKind ePageKind) const;
     sal_uInt32              GetCurrentPageId();
diff --git a/include/svx/connctrl.hxx b/include/svx/connctrl.hxx
index 024fba8edd45..d15140529f83 100644
--- a/include/svx/connctrl.hxx
+++ b/include/svx/connctrl.hxx
@@ -42,7 +42,7 @@ class SAL_WARN_UNUSED SVX_DLLPUBLIC SvxXConnectionPreview 
final : public weld::C
 private:
     MapMode aNewMapMode;
     SdrEdgeObj*         pEdgeObj;
-    std::unique_ptr<SdrPage> pSdrPage;
+    rtl::Reference<SdrPage> mxSdrPage;
     const SdrView*      pView;
 
     SVX_DLLPRIVATE void AdaptSize();
diff --git a/include/svx/dlgctl3d.hxx b/include/svx/dlgctl3d.hxx
index d43ab5f4c8d8..39790142b3e2 100644
--- a/include/svx/dlgctl3d.hxx
+++ b/include/svx/dlgctl3d.hxx
@@ -39,7 +39,7 @@ class SAL_WARN_UNUSED SVX_DLLPUBLIC Svx3DPreviewControl : 
public weld::CustomWid
 {
 protected:
     std::unique_ptr<FmFormModel> mpModel;
-    FmFormPage*             mpFmPage;
+    rtl::Reference<FmFormPage> mxFmPage;
     std::unique_ptr<E3dView> mp3DView;
     E3dScene*               mpScene;
     E3dObject*              mp3DObj;
diff --git a/include/svx/fmmodel.hxx b/include/svx/fmmodel.hxx
index 31ef08679cf6..15577d1dcc0d 100644
--- a/include/svx/fmmodel.hxx
+++ b/include/svx/fmmodel.hxx
@@ -49,11 +49,11 @@ public:
 
     virtual ~FmFormModel() override;
 
-    virtual SdrPage* AllocPage(bool bMasterPage) override;
+    virtual rtl::Reference<SdrPage> AllocPage(bool bMasterPage) override;
     virtual void     InsertPage(SdrPage* pPage, sal_uInt16 nPos=0xFFFF) 
override;
-    virtual SdrPage* RemovePage(sal_uInt16 nPgNum) override;
+    virtual rtl::Reference<SdrPage> RemovePage(sal_uInt16 nPgNum) override;
     virtual void     InsertMasterPage(SdrPage* pPage, sal_uInt16 nPos=0xFFFF) 
override;
-    virtual SdrPage* RemoveMasterPage(sal_uInt16 nPgNum) override;
+    virtual rtl::Reference<SdrPage> RemoveMasterPage(sal_uInt16 nPgNum) 
override;
 
     SfxObjectShell*         GetObjectShell() const { return m_pObjShell; }
     void                    SetObjectShell( SfxObjectShell* pShell );
diff --git a/include/svx/fmpage.hxx b/include/svx/fmpage.hxx
index 7459837ec31e..f8b3c11cb72f 100644
--- a/include/svx/fmpage.hxx
+++ b/include/svx/fmpage.hxx
@@ -50,7 +50,7 @@ public:
     explicit FmFormPage(FmFormModel& rModel, bool bMasterPage=false);
     virtual ~FmFormPage() override;
 
-    virtual SdrPage* CloneSdrPage(SdrModel& rTargetModel) const override;
+    virtual rtl::Reference<SdrPage> CloneSdrPage(SdrModel& rTargetModel) const 
override;
 
     virtual void    InsertObject(SdrObject* pObj, size_t nPos = SAL_MAX_SIZE) 
override;
 
diff --git a/include/svx/svdmodel.hxx b/include/svx/svdmodel.hxx
index 1d849acd71cd..0cf3828f5bf1 100644
--- a/include/svx/svdmodel.hxx
+++ b/include/svx/svdmodel.hxx
@@ -162,8 +162,8 @@ private:
     std::unordered_set< const SdrObject* >  maAllIncarnatedObjects;
 #endif
 protected:
-    std::vector<SdrPage*> maMaPag;     // master pages
-    std::vector<SdrPage*> maPages;
+    std::vector<rtl::Reference<SdrPage>> maMaPag;     // master pages
+    std::vector<rtl::Reference<SdrPage>> maPages;
     std::function<void(std::unique_ptr<SdrUndoAction>)> m_aUndoLink;  // link 
to a NotifyUndo-Handler
     Size           m_aMaxObjSize; // e.g. for auto-growing text
     Fraction       m_aObjUnit;   // description of the coordinate units for 
ClipBoard, Drag&Drop, ...
@@ -304,7 +304,7 @@ public:
     void BurnInStyleSheetAttributes();
     // If you inherit from SdrPage you also need to inherit from SdrModel
     // and implement both VM AllocPage() and AllocModel()...
-    virtual SdrPage*  AllocPage(bool bMasterPage);
+    virtual rtl::Reference<SdrPage>  AllocPage(bool bMasterPage);
     virtual SdrModel* AllocModel() const;
 
     // Changes on the layers set the modified flag and broadcast on the model!
@@ -397,7 +397,7 @@ public:
     virtual void     InsertPage(SdrPage* pPage, sal_uInt16 nPos=0xFFFF);
     virtual void     DeletePage(sal_uInt16 nPgNum);
     // Remove means transferring ownership to the caller (opposite of Insert)
-    virtual SdrPage* RemovePage(sal_uInt16 nPgNum);
+    virtual rtl::Reference<SdrPage> RemovePage(sal_uInt16 nPgNum);
     virtual void     MovePage(sal_uInt16 nPgNum, sal_uInt16 nNewPos);
     const SdrPage*   GetPage(sal_uInt16 nPgNum) const;
     SdrPage*         GetPage(sal_uInt16 nPgNum);
@@ -408,7 +408,7 @@ public:
     virtual void     InsertMasterPage(SdrPage* pPage, sal_uInt16 nPos=0xFFFF);
     void             DeleteMasterPage(sal_uInt16 nPgNum);
     // Remove means transferring ownership to the caller (opposite of Insert)
-    virtual SdrPage* RemoveMasterPage(sal_uInt16 nPgNum);
+    virtual rtl::Reference<SdrPage> RemoveMasterPage(sal_uInt16 nPgNum);
     void             MoveMasterPage(sal_uInt16 nPgNum, sal_uInt16 nNewPos);
     const SdrPage*   GetMasterPage(sal_uInt16 nPgNum) const;
     SdrPage*         GetMasterPage(sal_uInt16 nPgNum);
diff --git a/include/svx/svdpage.hxx b/include/svx/svdpage.hxx
index d2fd71482476..8ac47f247415 100644
--- a/include/svx/svdpage.hxx
+++ b/include/svx/svdpage.hxx
@@ -20,9 +20,9 @@
 #ifndef INCLUDED_SVX_SVDPAGE_HXX
 #define INCLUDED_SVX_SVDPAGE_HXX
 
+#include <cppuhelper/weak.hxx>
 #include <svl/stylesheetuser.hxx>
 #include <vcl/prntypes.hxx>
-#include <tools/weakbase.h>
 #include <svl/itemset.hxx>
 #include <svx/sdrpageuser.hxx>
 #include <svx/sdr/contact/viewobjectcontactredirector.hxx>
@@ -362,7 +362,7 @@ public:
 //          SwDPage
 //      OReportPage
 
-class SVXCORE_DLLPUBLIC SdrPage : public SdrObjList, public tools::WeakBase
+class SVXCORE_DLLPUBLIC SdrPage : public SdrObjList, public cppu::OWeakObject
 {
     // #i9076#
     friend class SdrModel;
@@ -446,7 +446,7 @@ public:
     explicit SdrPage(SdrModel& rModel, bool bMasterPage=false);
     virtual ~SdrPage() override;
 
-    virtual SdrPage* CloneSdrPage(SdrModel& rTargetModel) const;
+    virtual rtl::Reference<SdrPage> CloneSdrPage(SdrModel& rTargetModel) const;
     bool             IsMasterPage() const       { return mbMaster; }
     void             SetInserted(bool bNew = true);
     bool             IsInserted() const         { return mbInserted; }
diff --git a/include/svx/svdundo.hxx b/include/svx/svdundo.hxx
index e56b6d52f9a7..04665b9daa28 100644
--- a/include/svx/svdundo.hxx
+++ b/include/svx/svdundo.hxx
@@ -518,7 +518,7 @@ public:
 class SdrUndoPage : public SdrUndoAction
 {
 protected:
-    SdrPage&                    mrPage;
+    rtl::Reference<SdrPage>   mxPage;
 
 protected:
     void ImpInsertPage(sal_uInt16 nNum);
@@ -527,6 +527,7 @@ protected:
 
 protected:
     SdrUndoPage(SdrPage& rNewPg);
+    ~SdrUndoPage();
 
     static OUString ImpGetDescriptionStr(const char* pStrCacheID);
 };
@@ -540,10 +541,6 @@ class SVXCORE_DLLPUBLIC SdrUndoPageList : public 
SdrUndoPage
 protected:
     sal_uInt16                      nPageNum;
 
-    // It's possible that the object is re-assigned during a Undo/Redo.
-    // The Page is deleted in the dtor, if bItsMine==TRUE
-    bool                        bItsMine;
-
 protected:
     SdrUndoPageList(SdrPage& rNewPg);
     virtual ~SdrUndoPageList() override;
diff --git a/reportdesign/inc/RptModel.hxx b/reportdesign/inc/RptModel.hxx
index 260e40f4c978..3f17073ac741 100644
--- a/reportdesign/inc/RptModel.hxx
+++ b/reportdesign/inc/RptModel.hxx
@@ -58,8 +58,8 @@ public:
     virtual ~OReportModel() override;
 
     virtual void        SetChanged(bool bFlg = true) override;
-    virtual SdrPage*    AllocPage(bool bMasterPage) override;
-    virtual SdrPage*    RemovePage(sal_uInt16 nPgNum) override;
+    virtual rtl::Reference<SdrPage> AllocPage(bool bMasterPage) override;
+    virtual rtl::Reference<SdrPage> RemovePage(sal_uInt16 nPgNum) override;
     /** @returns the numbering type that is used to format page fields in 
drawing shapes */
     virtual SvxNumType  GetPageNumType() const override;
 
diff --git a/reportdesign/inc/RptPage.hxx b/reportdesign/inc/RptPage.hxx
index 1c0f77b0adeb..637687ad9a04 100644
--- a/reportdesign/inc/RptPage.hxx
+++ b/reportdesign/inc/RptPage.hxx
@@ -54,7 +54,7 @@ public:
     OReportPage( OReportModel& rModel
                 ,const css::uno::Reference< css::report::XSection >& _xSection 
);
 
-    virtual SdrPage* CloneSdrPage(SdrModel& rTargetModel) const override;
+    virtual rtl::Reference<SdrPage> CloneSdrPage(SdrModel& rTargetModel) const 
override;
 
     virtual void NbcInsertObject(SdrObject* pObj, size_t nPos=SAL_MAX_SIZE) 
override;
     virtual SdrObject* RemoveObject(size_t nObjNum) override;
diff --git a/reportdesign/source/core/sdr/RptModel.cxx 
b/reportdesign/source/core/sdr/RptModel.cxx
index 0a636fa2f93a..3ac79aa42beb 100644
--- a/reportdesign/source/core/sdr/RptModel.cxx
+++ b/reportdesign/source/core/sdr/RptModel.cxx
@@ -66,7 +66,7 @@ void OReportModel::detachController()
     m_xUndoEnv->Clear(OXUndoEnvironment::Accessor());
 }
 
-SdrPage* OReportModel::AllocPage(bool /*bMasterPage*/)
+rtl::Reference<SdrPage> OReportModel::AllocPage(bool /*bMasterPage*/)
 {
     OSL_FAIL("Who called me!");
     return nullptr;
@@ -86,19 +86,19 @@ void OReportModel::SetModified(bool _bModified)
         m_pController->setModified(_bModified);
 }
 
-SdrPage* OReportModel::RemovePage(sal_uInt16 nPgNum)
+rtl::Reference<SdrPage> OReportModel::RemovePage(sal_uInt16 nPgNum)
 {
-    OReportPage* pPage = 
dynamic_cast<OReportPage*>(SdrModel::RemovePage(nPgNum));
+    rtl::Reference<OReportPage> pPage = 
dynamic_cast<OReportPage*>(SdrModel::RemovePage(nPgNum).get());
     return pPage;
 }
 
 OReportPage* OReportModel::createNewPage(const uno::Reference< 
report::XSection >& _xSection)
 {
     SolarMutexGuard aSolarGuard;
-    OReportPage* pPage = new OReportPage( *this ,_xSection);
-    InsertPage(pPage);
+    rtl::Reference<OReportPage> pPage = new OReportPage( *this ,_xSection);
+    InsertPage(pPage.get());
     m_xUndoEnv->AddSection(_xSection);
-    return pPage;
+    return pPage.get();
 }
 
 OReportPage* OReportModel::getPage(const uno::Reference< report::XSection >& 
_xSection)
diff --git a/reportdesign/source/core/sdr/RptPage.cxx 
b/reportdesign/source/core/sdr/RptPage.cxx
index 4c1fcba02f43..0b856322796c 100644
--- a/reportdesign/source/core/sdr/RptPage.cxx
+++ b/reportdesign/source/core/sdr/RptPage.cxx
@@ -41,10 +41,10 @@ OReportPage::~OReportPage()
 {
 }
 
-SdrPage* OReportPage::CloneSdrPage(SdrModel& rTargetModel) const
+rtl::Reference<SdrPage> OReportPage::CloneSdrPage(SdrModel& rTargetModel) const
 {
     OReportModel& rOReportModel(static_cast< OReportModel& >(rTargetModel));
-    OReportPage* pClonedOReportPage(
+    rtl::Reference<OReportPage> pClonedOReportPage(
         new OReportPage(
             rOReportModel,
             m_xSection));
diff --git a/sc/inc/drawpage.hxx b/sc/inc/drawpage.hxx
index 09e607ea88b8..ce1e5ed58ea3 100644
--- a/sc/inc/drawpage.hxx
+++ b/sc/inc/drawpage.hxx
@@ -33,7 +33,7 @@ public:
     explicit ScDrawPage(ScDrawLayer& rNewModel, bool bMasterPage);
     virtual ~ScDrawPage() override;
 
-    virtual ScDrawPage* CloneSdrPage(SdrModel& rTargetModel) const override;
+    virtual rtl::Reference<SdrPage> CloneSdrPage(SdrModel& rTargetModel) const 
override;
 
     virtual css::uno::Reference<css::uno::XInterface> createUnoPage() override;
 };
diff --git a/sc/inc/drwlayer.hxx b/sc/inc/drwlayer.hxx
index 3e5119b8e65c..7cb266bdf600 100644
--- a/sc/inc/drwlayer.hxx
+++ b/sc/inc/drwlayer.hxx
@@ -114,7 +114,7 @@ public:
                     ScDrawLayer( ScDocument* pDocument, const OUString& rName 
);
     virtual         ~ScDrawLayer() override;
 
-    virtual SdrPage*  AllocPage(bool bMasterPage) override;
+    virtual rtl::Reference<SdrPage> AllocPage(bool bMasterPage) override;
     virtual SdrModel* AllocModel() const override;
     virtual void    SetChanged( bool bFlg = true ) override;
 
diff --git a/sc/source/core/data/drawpage.cxx b/sc/source/core/data/drawpage.cxx
index 0adba889261f..6b6f029fb205 100644
--- a/sc/source/core/data/drawpage.cxx
+++ b/sc/source/core/data/drawpage.cxx
@@ -32,10 +32,10 @@ ScDrawPage::~ScDrawPage()
 {
 }
 
-ScDrawPage* ScDrawPage::CloneSdrPage(SdrModel& rTargetModel) const
+rtl::Reference<SdrPage> ScDrawPage::CloneSdrPage(SdrModel& rTargetModel) const
 {
     ScDrawLayer& rScDrawLayer(static_cast< ScDrawLayer& >(rTargetModel));
-    ScDrawPage* pClonedScDrawPage(
+    rtl::Reference<ScDrawPage> pClonedScDrawPage(
         new ScDrawPage(
             rScDrawLayer,
             IsMasterPage()));
diff --git a/sc/source/core/data/drwlayer.cxx b/sc/source/core/data/drwlayer.cxx
index 9729629514ef..8e74fb8d1686 100644
--- a/sc/source/core/data/drwlayer.cxx
+++ b/sc/source/core/data/drwlayer.cxx
@@ -351,7 +351,7 @@ void ScDrawLayer::UseHyphenator()
     }
 }
 
-SdrPage* ScDrawLayer::AllocPage(bool bMasterPage)
+rtl::Reference<SdrPage> ScDrawLayer::AllocPage(bool bMasterPage)
 {
     return new ScDrawPage(*this, bMasterPage);
 }
@@ -381,8 +381,8 @@ bool ScDrawLayer::ScAddPage( SCTAB nTab )
     if (bDrawIsInUndo)
         return false;   // not inserted
 
-    ScDrawPage* pPage = static_cast<ScDrawPage*>(AllocPage( false ));
-    InsertPage(pPage, static_cast<sal_uInt16>(nTab));
+    rtl::Reference<ScDrawPage> pPage = static_cast<ScDrawPage*>(AllocPage( 
false ).get());
+    InsertPage(pPage.get(), static_cast<sal_uInt16>(nTab));
     if (bRecording)
         AddCalcUndo(std::make_unique<SdrUndoNewPage>(*pPage));
 
diff --git a/sc/source/ui/view/notemark.cxx b/sc/source/ui/view/notemark.cxx
index 9a6417856b24..7b8dd7f6aef2 100644
--- a/sc/source/ui/view/notemark.cxx
+++ b/sc/source/ui/view/notemark.cxx
@@ -90,7 +90,7 @@ IMPL_LINK_NOARG(ScNoteMarker, TimeHdl, Timer *, void)
             rOutliner.SetRefDevice(pPrinter);
         }
 
-        if( SdrPage* pPage = m_pModel->AllocPage( false ) )
+        if( rtl::Reference<SdrPage> pPage = m_pModel->AllocPage( false ) )
 
         {
             m_xObject = ScNoteUtil::CreateTempCaption( *m_pDoc, m_aDocPos, 
*pPage, m_aUserText, m_aVisRect, m_bLeft );
@@ -107,7 +107,7 @@ IMPL_LINK_NOARG(ScNoteMarker, TimeHdl, Timer *, void)
             }
 
             // Insert page so that the model recognise it and also deleted
-            m_pModel->InsertPage( pPage );
+            m_pModel->InsertPage( pPage.get() );
 
         }
         m_bVisible = true;
diff --git a/sd/inc/drawdoc.hxx b/sd/inc/drawdoc.hxx
index ba88b47b6005..e51886931fd7 100644
--- a/sd/inc/drawdoc.hxx
+++ b/sd/inc/drawdoc.hxx
@@ -224,8 +224,8 @@ public:
     SAL_DLLPRIVATE SdDrawDocument*     AllocSdDrawDocument() const;
     SAL_DLLPRIVATE virtual SdrModel*   AllocModel() const override; //forwards 
to AllocSdDrawDocument
 
-    SAL_DLLPRIVATE SdPage*             AllocSdPage(bool bMasterPage);
-    SAL_DLLPRIVATE virtual SdrPage*    AllocPage(bool bMasterPage) override; 
//forwards to AllocSdPage
+    SAL_DLLPRIVATE rtl::Reference<SdPage>          AllocSdPage(bool 
bMasterPage);
+    SAL_DLLPRIVATE virtual rtl::Reference<SdrPage> AllocPage(bool bMasterPage) 
override; //forwards to AllocSdPage
 
     SAL_DLLPRIVATE virtual bool        IsReadOnly() const override;
     SAL_DLLPRIVATE virtual void        SetChanged(bool bFlag = true) override;
@@ -262,10 +262,10 @@ public:
     SAL_DLLPRIVATE void                MovePage(sal_uInt16 nPgNum, sal_uInt16 
nNewPos) override;
     SAL_DLLPRIVATE void                InsertPage(SdrPage* pPage, sal_uInt16 
nPos=0xFFFF) override;
     SAL_DLLPRIVATE void                DeletePage(sal_uInt16 nPgNum) override;
-    SAL_DLLPRIVATE SdrPage*            RemovePage(sal_uInt16 nPgNum) override;
+    SAL_DLLPRIVATE rtl::Reference<SdrPage> RemovePage(sal_uInt16 nPgNum) 
override;
 
     SAL_DLLPRIVATE virtual void     InsertMasterPage(SdrPage* pPage, 
sal_uInt16 nPos=0xFFFF) override;
-    SAL_DLLPRIVATE virtual SdrPage* RemoveMasterPage(sal_uInt16 nPgNum) 
override;
+    SAL_DLLPRIVATE virtual rtl::Reference<SdrPage> RemoveMasterPage(sal_uInt16 
nPgNum) override;
 
     SAL_DLLPRIVATE void                RemoveUnnecessaryMasterPages( SdPage* 
pMaster=nullptr, bool bOnlyDuplicatePages=false, bool bUndo=true );
     void   SetMasterPage(sal_uInt16 nSdPageNum, std::u16string_view 
rLayoutName,
diff --git a/sd/inc/sdpage.hxx b/sd/inc/sdpage.hxx
index cca449bcce8d..d1662ee43308 100644
--- a/sd/inc/sdpage.hxx
+++ b/sd/inc/sdpage.hxx
@@ -154,7 +154,7 @@ public:
     SdPage(SdDrawDocument& rNewDoc, bool bMasterPage);
     virtual ~SdPage() override;
 
-    virtual SdrPage* CloneSdrPage(SdrModel& rTargetModel) const override;
+    virtual rtl::Reference<SdrPage> CloneSdrPage(SdrModel& rTargetModel) const 
override;
 
     virtual void    SetSize(const Size& aSize) override;
     virtual void    SetBorder(sal_Int32 nLft, sal_Int32 nUpp, sal_Int32 nRgt, 
sal_Int32 Lwr) override;
diff --git a/sd/inc/undo/undoobjects.hxx b/sd/inc/undo/undoobjects.hxx
index 5670002345f2..b7eae9b85d66 100644
--- a/sd/inc/undo/undoobjects.hxx
+++ b/sd/inc/undo/undoobjects.hxx
@@ -24,6 +24,7 @@
 #include <svx/svdundo.hxx>
 #include <pres.hxx>
 #include <tools/weakbase.hxx>
+#include <unotools/weakref.hxx>
 
 class SdrObjUserCall;
 class SdPage;
@@ -125,7 +126,7 @@ public:
 private:
     PresObjKind meOldKind;
     PresObjKind meNewKind;
-    ::tools::WeakReference<SdPage> mxPage;
+    ::unotools::WeakReference<SdPage> mxPage;
     ::tools::WeakReference<SdrObject> mxSdrObject;
 };
 
@@ -141,7 +142,7 @@ public:
     virtual void Redo() override;
 
 private:
-    ::tools::WeakReference<SdPage> mxPage;
+    ::unotools::WeakReference<SdPage> mxPage;
 };
 
 class UndoGeoObject final : public SdrUndoGeoObj
@@ -153,7 +154,7 @@ public:
     virtual void Redo() override;
 
 private:
-    ::tools::WeakReference<SdPage> mxPage;
+    ::unotools::WeakReference<SdPage> mxPage;
     ::tools::WeakReference<SdrObject> mxSdrObject;
 };
 
@@ -166,7 +167,7 @@ public:
     virtual void Redo() override;
 
 private:
-    ::tools::WeakReference<SdPage> mxPage;
+    ::unotools::WeakReference<SdPage> mxPage;
     ::tools::WeakReference<SdrObject> mxSdrObject;
 };
 
diff --git a/sd/qa/unit/uimpress.cxx b/sd/qa/unit/uimpress.cxx
index 0732f93be8ae..bd6bb16a75b5 100644
--- a/sd/qa/unit/uimpress.cxx
+++ b/sd/qa/unit/uimpress.cxx
@@ -17,6 +17,7 @@
 #include <comphelper/processfactory.hxx>
 
 #include <vcl/svapp.hxx>
+#include <svx/svdpage.hxx>
 #include <sddll.hxx>
 #include <drawdoc.hxx>
 
@@ -77,16 +78,16 @@ void Test::tearDown()
 
 void Test::testAddPage()
 {
-    SdrPage* pPage = m_pDoc->AllocPage(false);
-    m_pDoc->InsertPage(pPage);
+    rtl::Reference<SdrPage> pPage = m_pDoc->AllocPage(false);
+    m_pDoc->InsertPage(pPage.get());
     CPPUNIT_ASSERT_EQUAL_MESSAGE("added one page to model",
                                  static_cast<sal_uInt16>(1), 
m_pDoc->GetPageCount());
     m_pDoc->DeletePage(0);
     CPPUNIT_ASSERT_EQUAL_MESSAGE("removed one page to model",
                                  static_cast<sal_uInt16>(0), 
m_pDoc->GetPageCount());
 
-    SdrPage* pMasterPage = m_pDoc->AllocPage(true);
-    m_pDoc->InsertMasterPage(pMasterPage);
+    rtl::Reference<SdrPage> pMasterPage = m_pDoc->AllocPage(true);
+    m_pDoc->InsertMasterPage(pMasterPage.get());
     CPPUNIT_ASSERT_EQUAL_MESSAGE("added one master page to model",
                                  static_cast<sal_uInt16>(1), 
m_pDoc->GetMasterPageCount());
     m_pDoc->DeleteMasterPage(0);
diff --git a/sd/source/core/drawdoc.cxx b/sd/source/core/drawdoc.cxx
index 69f746150d64..c257fa909ae2 100644
--- a/sd/source/core/drawdoc.cxx
+++ b/sd/source/core/drawdoc.cxx
@@ -639,7 +639,7 @@ SdDrawDocument* SdDrawDocument::AllocSdDrawDocument() const
     return pNewModel;
 }
 
-SdPage* SdDrawDocument::AllocSdPage(bool bMasterPage)
+rtl::Reference<SdPage> SdDrawDocument::AllocSdPage(bool bMasterPage)
 {
     return new SdPage(*this, bMasterPage);
 }
@@ -647,7 +647,7 @@ SdPage* SdDrawDocument::AllocSdPage(bool bMasterPage)
 // This method creates a new page (SdPage) and returns a pointer to said page.
 // The drawing engine uses this method to create pages (whose types it does
 // not know, as they are _derivatives_ of SdrPage) when loading.
-SdrPage* SdDrawDocument::AllocPage(bool bMasterPage)
+rtl::Reference<SdrPage> SdDrawDocument::AllocPage(bool bMasterPage)
 {
     return AllocSdPage(bMasterPage);
 }
diff --git a/sd/source/core/drawdoc2.cxx b/sd/source/core/drawdoc2.cxx
index f9af8c83b2ba..3c3d4988d236 100644
--- a/sd/source/core/drawdoc2.cxx
+++ b/sd/source/core/drawdoc2.cxx
@@ -406,21 +406,21 @@ void SdDrawDocument::DeletePage(sal_uInt16 nPgNum)
 }
 
 // Remove page
-SdrPage* SdDrawDocument::RemovePage(sal_uInt16 nPgNum)
+rtl::Reference<SdrPage> SdDrawDocument::RemovePage(sal_uInt16 nPgNum)
 {
-    SdrPage* pPage = FmFormModel::RemovePage(nPgNum);
+    rtl::Reference<SdrPage> pPage = FmFormModel::RemovePage(nPgNum);
 
     bool bLast = ((nPgNum+1)/2 == (GetPageCount()+1)/2);
 
-    auto pSdPage = static_cast<SdPage*>(pPage);
+    auto pSdPage = static_cast<SdPage*>(pPage.get());
     pSdPage->DisconnectLink();
     ReplacePageInCustomShows( pSdPage, nullptr );
     UpdatePageObjectsInNotes(nPgNum);
 
     if (!bLast)
-        UpdatePageRelativeURLs(static_cast<SdPage*>(pPage), nPgNum, -1);
+        UpdatePageRelativeURLs(pSdPage, nPgNum, -1);
 
-    if (comphelper::LibreOfficeKit::isActive() && 
static_cast<SdPage*>(pPage)->GetPageKind() == PageKind::Standard)
+    if (comphelper::LibreOfficeKit::isActive() && pSdPage->GetPageKind() == 
PageKind::Standard)
     {
         SdXImpressDocument* pDoc = 
comphelper::getUnoTunnelImplementation<SdXImpressDocument>(this->getUnoModel());
         SfxLokHelper::notifyDocumentSizeChangedAllViews(pDoc);
@@ -443,7 +443,7 @@ void SdDrawDocument::InsertMasterPage(SdrPage* pPage, 
sal_uInt16 nPos )
     }
 }
 
-SdrPage* SdDrawDocument::RemoveMasterPage(sal_uInt16 nPgNum)
+rtl::Reference<SdrPage> SdDrawDocument::RemoveMasterPage(sal_uInt16 nPgNum)
 {
     SdPage* pPage = static_cast<SdPage*>(GetMasterPage(nPgNum ));
     if( pPage && pPage->IsMasterPage() && (pPage->GetPageKind() == 
PageKind::Standard) )
@@ -502,7 +502,7 @@ void SdDrawDocument::CreateFirstPages( SdDrawDocument const 
* pRefDocument /* =
     Size aDefSize = SvxPaperInfo::GetDefaultPaperSize( MapUnit::Map100thMM );
 
     // Insert handout page
-    SdPage* pHandoutPage = AllocSdPage(false);
+    rtl::Reference<SdPage> pHandoutPage = AllocSdPage(false);
 
     SdPage* pRefPage = nullptr;
 
@@ -522,23 +522,23 @@ void SdDrawDocument::CreateFirstPages( SdDrawDocument 
const * pRefDocument /* =
 
     pHandoutPage->SetPageKind(PageKind::Handout);
     pHandoutPage->SetName( SdResId(STR_HANDOUT) );
-    InsertPage(pHandoutPage, 0);
+    InsertPage(pHandoutPage.get(), 0);
 
     // Insert master page and register this with the handout page
-    SdPage* pHandoutMPage = AllocSdPage(true);
+    rtl::Reference<SdPage> pHandoutMPage = AllocSdPage(true);
     pHandoutMPage->SetSize( pHandoutPage->GetSize() );
     pHandoutMPage->SetPageKind(PageKind::Handout);
     pHandoutMPage->SetBorder( pHandoutPage->GetLeftBorder(),
                               pHandoutPage->GetUpperBorder(),
                               pHandoutPage->GetRightBorder(),
                               pHandoutPage->GetLowerBorder() );
-    InsertMasterPage(pHandoutMPage, 0);
+    InsertMasterPage(pHandoutMPage.get(), 0);
     pHandoutPage->TRG_SetMasterPage( *pHandoutMPage );
 
     // Insert page
     // If nPageCount==1 is, the model for the clipboard was created, thus a
     // default page must already exist
-    SdPage* pPage;
+    rtl::Reference<SdPage> pPage;
     bool bClipboard = false;
 
     if( pRefDocument )
@@ -591,7 +591,7 @@ void SdDrawDocument::CreateFirstPages( SdDrawDocument const 
* pRefDocument /* =
             pPage->SetBorder(0, 0, 0, 0);
         }
 
-        InsertPage(pPage, 1);
+        InsertPage(pPage.get(), 1);
     }
     else
     {
@@ -600,19 +600,19 @@ void SdDrawDocument::CreateFirstPages( SdDrawDocument 
const * pRefDocument /* =
     }
 
     // Insert master page, then register this with the page
-    SdPage* pMPage = AllocSdPage(true);
+    rtl::Reference<SdPage> pMPage = AllocSdPage(true);
     pMPage->SetSize( pPage->GetSize() );
     pMPage->SetBorder( pPage->GetLeftBorder(),
                        pPage->GetUpperBorder(),
                        pPage->GetRightBorder(),
                        pPage->GetLowerBorder() );
-    InsertMasterPage(pMPage, 1);
+    InsertMasterPage(pMPage.get(), 1);
     pPage->TRG_SetMasterPage( *pMPage );
     if( bClipboard )
         pMPage->SetLayoutName( pPage->GetLayoutName() );
 
     // Insert notes page
-    SdPage* pNotesPage = AllocSdPage(false);
+    rtl::Reference<SdPage> pNotesPage = AllocSdPage(false);
 
     if( pRefDocument )
         pRefPage = pRefDocument->GetSdPage( 0, PageKind::Notes );
@@ -637,19 +637,19 @@ void SdDrawDocument::CreateFirstPages( SdDrawDocument 
const * pRefDocument /* =
         pNotesPage->SetBorder(0, 0, 0, 0);
     }
     pNotesPage->SetPageKind(PageKind::Notes);
-    InsertPage(pNotesPage, 2);
+    InsertPage(pNotesPage.get(), 2);
     if( bClipboard )
         pNotesPage->SetLayoutName( pPage->GetLayoutName() );
 
     // Insert master page, then register this with the notes page
-    SdPage* pNotesMPage = AllocSdPage(true);
+    rtl::Reference<SdPage> pNotesMPage = AllocSdPage(true);
     pNotesMPage->SetSize( pNotesPage->GetSize() );
     pNotesMPage->SetPageKind(PageKind::Notes);
     pNotesMPage->SetBorder( pNotesPage->GetLeftBorder(),
                             pNotesPage->GetUpperBorder(),
                             pNotesPage->GetRightBorder(),
                             pNotesPage->GetLowerBorder() );
-    InsertMasterPage(pNotesMPage, 2);
+    InsertMasterPage(pNotesMPage.get(), 2);
     pNotesPage->TRG_SetMasterPage( *pNotesMPage );
     if( bClipboard )
         pNotesMPage->SetLayoutName( pPage->GetLayoutName() );
@@ -1089,7 +1089,7 @@ void SdDrawDocument::CheckMasterPages()
                 if( nFound == nMaxPages )
                     pRefNotesPage = nullptr;
 
-                SdPage* pNewNotesPage = AllocSdPage(true);
+                rtl::Reference<SdPage> pNewNotesPage = AllocSdPage(true);
                 pNewNotesPage->SetPageKind(PageKind::Notes);
                 if( pRefNotesPage )
                 {
@@ -1099,7 +1099,7 @@ void SdDrawDocument::CheckMasterPages()
                                             pRefNotesPage->GetRightBorder(),
                                             pRefNotesPage->GetLowerBorder() );
                 }
-                InsertMasterPage(pNewNotesPage,  nPage );
+                InsertMasterPage(pNewNotesPage.get(),  nPage );
                 pNewNotesPage->SetLayoutName( pPage->GetLayoutName() );
                 pNewNotesPage->SetAutoLayout(AUTOLAYOUT_NOTES, true, true );
                 nMaxPages++;
@@ -1138,8 +1138,8 @@ sal_uInt16 SdDrawDocument::CreatePage (
 {
     SdPage* pPreviousStandardPage;
     SdPage* pPreviousNotesPage;
-    SdPage* pStandardPage;
-    SdPage* pNotesPage;
+    rtl::Reference<SdPage> pStandardPage;
+    rtl::Reference<SdPage> pNotesPage;
 
     // From the given page determine the standard page and notes page of which
     // to take the layout and the position where to insert the new pages.
@@ -1207,8 +1207,8 @@ sal_uInt16 SdDrawDocument::CreatePage (
         sNotesPageName,
         bIsPageBack,
         bIsPageObj,
-        pStandardPage,
-        pNotesPage,
+        pStandardPage.get(),
+        pNotesPage.get(),
         nInsertPosition);
 }
 
@@ -1244,8 +1244,8 @@ sal_uInt16 SdDrawDocument::DuplicatePage (
 {
     SdPage* pPreviousStandardPage;
     SdPage* pPreviousNotesPage;
-    SdPage* pStandardPage;
-    SdPage* pNotesPage;
+    rtl::Reference<SdPage> pStandardPage;
+    rtl::Reference<SdPage> pNotesPage;
 
     // From the given page determine the standard page and the notes page
     // of which to make copies.
@@ -1263,8 +1263,8 @@ sal_uInt16 SdDrawDocument::DuplicatePage (
     }
 
     // Create duplicates of a standard page and the associated notes page
-    pStandardPage = static_cast<SdPage*>( 
pPreviousStandardPage->CloneSdrPage(*this) );
-    pNotesPage = static_cast<SdPage*>( pPreviousNotesPage->CloneSdrPage(*this) 
);
+    pStandardPage = static_cast<SdPage*>( 
pPreviousStandardPage->CloneSdrPage(*this).get() );
+    pNotesPage = static_cast<SdPage*>( 
pPreviousNotesPage->CloneSdrPage(*this).get() );
 
     return InsertPageSet (
         pActualPage,
@@ -1273,8 +1273,8 @@ sal_uInt16 SdDrawDocument::DuplicatePage (
         sNotesPageName,
         bIsPageBack,
         bIsPageObj,
-        pStandardPage,
-        pNotesPage,
+        pStandardPage.get(),
+        pNotesPage.get(),
         nInsertPosition);
 }
 
diff --git a/sd/source/core/drawdoc3.cxx b/sd/source/core/drawdoc3.cxx
index e48117625dfe..fe99838bd31c 100644
--- a/sd/source/core/drawdoc3.cxx
+++ b/sd/source/core/drawdoc3.cxx
@@ -721,9 +721,6 @@ bool SdDrawDocument::InsertBookmarkAsPage(
                             
AddUndo(GetSdrUndoFactory().CreateUndoDeletePage(*pStandardPage));
 
                         RemovePage(nDestPageNum);
-
-                        if( !bUndo )
-                            delete pStandardPage;
                     }
 
                     SdPage* pNotesPage = nullptr;
@@ -747,9 +744,6 @@ bool SdDrawDocument::InsertBookmarkAsPage(
                             
AddUndo(GetSdrUndoFactory().CreateUndoDeletePage(*pNotesPage));
 
                         RemovePage(nDestPageNum);
-
-                        if( !bUndo )
-                            delete pNotesPage;
                     }
 
                     nReplacedStandardPages++;
@@ -787,8 +781,6 @@ bool SdDrawDocument::InsertBookmarkAsPage(
 
                 RemoveMasterPage(nPage);
 
-                if( !bUndo )
-                    delete pRefPage;
                 nNewMPageCount--;
                 break;
             }
@@ -1253,17 +1245,11 @@ void 
SdDrawDocument::RemoveUnnecessaryMasterPages(SdPage* pMasterPage, bool bOnl
 
                 RemoveMasterPage( pNotesMaster->GetPageNum() );
 
-                if( !bUndo )
-                    delete pNotesMaster;
-
                 if( bUndo )
                     
AddUndo(GetSdrUndoFactory().CreateUndoDeletePage(*pMaster));
 
                 RemoveMasterPage( pMaster->GetPageNum() );
 
-                if( !bUndo )
-                    delete pMaster;
-
                 if( bUndo )
                     EndUndo();  // do this here already, so Joe's actions 
happen _between_ our own
 
@@ -1394,8 +1380,8 @@ void SdDrawDocument::SetMasterPage(sal_uInt16 nSdPageNum,
     SdPage* pNotes          = static_cast<SdPage*>( 
GetPage(pSelectedPage->GetPageNum()+1) );
     SdPage& rOldMaster      = 
static_cast<SdPage&>(pSelectedPage->TRG_GetMasterPage());
     SdPage& rOldNotesMaster = 
static_cast<SdPage&>(pNotes->TRG_GetMasterPage());
-    SdPage* pMaster         = nullptr;
-    SdPage* pNotesMaster    = nullptr;
+    rtl::Reference<SdPage> pMaster;
+    rtl::Reference<SdPage> pNotesMaster;
     OUString aOldPageLayoutName(pSelectedPage->GetLayoutName());
     OUString aOldLayoutName(aOldPageLayoutName);
     sal_Int32 nIndex = aOldLayoutName.indexOf( SD_LT_SEPARATOR );
@@ -1465,8 +1451,8 @@ void SdDrawDocument::SetMasterPage(sal_uInt16 nSdPageNum,
         if (pSourceDoc != this)
         {
             // #i121863# clone masterpages, they are from another model (!)
-            std::unique_ptr<SdPage> pNewNotesMaster(dynamic_cast< SdPage* 
>(pNotesMaster->CloneSdrPage(*this)));
-            std::unique_ptr<SdPage> pNewMaster(dynamic_cast< SdPage* 
>(pMaster->CloneSdrPage(*this)));
+            rtl::Reference<SdPage> pNewNotesMaster(dynamic_cast< SdPage* 
>(pNotesMaster->CloneSdrPage(*this).get()));
+            rtl::Reference<SdPage> pNewMaster(dynamic_cast< SdPage* 
>(pMaster->CloneSdrPage(*this).get()));
 
             if(!pNewNotesMaster || !pNewMaster)
             {
@@ -1474,8 +1460,8 @@ void SdDrawDocument::SetMasterPage(sal_uInt16 nSdPageNum,
                 return;
             }
 
-            pNotesMaster = pNewNotesMaster.release();
-            pMaster = pNewMaster.release();
+            pNotesMaster = pNewNotesMaster;
+            pMaster = pNewMaster;
 
             // layout name needs to be unique
             aTargetNewLayoutName = pMaster->GetLayoutName();
@@ -1648,14 +1634,14 @@ void SdDrawDocument::SetMasterPage(sal_uInt16 
nSdPageNum,
 
             if (!bLayoutReloaded)
                 nInsertPos = 0xFFFF;
-            InsertMasterPage(pMaster, nInsertPos);
+            InsertMasterPage(pMaster.get(), nInsertPos);
             if( bUndo )
                 AddUndo(GetSdrUndoFactory().CreateUndoNewPage(*pMaster));
 
             nInsertPos++;
             if (!bLayoutReloaded)
                 nInsertPos = 0xFFFF;
-            InsertMasterPage(pNotesMaster, nInsertPos);
+            InsertMasterPage(pNotesMaster.get(), nInsertPos);
             if( bUndo )
             {
                 AddUndo(GetSdrUndoFactory().CreateUndoNewPage(*pNotesMaster));
@@ -1665,7 +1651,7 @@ void SdDrawDocument::SetMasterPage(sal_uInt16 nSdPageNum,
         }
 
         // Fill list with pages
-        std::vector<SdPage*> aPageList;
+        std::vector<rtl::Reference<SdPage>> aPageList;
 
 //      #98456, this has to be removed according to CL (KA 07/08/2002)
 //      #109884# but we need them again to restore the styles of the 
presentation objects while undo
@@ -1691,7 +1677,7 @@ void SdDrawDocument::SetMasterPage(sal_uInt16 nSdPageNum,
             aPageList.push_back(pNotes);
         }
 
-        for (SdPage* pPage : aPageList)
+        for (rtl::Reference<SdPage>& pPage : aPageList)
         {
             AutoLayout eAutoLayout = pPage->GetAutoLayout();
 
@@ -1701,7 +1687,7 @@ void SdDrawDocument::SetMasterPage(sal_uInt16 nSdPageNum,
                         (this,
                         pPage->IsMasterPage() ? aLayoutName : aOldLayoutName,
                         aLayoutName,
-                         eAutoLayout, eAutoLayout, false, pPage));
+                         eAutoLayout, eAutoLayout, false, pPage.get()));
             }
             pPage->SetPresentationLayout(aLayoutName);
             pPage->SetAutoLayout(eAutoLayout);
@@ -1776,7 +1762,7 @@ void SdDrawDocument::SetMasterPage(sal_uInt16 nSdPageNum,
                            pSelectedPage->GetLowerBorder() );
         pMaster->SetName(aName);
         pMaster->SetLayoutName(aPageLayoutName);
-        InsertMasterPage(pMaster);
+        InsertMasterPage(pMaster.get());
 
         if( bUndo )
             AddUndo(GetSdrUndoFactory().CreateUndoNewPage(*pMaster));
@@ -1792,7 +1778,7 @@ void SdDrawDocument::SetMasterPage(sal_uInt16 nSdPageNum,
                                 pNotes->GetLowerBorder() );
         pNotesMaster->SetName(aName);
         pNotesMaster->SetLayoutName(aPageLayoutName);
-        InsertMasterPage(pNotesMaster);
+        InsertMasterPage(pNotesMaster.get());
 
         if( bUndo )
             AddUndo(GetSdrUndoFactory().CreateUndoNewPage(*pNotesMaster));
diff --git a/sd/source/core/sdpage2.cxx b/sd/source/core/sdpage2.cxx
index 6680bdde7fc6..dcb63d5e51ea 100644
--- a/sd/source/core/sdpage2.cxx
+++ b/sd/source/core/sdpage2.cxx
@@ -418,10 +418,10 @@ void SdPage::lateInit(const SdPage& rSrcPage)
 |*
 \************************************************************************/
 
-SdrPage* SdPage::CloneSdrPage(SdrModel& rTargetModel) const
+rtl::Reference<SdrPage> SdPage::CloneSdrPage(SdrModel& rTargetModel) const
 {
     SdDrawDocument& rSdDrawDocument(static_cast< SdDrawDocument& 
>(rTargetModel));
-    SdPage* pClonedSdPage(
+    rtl::Reference<SdPage> pClonedSdPage(
         new SdPage(
             rSdDrawDocument,
             IsMasterPage()));
diff --git a/sd/source/core/stlfamily.cxx b/sd/source/core/stlfamily.cxx
index 164fb7814b41..a396bd1701ba 100644
--- a/sd/source/core/stlfamily.cxx
+++ b/sd/source/core/stlfamily.cxx
@@ -27,7 +27,7 @@
 #include <svl/style.hxx>
 
 #include <tools/debug.hxx>
-#include <tools/weakbase.hxx>
+#include <unotools/weakref.hxx>
 
 #include <strings.hrc>
 #include <stlfamily.hxx>
@@ -49,7 +49,7 @@ typedef std::map< OUString, rtl::Reference< SdStyleSheet > > 
PresStyleMap;
 
 struct SdStyleFamilyImpl
 {
-    tools::WeakReference<SdPage> mxMasterPage;
+    unotools::WeakReference<SdPage> mxMasterPage;
     OUString maLayoutName;
 
     PresStyleMap& getStyleSheets();
@@ -61,9 +61,13 @@ private:
 
 PresStyleMap& SdStyleFamilyImpl::getStyleSheets()
 {
-    if( mxMasterPage.is() && (mxMasterPage->GetLayoutName() != maLayoutName) )
+    auto pMasterPage = mxMasterPage.get();
+    if (!pMasterPage)
+        return maStyleSheets;
+
+    if (pMasterPage->GetLayoutName() != maLayoutName )
     {
-        maLayoutName = mxMasterPage->GetLayoutName();
+        maLayoutName = pMasterPage->GetLayoutName();
 
         OUString aLayoutName( maLayoutName );
         const sal_Int32 nLen = aLayoutName.indexOf(SD_LT_SEPARATOR ) + 4;
@@ -102,7 +106,7 @@ SdStyleFamily::SdStyleFamily( const rtl::Reference< 
SfxStyleSheetPool >& xPool,
 , mxPool( xPool )
 , mpImpl( new SdStyleFamilyImpl )
 {
-    mpImpl->mxMasterPage.reset( const_cast< SdPage* >( pMasterPage ) );
+    mpImpl->mxMasterPage = const_cast< SdPage* >( pMasterPage );
     mpImpl->mxPool = xPool;
 }
 
@@ -183,7 +187,7 @@ OUString SAL_CALL SdStyleFamily::getName()
 {
     if( mnFamily == SfxStyleFamily::Page )
     {
-        SdPage* pPage = mpImpl->mxMasterPage.get();
+        rtl::Reference<SdPage> pPage = mpImpl->mxMasterPage.get();
         if( pPage == nullptr )
             throw DisposedException();
 
diff --git a/sd/source/core/undo/undoobjects.cxx 
b/sd/source/core/undo/undoobjects.cxx
index b00fc68aa8d9..f586dac21f64 100644
--- a/sd/source/core/undo/undoobjects.cxx
+++ b/sd/source/core/undo/undoobjects.cxx
@@ -257,17 +257,18 @@ 
UndoObjectPresentationKind::UndoObjectPresentationKind(SdrObject& rObject)
 ,   mxPage( static_cast<SdPage*>(rObject.getSdrPageFromSdrObject()) )
 ,   mxSdrObject( &rObject )
 {
-    DBG_ASSERT( mxPage.is(), 
"sd::UndoObjectPresentationKind::UndoObjectPresentationKind(), does not work 
for shapes without a slide!" );
+    DBG_ASSERT( mxPage.get(), 
"sd::UndoObjectPresentationKind::UndoObjectPresentationKind(), does not work 
for shapes without a slide!" );
 
-    if( mxPage.is() )
-        meOldKind = mxPage->GetPresObjKind( &rObject );
+    if( auto pPage = mxPage.get() )
+        meOldKind = pPage->GetPresObjKind( &rObject );
 }
 
 void UndoObjectPresentationKind::Undo()
 {
-    if( mxPage.is() && mxSdrObject.is() )
+    if( !mxSdrObject.is() )
+        return;
+    if( rtl::Reference<SdPage> pPage = mxPage.get() )
     {
-        SdPage* pPage = mxPage.get();
         meNewKind =  pPage->GetPresObjKind( mxSdrObject.get() );
         if( meNewKind != PresObjKind::NONE )
             pPage->RemovePresObj( mxSdrObject.get() );
@@ -278,9 +279,10 @@ void UndoObjectPresentationKind::Undo()
 
 void UndoObjectPresentationKind::Redo()
 {
-    if( mxPage.is() && mxSdrObject.is() )
+    if( !mxSdrObject.is() )
+        return;
+    if( rtl::Reference<SdPage> pPage = mxPage.get() )
     {
-        SdPage* pPage = mxPage.get();
         if( meOldKind != PresObjKind::NONE )
             pPage->RemovePresObj( mxSdrObject.get() );
         if( meNewKind != PresObjKind::NONE )
@@ -300,7 +302,7 @@ void UndoAutoLayoutPosAndSize::Undo()
 
 void UndoAutoLayoutPosAndSize::Redo()
 {
-    SdPage* pPage = mxPage.get();
+    rtl::Reference<SdPage> pPage = mxPage.get();
     if( pPage )
         pPage->SetAutoLayout( pPage->GetAutoLayout() );
 }
@@ -317,9 +319,9 @@ void UndoGeoObject::Undo()
     DBG_ASSERT( mxSdrObject.is(), "sd::UndoGeoObject::Undo(), object already 
dead!" );
     if( mxSdrObject.is() )
     {
-        if( mxPage.is() )
+        if( auto pPage = mxPage.get() )
         {
-            ScopeLockGuard aGuard( mxPage->maLockAutoLayoutArrangement );
+            ScopeLockGuard aGuard( pPage->maLockAutoLayoutArrangement );
             SdrUndoGeoObj::Undo();
         }
         else
@@ -334,9 +336,9 @@ void UndoGeoObject::Redo()
     DBG_ASSERT( mxSdrObject.is(), "sd::UndoGeoObject::Redo(), object already 
dead!" );
     if( mxSdrObject.is() )
     {
-        if( mxPage.is() )
+        if( auto pPage = mxPage.get() )
         {
-            ScopeLockGuard aGuard( mxPage->maLockAutoLayoutArrangement );
+            ScopeLockGuard aGuard( pPage->maLockAutoLayoutArrangement );
             SdrUndoGeoObj::Redo();
         }
         else
@@ -358,9 +360,9 @@ void UndoAttrObject::Undo()
     DBG_ASSERT( mxSdrObject.is(), "sd::UndoAttrObject::Undo(), object already 
dead!" );
     if( mxSdrObject.is() )
     {
-        if( mxPage.is() )
+        if( auto pPage = mxPage.get() )
         {
-            ScopeLockGuard aGuard( mxPage->maLockAutoLayoutArrangement );
+            ScopeLockGuard aGuard( pPage->maLockAutoLayoutArrangement );
             SdrUndoAttrObj::Undo();
         }
         else
@@ -375,9 +377,9 @@ void UndoAttrObject::Redo()
     DBG_ASSERT( mxSdrObject.is(), "sd::UndoAttrObject::Redo(), object already 
dead!" );
     if( mxSdrObject.is() )
     {
-        if( mxPage.is() )
+        if( auto pPage = mxPage.get() )
         {
-            ScopeLockGuard aGuard( mxPage->maLockAutoLayoutArrangement );
+            ScopeLockGuard aGuard( pPage->maLockAutoLayoutArrangement );
             SdrUndoAttrObj::Redo();
         }
         else
diff --git a/sd/source/filter/ppt/pptin.cxx b/sd/source/filter/ppt/pptin.cxx
index 55e0f4bd2962..e4bd9b24bb42 100644
--- a/sd/source/filter/ppt/pptin.cxx
+++ b/sd/source/filter/ppt/pptin.cxx
@@ -595,7 +595,7 @@ bool ImplSdPPTImport::Import()
         for ( sal_uInt16 nMasterNum = 0; nMasterNum < nMasterCnt; nMasterNum++ 
)
         {
             SetPageNum( nMasterNum, PPT_MASTERPAGE );
-            SdPage* pPage = static_cast<SdPage*>(MakeBlankPage( true ));
+            rtl::Reference<SdPage> pPage = static_cast<SdPage*>(MakeBlankPage( 
true ).get());
             if ( pPage )
             {
                 bool bNotesMaster = (*GetPageList( m_eCurrentPageKind ) )[ 
m_nCurrentPageNum ].bNotesMaster;
@@ -607,7 +607,7 @@ bool ImplSdPPTImport::Import()
                     ePgKind = PageKind::Handout;
 
                 pPage->SetPageKind( ePgKind );
-                pSdrModel->InsertMasterPage( static_cast<SdrPage*>(pPage) );
+                pSdrModel->InsertMasterPage( pPage.get() );
                 if ( bNotesMaster && bStarDrawFiller )
                     pPage->SetAutoLayout( AUTOLAYOUT_NOTES, true );
                 if ( nMasterNum )
@@ -754,13 +754,14 @@ bool ImplSdPPTImport::Import()
             if ( pPersist->bStarDrawFiller && pPersist->bNotesMaster && ( 
m_nCurrentPageNum > 2 ) && ( ( m_nCurrentPageNum & 1 ) == 0 ) )
             {
                 pSdrModel->DeleteMasterPage( m_nCurrentPageNum );
-                SdrPage* pNotesClone = 
static_cast<SdPage*>(pSdrModel->GetMasterPage( 2 ))->CloneSdrPage(*pSdrModel);
-                pSdrModel->InsertMasterPage( pNotesClone, m_nCurrentPageNum );
+                SdPage* pMasterPage2 = 
static_cast<SdPage*>(pSdrModel->GetMasterPage( 2 ));
+                rtl::Reference<SdPage> pNotesClone = 
static_cast<SdPage*>(pMasterPage2->CloneSdrPage(*pSdrModel).get());
+                pSdrModel->InsertMasterPage( pNotesClone.get(), 
m_nCurrentPageNum );
                 if ( pNotesClone )
                 {
                     OUString aLayoutName( 
static_cast<SdPage*>(pSdrModel->GetMasterPage( m_nCurrentPageNum - 1 
))->GetLayoutName() );
-                    static_cast<SdPage*>(pNotesClone)->SetPresentationLayout( 
aLayoutName, false, false );
-                    static_cast<SdPage*>(pNotesClone)->SetLayoutName( 
aLayoutName );
+                    pNotesClone->SetPresentationLayout( aLayoutName, false, 
false );
+                    pNotesClone->SetLayoutName( aLayoutName );
                 }
             }
             else if ( !pPersist->bStarDrawFiller )
@@ -923,9 +924,9 @@ bool ImplSdPPTImport::Import()
         PptPageKind     ePageKind = m_eCurrentPageKind;
         sal_uInt16          nPageNum = m_nCurrentPageNum;
 
-        SdPage* pHandoutPage = static_cast<SdPage*>(MakeBlankPage( false ));
+        rtl::Reference<SdPage> pHandoutPage = 
static_cast<SdPage*>(MakeBlankPage( false ).get());
         pHandoutPage->SetPageKind( PageKind::Handout );
-        pSdrModel->InsertPage( pHandoutPage );
+        pSdrModel->InsertPage( pHandoutPage.get() );
 
         sal_uInt16 nPageCnt = GetPageCount();
         if ( nPageCnt )
@@ -934,7 +935,7 @@ bool ImplSdPPTImport::Import()
             {
                 mePresChange = PresChange::SemiAuto;
                 SetPageNum( nPage );
-                SdPage* pPage = static_cast<SdPage*>(MakeBlankPage( false ));
+                rtl::Reference<SdPage> pPage = 
static_cast<SdPage*>(MakeBlankPage( false ).get());
                 PptSlidePersistEntry* pMasterPersist = nullptr;
                 if ( HasMasterPage( nPage ) )     // try to get the LayoutName 
from the masterpage
                 {
@@ -946,9 +947,9 @@ bool ImplSdPPTImport::Import()
                     
pPage->SetLayoutName(static_cast<SdPage&>(pPage->TRG_GetMasterPage()).GetLayoutName());
                 }
                 pPage->SetPageKind( PageKind::Standard );
-                pSdrModel->InsertPage( pPage );         // SJ: #i29625# 
because of form controls, the
-                ImportPage( pPage, pMasterPersist );    //  page must be 
inserted before importing
-                SetHeaderFooterPageSettings( pPage, pMasterPersist );
+                pSdrModel->InsertPage( pPage.get() );         // SJ: #i29625# 
because of form controls, the
+                ImportPage( pPage.get(), pMasterPersist );    //  page must be 
inserted before importing
+                SetHeaderFooterPageSettings( pPage.get(), pMasterPersist );
                 // CWS preseng01: pPage->SetPageKind( PageKind::Standard );
 
                 DffRecordHeader aPageHd;
@@ -1006,12 +1007,12 @@ bool ImplSdPPTImport::Import()
                         if (!aHd.SeekToEndOfRecord(rStCtrl))
                             break;
                     }
-                    ImportPageEffect( pPage, bNewAnimationsUsed );
+                    ImportPageEffect( pPage.get(), bNewAnimationsUsed );
                 }
 
                 // creating the corresponding note page
                 m_eCurrentPageKind = PPT_NOTEPAGE;
-                SdPage* pNotesPage = static_cast<SdPage*>(MakeBlankPage( false 
));
+                rtl::Reference<SdPage> pNotesPage = 
static_cast<SdPage*>(MakeBlankPage( false ).get());
                 sal_uInt16 nNotesMasterNum = GetMasterPageIndex( nPage ) + 1;
                 sal_uInt32 nNotesPageId = GetNotesPageId( nPage );
                 if ( nNotesPageId )
@@ -1032,9 +1033,9 @@ bool ImplSdPPTImport::Import()
                     }
                     pNotesPage->SetPageKind( PageKind::Notes );
                     
pNotesPage->TRG_SetMasterPage(*pSdrModel->GetMasterPage(nNotesMasterNum));
-                    pSdrModel->InsertPage( pNotesPage );        // SJ: 
#i29625# because of form controls, the
-                    ImportPage( pNotesPage, pMasterPersist2 );  // page must 
be inserted before importing
-                    SetHeaderFooterPageSettings( pNotesPage, pMasterPersist2 );
+                    pSdrModel->InsertPage( pNotesPage.get() );        // SJ: 
#i29625# because of form controls, the
+                    ImportPage( pNotesPage.get(), pMasterPersist2 );  // page 
must be inserted before importing
+                    SetHeaderFooterPageSettings( pNotesPage.get(), 
pMasterPersist2 );
                     pNotesPage->SetAutoLayout( AUTOLAYOUT_NOTES );
                 }
                 else
@@ -1042,7 +1043,7 @@ bool ImplSdPPTImport::Import()
                     pNotesPage->SetPageKind( PageKind::Notes );
                     
pNotesPage->TRG_SetMasterPage(*pSdrModel->GetMasterPage(nNotesMasterNum));
                     pNotesPage->SetAutoLayout( AUTOLAYOUT_NOTES, true );
-                    pSdrModel->InsertPage( pNotesPage );
+                    pSdrModel->InsertPage( pNotesPage.get() );
                     SdrObject* pPageObj = pNotesPage->GetPresObj( 
PresObjKind::Page );
                     if ( pPageObj )
                         
static_cast<SdrPageObj*>(pPageObj)->SetReferencedPage(pSdrModel->GetPage(( 
nPage << 1 ) + 1));
@@ -1056,8 +1057,8 @@ bool ImplSdPPTImport::Import()
         {
             // that can happen by document templates
             m_eCurrentPageKind = PPT_SLIDEPAGE;
-            SdrPage* pPage = MakeBlankPage( false );
-            pSdrModel->InsertPage( pPage );
+            rtl::Reference<SdPage> pPage = static_cast<SdPage*>(MakeBlankPage( 
false ).get());
+            pSdrModel->InsertPage( pPage.get() );
 
             // #i37397#, trying to set the title master for the first page
             sal_uInt16 nMaster, nMasterCount = pSdrModel->GetMasterPageCount();
@@ -1078,14 +1079,14 @@ bool ImplSdPPTImport::Import()
             }
             if ( pFoundMaster )
             {
-                static_cast<SdPage*>(pPage)->TRG_SetMasterPage( *pFoundMaster 
);
-                static_cast<SdPage*>(pPage)->SetLayoutName( 
pFoundMaster->GetLayoutName() );
+                pPage->TRG_SetMasterPage( *pFoundMaster );
+                pPage->SetLayoutName( pFoundMaster->GetLayoutName() );
             }
-            static_cast<SdPage*>(pPage)->SetAutoLayout( AUTOLAYOUT_TITLE, 
true, true );
+            pPage->SetAutoLayout( AUTOLAYOUT_TITLE, true, true );
 
             m_eCurrentPageKind = PPT_NOTEPAGE;
-            SdrPage* pNPage = MakeBlankPage( false );
-            pSdrModel->InsertPage( pNPage );
+            rtl::Reference<SdrPage> pNPage = MakeBlankPage( false );
+            pSdrModel->InsertPage( pNPage.get() );
         }
         SetPageNum( nPageNum, ePageKind );
         rStCtrl.Seek( nOldFPos );
diff --git a/sd/source/ui/dlg/animobjs.cxx b/sd/source/ui/dlg/animobjs.cxx
index e900ffb45e88..1ef08386396b 100644
--- a/sd/source/ui/dlg/animobjs.cxx
+++ b/sd/source/ui/dlg/animobjs.cxx
@@ -158,8 +158,8 @@ AnimationWindow::AnimationWindow(SfxBindings* pInBindings, 
SfxChildWindow *pCW,
 
     // create new document with page
     pMyDoc.reset( new SdDrawDocument(DocumentType::Impress, nullptr) );
-    SdPage* pPage = pMyDoc->AllocSdPage(false);
-    pMyDoc->InsertPage(pPage);
+    rtl::Reference<SdPage> pPage = pMyDoc->AllocSdPage(false);
+    pMyDoc->InsertPage(pPage.get());
 
     pControllerItem.reset( new AnimationControllerItem( SID_ANIMATOR_STATE, 
this, pInBindings ) );
 
diff --git a/sd/source/ui/func/fuexpand.cxx b/sd/source/ui/func/fuexpand.cxx
index b95ad8515b67..ebaa0ee5d944 100644
--- a/sd/source/ui/func/fuexpand.cxx
+++ b/sd/source/ui/func/fuexpand.cxx
@@ -133,7 +133,7 @@ void FuExpandPage::DoExecute( SfxRequest& )
             if ( nDepth == 0 )
             {
                 // page with title & structuring!
-                SdPage* pPage = mpDoc->AllocSdPage(false);
+                rtl::Reference<SdPage> pPage = mpDoc->AllocSdPage(false);
                 pPage->SetSize(pActualPage->GetSize() );
                 pPage->SetBorder(pActualPage->GetLeftBorder(),
                                  pActualPage->GetUpperBorder(),
@@ -142,7 +142,7 @@ void FuExpandPage::DoExecute( SfxRequest& )
                 pPage->SetName(OUString());
 
                 // insert page after current page
-                mpDoc->InsertPage(pPage, nActualPageNum + nPos);
+                mpDoc->InsertPage(pPage.get(), nActualPageNum + nPos);
                 nPos++;
 
                 if( bUndo )
@@ -155,7 +155,7 @@ void FuExpandPage::DoExecute( SfxRequest& )
                 pPage->TRG_SetMasterPageVisibleLayers(aVisibleLayers);
 
                 // notes-page
-                SdPage* pNotesPage = mpDoc->AllocSdPage(false);
+                rtl::Reference<SdPage> pNotesPage = mpDoc->AllocSdPage(false);
                 pNotesPage->SetSize(pActualNotesPage->GetSize());
                 pNotesPage->SetBorder(pActualNotesPage->GetLeftBorder(),
                                       pActualNotesPage->GetUpperBorder(),
@@ -165,7 +165,7 @@ void FuExpandPage::DoExecute( SfxRequest& )
                 pNotesPage->SetName(OUString());
 
                 // insert page after current page
-                mpDoc->InsertPage(pNotesPage, nActualPageNum + nPos);
+                mpDoc->InsertPage(pNotesPage.get(), nActualPageNum + nPos);
                 nPos++;
 
                 if( bUndo )
diff --git a/sd/source/ui/func/fusumry.cxx b/sd/source/ui/func/fusumry.cxx
index 5984ffa19d44..5e97176ccc18 100644
--- a/sd/source/ui/func/fusumry.cxx
+++ b/sd/source/ui/func/fusumry.cxx
@@ -63,7 +63,7 @@ rtl::Reference<FuPoor> FuSummaryPage::Create( ViewShell* 
pViewSh, ::sd::Window*
 void FuSummaryPage::DoExecute( SfxRequest& )
 {
     std::unique_ptr<SdOutliner> pOutl;
-    SdPage* pSummaryPage = nullptr;
+    rtl::Reference<SdPage> pSummaryPage;
     sal_uInt16 i = 0;
     sal_uInt16 nFirstPage = SDRPAGE_NOTFOUND;
     sal_uInt16 nSelectedPages = 0;
@@ -126,7 +126,7 @@ void FuSummaryPage::DoExecute( SfxRequest& )
                                      pActualPage->GetLowerBorder() );
 
                     // insert page at the back
-                    mpDoc->InsertPage(pSummaryPage, nCount * 2 + 1);
+                    mpDoc->InsertPage(pSummaryPage.get(), nCount * 2 + 1);
                     if( bUndo )
                         
mpView->AddUndo(mpDoc->GetSdrUndoFactory().CreateUndoNewPage(*pSummaryPage));
 
@@ -138,7 +138,7 @@ void FuSummaryPage::DoExecute( SfxRequest& )
                     
pSummaryPage->setHeaderFooterSettings(pActualPage->getHeaderFooterSettings());
 
                     // notes-page
-                    SdPage* pNotesPage = mpDoc->AllocSdPage(false);
+                    rtl::Reference<SdPage> pNotesPage = 
mpDoc->AllocSdPage(false);
                     pNotesPage->SetSize(pActualNotesPage->GetSize());
                     pNotesPage->SetBorder(pActualNotesPage->GetLeftBorder(),
                                           pActualNotesPage->GetUpperBorder(),
@@ -147,7 +147,7 @@ void FuSummaryPage::DoExecute( SfxRequest& )
                     pNotesPage->SetPageKind(PageKind::Notes);
 
                     // insert page at the back
-                    mpDoc->InsertPage(pNotesPage, nCount * 2 + 2);
+                    mpDoc->InsertPage(pNotesPage.get(), nCount * 2 + 2);
 
                     if( bUndo )
                         
mpView->AddUndo(mpDoc->GetSdrUndoFactory().CreateUndoNewPage(*pNotesPage));
diff --git a/sd/source/ui/inc/DrawController.hxx 
b/sd/source/ui/inc/DrawController.hxx
index 71d61908152b..5adf9fa50206 100644
--- a/sd/source/ui/inc/DrawController.hxx
+++ b/sd/source/ui/inc/DrawController.hxx
@@ -30,7 +30,7 @@
 #include <com/sun/star/lang/XUnoTunnel.hpp>
 #include <comphelper/uno3.hxx>
 #include <cppuhelper/implbase.hxx>
-#include <tools/weakbase.h>
+#include <unotools/weakref.hxx>
 #include <tools/gen.hxx>
 #include <memory>
 #include <vector>
@@ -289,7 +289,7 @@ private:
     ViewShellBase* mpBase;
 
     ::tools::Rectangle maLastVisArea;
-    ::tools::WeakReference<SdPage> mpCurrentPage;
+    ::unotools::WeakReference<SdPage> mpCurrentPage;
     bool mbMasterPageMode;
     bool mbLayerMode;
 
diff --git a/sd/source/ui/sidebar/DocumentHelper.cxx 
b/sd/source/ui/sidebar/DocumentHelper.cxx
index c3a848712f51..00c0288689f9 100644
--- a/sd/source/ui/sidebar/DocumentHelper.cxx
+++ b/sd/source/ui/sidebar/DocumentHelper.cxx
@@ -202,25 +202,25 @@ SdPage* DocumentHelper::AddMasterPage (
     SdDrawDocument& rTargetDocument,
     SdPage const * pMasterPage)
 {
-    SdPage* pClonedMasterPage = nullptr;
+    rtl::Reference<SdPage> pClonedMasterPage;
 
     if (pMasterPage!=nullptr)
     {
         try
         {
             // Duplicate the master page.
-            pClonedMasterPage = 
static_cast<SdPage*>(pMasterPage->CloneSdrPage(rTargetDocument));
+            pClonedMasterPage = 
static_cast<SdPage*>(pMasterPage->CloneSdrPage(rTargetDocument).get());
 
             // Copy the necessary styles.
             SdDrawDocument& rSourceDocument(static_cast< SdDrawDocument& 
>(pMasterPage->getSdrModelFromSdrPage()));
-            ProvideStyles(rSourceDocument, rTargetDocument, pClonedMasterPage);
+            ProvideStyles(rSourceDocument, rTargetDocument, 
pClonedMasterPage.get());
 
             // Copy the precious flag.
             pClonedMasterPage->SetPrecious(pMasterPage->IsPrecious());
 
             // Now that the styles are available we can insert the cloned
             // master page.
-            rTargetDocument.InsertMasterPage (pClonedMasterPage);
+            rTargetDocument.InsertMasterPage (pClonedMasterPage.get());
         }
         catch(const uno::Exception&)
         {
@@ -239,7 +239,7 @@ SdPage* DocumentHelper::AddMasterPage (
         }
     }
 
-    return pClonedMasterPage;
+    return pClonedMasterPage.get();
 }
 
 void DocumentHelper::ProvideStyles (
@@ -341,23 +341,23 @@ SdPage* DocumentHelper::AddMasterPage (
     SdPage const * pMasterPage,
     sal_uInt16 nInsertionIndex)
 {
-    SdPage* pClonedMasterPage = nullptr;
+    rtl::Reference<SdPage> pClonedMasterPage;
 
     if (pMasterPage!=nullptr)
     {
         // Duplicate the master page.
-        pClonedMasterPage = 
static_cast<SdPage*>(pMasterPage->CloneSdrPage(rTargetDocument));
+        pClonedMasterPage = 
static_cast<SdPage*>(pMasterPage->CloneSdrPage(rTargetDocument).get());
 
         // Copy the precious flag.
         pClonedMasterPage->SetPrecious(pMasterPage->IsPrecious());
 
         // Copy the necessary styles.
         SdDrawDocument& rSourceDocument(static_cast< SdDrawDocument& 
>(pMasterPage->getSdrModelFromSdrPage()));
-        ProvideStyles(rSourceDocument, rTargetDocument, pClonedMasterPage);
+        ProvideStyles(rSourceDocument, rTargetDocument, 
pClonedMasterPage.get());
 
         // Now that the styles are available we can insert the cloned
         // master page.
-        rTargetDocument.InsertMasterPage (pClonedMasterPage, nInsertionIndex);
+        rTargetDocument.InsertMasterPage (pClonedMasterPage.get(), 
nInsertionIndex);
 
         // Adapt the size of the new master page to that of the pages in
         // the document.
@@ -372,7 +372,7 @@ SdPage* DocumentHelper::AddMasterPage (
         pClonedMasterPage->CreateTitleAndLayout(true);
     }
 
-    return pClonedMasterPage;
+    return pClonedMasterPage.get();
 }
 
 /** In here we have to handle three cases:
diff --git a/sd/source/ui/unoidl/DrawController.cxx 
b/sd/source/ui/unoidl/DrawController.cxx
index a82460b7fb7c..4f61f2cf1afa 100644
--- a/sd/source/ui/unoidl/DrawController.cxx
+++ b/sd/source/ui/unoidl/DrawController.cxx
@@ -318,8 +318,9 @@ Reference< drawing::XDrawPage > SAL_CALL 
DrawController::getCurrentPage()
 
     // When there is not yet a sub controller (during initialization) then 
fall back
     // to the current page in mpCurrentPage.
-    if ( ! xPage.is() && mpCurrentPage.is())
-        xPage.set(mpCurrentPage->getUnoPage(), UNO_QUERY);
+    if ( ! xPage.is() )
+        if (rtl::Reference<SdPage> pPage = mpCurrentPage.get())
+            xPage.set(pPage->getUnoPage(), UNO_QUERY);
 
     return xPage;
 }
@@ -403,8 +404,8 @@ void DrawController::FireChangeLayerMode (bool bLayerMode) 
throw()
 
 void DrawController::FireSwitchCurrentPage (SdPage* pNewCurrentPage) throw()
 {
-    SdrPage* pCurrentPage  = mpCurrentPage.get();
-    if (pNewCurrentPage == pCurrentPage)
+    rtl::Reference<SdrPage> pCurrentPage  = mpCurrentPage.get();
+    if (pNewCurrentPage == pCurrentPage.get())
         return;
 
     try
@@ -421,7 +422,7 @@ void DrawController::FireSwitchCurrentPage (SdPage* 
pNewCurrentPage) throw()
 
         FirePropertyChange(PROPERTY_CURRENTPAGE, aNewValue, aOldValue);
 
-        mpCurrentPage.reset(pNewCurrentPage);
+        mpCurrentPage = pNewCurrentPage;
     }
     catch (const uno::Exception&)
     {
diff --git a/sd/source/ui/unoidl/unomodel.cxx b/sd/source/ui/unoidl/unomodel.cxx
index 6051e07fe8f3..411af888513f 100644
--- a/sd/source/ui/unoidl/unomodel.cxx
+++ b/sd/source/ui/unoidl/unomodel.cxx
@@ -453,7 +453,7 @@ SdPage* SdXImpressDocument::InsertSdPage( sal_uInt16 nPage, 
bool bDuplicate )
     SdrLayerID aBckgrnd = rLayerAdmin.GetLayerID(sUNO_LayerName_background);
     SdrLayerID aBckgrndObj = 
rLayerAdmin.GetLayerID(sUNO_LayerName_background_objects);
 
-    SdPage* pStandardPage = nullptr;
+    rtl::Reference<SdPage> pStandardPage;
 
     if( 0 == nPageCount )
     {
@@ -462,7 +462,7 @@ SdPage* SdXImpressDocument::InsertSdPage( sal_uInt16 nPage, 
bool bDuplicate )
 
         Size aDefSize(21000, 29700);   // A4 portrait orientation
         pStandardPage->SetSize( aDefSize );
-        mpDoc->InsertPage(pStandardPage, 0);
+        mpDoc->InsertPage(pStandardPage.get(), 0);
     }
     else
     {
@@ -487,7 +487,7 @@ SdPage* SdXImpressDocument::InsertSdPage( sal_uInt16 nPage, 
bool bDuplicate )
         * standard page
         **************************************************************/
         if( bDuplicate )
-            pStandardPage = static_cast<SdPage*>( 
pPreviousStandardPage->CloneSdrPage(*mpDoc) );
+            pStandardPage = static_cast<SdPage*>( 
pPreviousStandardPage->CloneSdrPage(*mpDoc).get() );
         else
             pStandardPage = mpDoc->AllocSdPage(false);
 
@@ -500,7 +500,7 @@ SdPage* SdXImpressDocument::InsertSdPage( sal_uInt16 nPage, 
bool bDuplicate )
         pStandardPage->SetName(OUString());
 
         // insert page after current page
-        mpDoc->InsertPage(pStandardPage, nStandardPageNum);
+        mpDoc->InsertPage(pStandardPage.get(), nStandardPageNum);
 
         if( !bDuplicate )
         {
@@ -519,10 +519,10 @@ SdPage* SdXImpressDocument::InsertSdPage( sal_uInt16 
nPage, bool bDuplicate )
         /**************************************************************
         * notes page
         **************************************************************/
-        SdPage* pNotesPage = nullptr;
+        rtl::Reference<SdPage> pNotesPage;
 
         if( bDuplicate )
-            pNotesPage = static_cast<SdPage*>( 
pPreviousNotesPage->CloneSdrPage(*mpDoc) );
+            pNotesPage = static_cast<SdPage*>( 
pPreviousNotesPage->CloneSdrPage(*mpDoc).get() );
         else
             pNotesPage = mpDoc->AllocSdPage(false);
 
@@ -536,7 +536,7 @@ SdPage* SdXImpressDocument::InsertSdPage( sal_uInt16 nPage, 
bool bDuplicate )
         pNotesPage->SetPageKind(PageKind::Notes);
 
         // insert page after current page
-        mpDoc->InsertPage(pNotesPage, nNotesPageNum);
+        mpDoc->InsertPage(pNotesPage.get(), nNotesPageNum);
 
         if( !bDuplicate )
         {
@@ -549,7 +549,7 @@ SdPage* SdXImpressDocument::InsertSdPage( sal_uInt16 nPage, 
bool bDuplicate )
 
     SetModified();
 
-    return pStandardPage;
+    return pStandardPage.get();
 }
 
 void SdXImpressDocument::SetModified() throw()
@@ -2962,11 +2962,6 @@ void SAL_CALL SdDrawPagesAccess::remove( const 
uno::Reference< drawing::XDrawPag
                 {
                     rDoc.EndUndo();
                 }
-                else
-                {
-                    delete pNotesPage;
-                    delete pPage;
-                }
             }
         }
     }
@@ -3135,14 +3130,14 @@ uno::Reference< drawing::XDrawPage > SAL_CALL 
SdMasterPagesAccess::insertNewByIn
         SdPage* pRefNotesPage = mpModel->mpDoc->GetSdPage( sal_uInt16(0), 
PageKind::Notes);
 
         // create and insert new draw masterpage
-        SdPage* pMPage = mpModel->mpDoc->AllocSdPage(true);
+        rtl::Reference<SdPage> pMPage = mpModel->mpDoc->AllocSdPage(true);
         pMPage->SetSize( pPage->GetSize() );
         pMPage->SetBorder( pPage->GetLeftBorder(),
                            pPage->GetUpperBorder(),
                            pPage->GetRightBorder(),
                            pPage->GetLowerBorder() );
         pMPage->SetLayoutName( aLayoutName );
-        pDoc->InsertMasterPage(pMPage,  static_cast<sal_uInt16>(nInsertPos));
+        pDoc->InsertMasterPage(pMPage.get(),  
static_cast<sal_uInt16>(nInsertPos));
 
         {
             // ensure default MasterPage fill
@@ -3152,7 +3147,7 @@ uno::Reference< drawing::XDrawPage > SAL_CALL 
SdMasterPagesAccess::insertNewByIn
         xDrawPage.set( pMPage->getUnoPage(), uno::UNO_QUERY );
 
         // create and insert new notes masterpage
-        SdPage* pMNotesPage = mpModel->mpDoc->AllocSdPage(true);
+        rtl::Reference<SdPage> pMNotesPage = mpModel->mpDoc->AllocSdPage(true);
         pMNotesPage->SetSize( pRefNotesPage->GetSize() );
         pMNotesPage->SetPageKind(PageKind::Notes);
         pMNotesPage->SetBorder( pRefNotesPage->GetLeftBorder(),
@@ -3160,7 +3155,7 @@ uno::Reference< drawing::XDrawPage > SAL_CALL 
SdMasterPagesAccess::insertNewByIn
                                 pRefNotesPage->GetRightBorder(),
                                 pRefNotesPage->GetLowerBorder() );
         pMNotesPage->SetLayoutName( aLayoutName );
-        pDoc->InsertMasterPage(pMNotesPage,  
static_cast<sal_uInt16>(nInsertPos) + 1);
+        pDoc->InsertMasterPage(pMNotesPage.get(),  
static_cast<sal_uInt16>(nInsertPos) + 1);
         pMNotesPage->SetAutoLayout(AUTOLAYOUT_NOTES, true, true);
         mpModel->SetModified();
     }
@@ -3219,11 +3214,6 @@ void SAL_CALL SdMasterPagesAccess::remove( const 
uno::Reference< drawing::XDrawP
     {
         rDoc.EndUndo();
     }
-    else
-    {
-        delete pNotesPage;
-        delete pPage;
-    }
 }
 
 // XServiceInfo
diff --git a/sd/source/ui/view/outlview.cxx b/sd/source/ui/view/outlview.cxx
index 8abad53cccc6..9c0c16ed9462 100644
--- a/sd/source/ui/view/outlview.cxx
+++ b/sd/source/ui/view/outlview.cxx
@@ -390,12 +390,12 @@ SdPage* OutlineView::InsertSlideForParagraph( Paragraph* 
pPara )
 
     // this page is exemplary
     SdPage* pExample = mrDoc.GetSdPage(static_cast<sal_uInt16>(nExample), 
PageKind::Standard);
-    SdPage* pPage = mrDoc.AllocSdPage(false);
+    rtl::Reference<SdPage> pPage = mrDoc.AllocSdPage(false);
 
     pPage->SetLayoutName(pExample->GetLayoutName());
 
     // insert (page)
-    mrDoc.InsertPage(pPage, static_cast<sal_uInt16>(nTarget) * 2 + 1);
+    mrDoc.InsertPage(pPage.get(), static_cast<sal_uInt16>(nTarget) * 2 + 1);
     if( isRecordingUndo() )
         AddUndo(mrDoc.GetSdrUndoFactory().CreateUndoNewPage(*pPage));
 
@@ -427,14 +427,14 @@ SdPage* OutlineView::InsertSlideForParagraph( Paragraph* 
pPara )
     |* now the notes page
     \*********************************************************************/
     pExample = mrDoc.GetSdPage(static_cast<sal_uInt16>(nExample), 
PageKind::Notes);
-    SdPage* pNotesPage = mrDoc.AllocSdPage(false);
+    rtl::Reference<SdPage> pNotesPage = mrDoc.AllocSdPage(false);
 
     pNotesPage->SetLayoutName(pExample->GetLayoutName());
 
     pNotesPage->SetPageKind(PageKind::Notes);
 
     // insert (notes page)
-    mrDoc.InsertPage(pNotesPage, static_cast<sal_uInt16>(nTarget) * 2 + 2);
+    mrDoc.InsertPage(pNotesPage.get(), static_cast<sal_uInt16>(nTarget) * 2 + 
2);
     if( isRecordingUndo() )
         AddUndo(mrDoc.GetSdrUndoFactory().CreateUndoNewPage(*pNotesPage));
 
@@ -453,7 +453,7 @@ SdPage* OutlineView::InsertSlideForParagraph( Paragraph* 
pPara )
 
     mrOutliner.UpdateFields();
 
-    return pPage;
+    return pPage.get();
 }
 
 /**
diff --git a/sd/source/ui/view/sdview3.cxx b/sd/source/ui/view/sdview3.cxx
index d334cb7a2254..90a1f599d2fe 100644
--- a/sd/source/ui/view/sdview3.cxx
+++ b/sd/source/ui/view/sdview3.cxx
@@ -677,7 +677,7 @@ bool View::InsertData( const TransferableDataHelper& 
rDataHelper,
             xShell->DoInitNew();
 
             SdDrawDocument* pModel = xShell->GetDoc();
-            pModel->InsertPage(pModel->AllocPage(false));
+            pModel->InsertPage(pModel->AllocPage(false).get());
 
             Reference< XComponent > xComponent = xShell->GetModel();
             xStm->Seek( 0 );
@@ -1530,7 +1530,7 @@ bool View::PasteRTFTable( const 
::tools::SvRef<SotTempStream>& xStm, SdrPage* pP
     std::unique_ptr<SdDrawDocument> pModel(new SdDrawDocument( 
DocumentType::Impress, mpDocSh ));
     pModel->NewOrLoadCompleted(DocCreationMode::New);
     pModel->GetItemPool().SetDefaultMetric(MapUnit::Map100thMM);
-    pModel->InsertPage(pModel->AllocPage(false));
+    pModel->InsertPage(pModel->AllocPage(false).get());
 
     Reference< XComponent > xComponent( new SdXImpressDocument( pModel.get(), 
true ) );
     pModel->setUnoModel( Reference< XInterface >::query( xComponent ) );
diff --git a/svx/inc/sdr/contact/objectcontactofobjlistpainter.hxx 
b/svx/inc/sdr/contact/objectcontactofobjlistpainter.hxx
index 724855d8be3e..3f5ebd768940 100644
--- a/svx/inc/sdr/contact/objectcontactofobjlistpainter.hxx
+++ b/svx/inc/sdr/contact/objectcontactofobjlistpainter.hxx
@@ -23,7 +23,7 @@
 #include <svx/sdr/contact/objectcontact.hxx>
 #include <svx/svxdllapi.h>
 #include <svx/svdpage.hxx>
-#include <tools/weakbase.hxx>
+#include <unotools/weakref.hxx>
 
 class SdrPage;
 class SdrObject;
@@ -87,7 +87,7 @@ class ObjectContactOfPagePainter : public ObjectContactPainter
     ObjectContact&                                  mrOriginalObjectContact;
 
     // Set StartPoint for next run, also given in constructor
-    tools::WeakReference<SdrPage>                   mxStartPage;
+    unotools::WeakReference<SdrPage>                mxStartPage;
 
 protected:
     // Hierarchy access methods
@@ -101,8 +101,8 @@ public:
 
     // set another page
     void SetStartPage(const SdrPage* pPage);
-    const SdrPage* GetStartPage() const { return mxStartPage.get(); }
-    SdrPage* GetStartPage() { return mxStartPage.get(); }
+    const SdrPage* GetStartPage() const { return mxStartPage.get().get(); }
+    SdrPage* GetStartPage() { return mxStartPage.get().get(); }
 
     virtual OutputDevice* TryToGetOutputDevice() const override;
 };
diff --git a/svx/source/dialog/connctrl.cxx b/svx/source/dialog/connctrl.cxx
index ecc0cc98f0cb..ec6e64b4d07d 100644
--- a/svx/source/dialog/connctrl.cxx
+++ b/svx/source/dialog/connctrl.cxx
@@ -62,13 +62,13 @@ void SvxXConnectionPreview::Resize()
 void SvxXConnectionPreview::AdaptSize()
 {
     // Adapt size
-    if( !pSdrPage )
+    if( !mxSdrPage )
         return;
 
     SetMapMode(MapMode(MapUnit::Map100thMM));
 
     OutputDevice* pOD = pView->GetFirstOutputDevice(); // GetWin( 0 );
-    tools::Rectangle aRect = pSdrPage->GetAllObjBoundRect();
+    tools::Rectangle aRect = mxSdrPage->GetAllObjBoundRect();
 
     MapMode aMapMode = GetMapMode();
     aMapMode.SetMapUnit( pOD->GetMapMode().GetMapUnit() );
@@ -146,15 +146,15 @@ void SvxXConnectionPreview::Construct()
 
                 // potential memory leak here (!). Create SdrObjList only when 
there is
                 // not yet one.
-                if(!pSdrPage)
+                if(!mxSdrPage)
                 {
-                    pSdrPage.reset( new SdrPage(
+                    mxSdrPage = new SdrPage(
                         pView->getSdrModelFromSdrView(),
-                        false) );
+                        false);
                 }
 
                 const SdrEdgeObj* pTmpEdgeObj = static_cast<const 
SdrEdgeObj*>(pObj);
-                pEdgeObj = 
pTmpEdgeObj->CloneSdrObject(pSdrPage->getSdrModelFromSdrPage());
+                pEdgeObj = 
pTmpEdgeObj->CloneSdrObject(mxSdrPage->getSdrModelFromSdrPage());
 
                 SdrObjConnection& rConn1 = pEdgeObj->GetConnection( true );
                 SdrObjConnection& rConn2 = pEdgeObj->GetConnection( false );
@@ -167,19 +167,19 @@ void SvxXConnectionPreview::Construct()
 
                 if( pTmpObj1 )
                 {
-                    SdrObject* pObj1 = 
pTmpObj1->CloneSdrObject(pSdrPage->getSdrModelFromSdrPage());
-                    pSdrPage->InsertObject( pObj1 );
+                    SdrObject* pObj1 = 
pTmpObj1->CloneSdrObject(mxSdrPage->getSdrModelFromSdrPage());
+                    mxSdrPage->InsertObject( pObj1 );
                     pEdgeObj->ConnectToNode( true, pObj1 );
                 }
 
                 if( pTmpObj2 )
                 {
-                    SdrObject* pObj2 = 
pTmpObj2->CloneSdrObject(pSdrPage->getSdrModelFromSdrPage());
-                    pSdrPage->InsertObject( pObj2 );
+                    SdrObject* pObj2 = 
pTmpObj2->CloneSdrObject(mxSdrPage->getSdrModelFromSdrPage());
+                    mxSdrPage->InsertObject( pObj2 );
                     pEdgeObj->ConnectToNode( false, pObj2 );
                 }
 
-                pSdrPage->InsertObject( pEdgeObj );
+                mxSdrPage->InsertObject( pEdgeObj );
             }
         }
     }
@@ -202,7 +202,7 @@ void SvxXConnectionPreview::Paint(vcl::RenderContext& 
rRenderContext, const tool
     rRenderContext.SetDrawMode(rStyles.GetHighContrastMode() ? 
OUTPUT_DRAWMODE_CONTRAST : OUTPUT_DRAWMODE_COLOR);
     rRenderContext.SetBackground(Wallpaper(rStyles.GetFieldColor()));
 
-    if (pSdrPage)
+    if (mxSdrPage)
     {
         // This will not work anymore. To not start at Adam and Eve, i will
         // ATM not try to change all this stuff to really using an own model
@@ -213,9 +213,9 @@ void SvxXConnectionPreview::Paint(vcl::RenderContext& 
rRenderContext, const tool
         // New stuff: Use an ObjectContactOfObjListPainter.
         sdr::contact::SdrObjectVector aObjectVector;
 
-        for (size_t a = 0; a < pSdrPage->GetObjCount(); ++a)
+        for (size_t a = 0; a < mxSdrPage->GetObjCount(); ++a)
         {
-            SdrObject* pObject = pSdrPage->GetObj(a);
+            SdrObject* pObject = mxSdrPage->GetObj(a);
             DBG_ASSERT(pObject,
                 "SvxXConnectionPreview::Paint: Corrupt ObjectList (!)");
             aObjectVector.push_back(pObject);
diff --git a/svx/source/dialog/dlgctl3d.cxx b/svx/source/dialog/dlgctl3d.cxx
index 7eb7e07f9f9a..ba8ac822fa62 100644
--- a/svx/source/dialog/dlgctl3d.cxx
+++ b/svx/source/dialog/dlgctl3d.cxx
@@ -45,8 +45,7 @@
 using namespace com::sun::star;
 
 Svx3DPreviewControl::Svx3DPreviewControl()
-    : mpFmPage(nullptr)
-    , mpScene(nullptr)
+    : mpScene(nullptr)
     , mp3DObj(nullptr)
     , mnObjectType(SvxPreviewObjectType::SPHERE)
 {
@@ -81,8 +80,8 @@ void Svx3DPreviewControl::Construct()
     mpModel->GetItemPool().FreezeIdRanges();
 
     // Page
-    mpFmPage = new FmFormPage( *mpModel );
-    mpModel->InsertPage( mpFmPage, 0 );
+    mxFmPage = new FmFormPage( *mpModel );
+    mpModel->InsertPage( mxFmPage.get(), 0 );
 
     // 3D View
     mp3DView.reset(new E3dView(*mpModel, &rDevice));
@@ -112,7 +111,7 @@ void Svx3DPreviewControl::Construct()
     rCamera.SetFocalLength(fDefaultCamFocal);
 
     mpScene->SetCamera( rCamera );
-    mpFmPage->InsertObject( mpScene );
+    mxFmPage->InsertObject( mpScene );
 
     basegfx::B3DHomMatrix aRotation;
     aRotation.rotate(DEG2RAD( 25 ), 0.0, 0.0);
@@ -132,7 +131,7 @@ void Svx3DPreviewControl::Construct()
     mpScene->SetMergedItemSet(aSet);
 
     // PageView
-    SdrPageView* pPageView = mp3DView->ShowSdrPage( mpFmPage );
+    SdrPageView* pPageView = mp3DView->ShowSdrPage( mxFmPage.get() );
     mp3DView->hideMarkHandles();
 
     // mark scene
@@ -144,7 +143,7 @@ void Svx3DPreviewControl::Resize()
     // size of page
     Size aSize(GetOutputSizePixel());
     aSize = GetDrawingArea()->get_ref_device().PixelToLogic(aSize);
-    mpFmPage->SetSize(aSize);
+    mxFmPage->SetSize(aSize);
 
     // set size
     Size aObjSize( aSize.Width()*5/6, aSize.Height()*5/6 );
@@ -685,7 +684,7 @@ void Svx3DLightControl::Resize()
 {
     // set size of page
     const Size 
aSize(GetDrawingArea()->get_ref_device().PixelToLogic(GetOutputSizePixel()));
-    mpFmPage->SetSize(aSize);
+    mxFmPage->SetSize(aSize);
 
     // set position and size of scene
     mpScene->SetSnapRect(tools::Rectangle(Point(0, 0), aSize));
diff --git a/svx/source/dialog/graphctl.cxx b/svx/source/dialog/graphctl.cxx
index 4f1ad5049deb..f73e316cfa18 100644
--- a/svx/source/dialog/graphctl.cxx
+++ b/svx/source/dialog/graphctl.cxx
@@ -108,7 +108,7 @@ void GraphCtrl::InitSdrModel()
 {
     SolarMutexGuard aGuard;
 
-    SdrPage* pPage;
+    rtl::Reference<SdrPage> pPage;
 
     // destroy old junk
     pView.reset();
@@ -125,7 +125,7 @@ void GraphCtrl::InitSdrModel()
 
     pPage->SetSize( aGraphSize );
     pPage->SetBorder( 0, 0, 0, 0 );
-    pModel->InsertPage( pPage );
+    pModel->InsertPage( pPage.get() );
     pModel->SetChanged( false );
 
     // Creating a View
diff --git a/svx/source/form/fmmodel.cxx b/svx/source/form/fmmodel.cxx
index 690ffb77814b..6d1f1a2c0cf6 100644
--- a/svx/source/form/fmmodel.cxx
+++ b/svx/source/form/fmmodel.cxx
@@ -71,7 +71,7 @@ FmFormModel::~FmFormModel()
     SetMaxUndoActionCount(1);
 }
 
-SdrPage* FmFormModel::AllocPage(bool bMasterPage)
+rtl::Reference<SdrPage> FmFormModel::AllocPage(bool bMasterPage)
 {
     return new FmFormPage(*this, bMasterPage);
 }
@@ -85,7 +85,7 @@ void FmFormModel::InsertPage(SdrPage* pPage, sal_uInt16 nPos)
     SdrModel::InsertPage( pPage, nPos );
 }
 
-SdrPage* FmFormModel::RemovePage(sal_uInt16 nPgNum)
+rtl::Reference<SdrPage> FmFormModel::RemovePage(sal_uInt16 nPgNum)
 {
     FmFormPage* pToBeRemovedPage = dynamic_cast< FmFormPage* >( GetPage( 
nPgNum ) );
     OSL_ENSURE( pToBeRemovedPage, "FmFormModel::RemovePage: *which page*?" );
@@ -97,7 +97,7 @@ SdrPage* FmFormModel::RemovePage(sal_uInt16 nPgNum)
             m_pImpl->mxUndoEnv->RemoveForms( xForms );
     }
 
-    FmFormPage* pRemovedPage = 
static_cast<FmFormPage*>(SdrModel::RemovePage(nPgNum));
+    rtl::Reference<FmFormPage> pRemovedPage = 
static_cast<FmFormPage*>(SdrModel::RemovePage(nPgNum).get());
     OSL_ENSURE( pRemovedPage == pToBeRemovedPage, "FmFormModel::RemovePage: 
inconsistency!" );
     return pRemovedPage;
 }
@@ -111,9 +111,9 @@ void FmFormModel::InsertMasterPage(SdrPage* pPage, 
sal_uInt16 nPos)
     SdrModel::InsertMasterPage(pPage, nPos);
 }
 
-SdrPage* FmFormModel::RemoveMasterPage(sal_uInt16 nPgNum)
+rtl::Reference<SdrPage> FmFormModel::RemoveMasterPage(sal_uInt16 nPgNum)
 {
-    FmFormPage* pPage = 
static_cast<FmFormPage*>(SdrModel::RemoveMasterPage(nPgNum));
+    rtl::Reference<FmFormPage> pPage = 
static_cast<FmFormPage*>(SdrModel::RemoveMasterPage(nPgNum).get());
 
     if ( pPage )
     {
diff --git a/svx/source/form/fmpage.cxx b/svx/source/form/fmpage.cxx
index a77d7fef1f8e..f7b875edcb02 100644
--- a/svx/source/form/fmpage.cxx
+++ b/svx/source/form/fmpage.cxx
@@ -62,13 +62,13 @@ FmFormPage::~FmFormPage()
 {
 }
 
-SdrPage* FmFormPage::CloneSdrPage(SdrModel& rTargetModel) const
+rtl::Reference<SdrPage> FmFormPage::CloneSdrPage(SdrModel& rTargetModel) const
 {
     FmFormModel& rFmFormModel(static_cast< FmFormModel& >(rTargetModel));
-    FmFormPage* pClonedFmFormPage(
+    rtl::Reference<FmFormPage> pClonedFmFormPage =
         new FmFormPage(
             rFmFormModel,
-            IsMasterPage()));
+            IsMasterPage());
     pClonedFmFormPage->lateInit(*this);
     return pClonedFmFormPage;
 }
diff --git a/svx/source/gallery2/galobj.cxx b/svx/source/gallery2/galobj.cxx
index de1d5e147ee4..133e7cf990b4 100644
--- a/svx/source/gallery2/galobj.cxx
+++ b/svx/source/gallery2/galobj.cxx
@@ -410,7 +410,7 @@ SvxGalleryDrawModel::SvxGalleryDrawModel()
             
reinterpret_cast<SdrModel*>(xTunnel->getSomething(SdrModel::getUnoTunnelId())));
         if( mpFormModel )
         {
-            mpFormModel->InsertPage( mpFormModel->AllocPage( false ) );
+            mpFormModel->InsertPage( mpFormModel->AllocPage( false ).get() );
         }
     }
 }
diff --git a/svx/source/sdr/contact/objectcontactofobjlistpainter.cxx 
b/svx/source/sdr/contact/objectcontactofobjlistpainter.cxx
index 83dcc051588a..200b05b9cbc0 100644
--- a/svx/source/sdr/contact/objectcontactofobjlistpainter.cxx
+++ b/svx/source/sdr/contact/objectcontactofobjlistpainter.cxx
@@ -175,7 +175,7 @@ void ObjectContactOfPagePainter::SetStartPage(const 
SdrPage* pPage)
 {
     if(pPage != GetStartPage())
     {
-        mxStartPage.reset(const_cast< SdrPage* >(pPage)); // no 
tools::WeakReference<SdrPage> available to hold a const SdrPage*
+        mxStartPage = const_cast< SdrPage* >(pPage); // no 
tools::WeakReference<SdrPage> available to hold a const SdrPage*
     }
 }
 
diff --git a/svx/source/svdraw/svdmodel.cxx b/svx/source/svdraw/svdmodel.cxx
index d5add2eb33b4..89633cf6af5c 100644
--- a/svx/source/svdraw/svdmodel.cxx
+++ b/svx/source/svdraw/svdmodel.cxx
@@ -634,7 +634,7 @@ SdrModel* SdrModel::AllocModel() const
     return pModel;
 }
 
-SdrPage* SdrModel::AllocPage(bool bMasterPage)
+rtl::Reference<SdrPage> SdrModel::AllocPage(bool bMasterPage)
 {
     return new SdrPage(*this,bMasterPage);
 }
@@ -1208,7 +1208,7 @@ void SdrModel::RecalcPageNums(bool bMaster)
         sal_uInt16 nCount=sal_uInt16(maMaPag.size());
         sal_uInt16 i;
         for (i=0; i<nCount; i++) {
-            SdrPage* pPg=maMaPag[i];
+            SdrPage* pPg = maMaPag[i].get();
             pPg->SetPageNum(i);
         }
         m_bMPgNumsDirty=false;
@@ -1218,7 +1218,7 @@ void SdrModel::RecalcPageNums(bool bMaster)
         sal_uInt16 nCount=sal_uInt16(maPages.size());
         sal_uInt16 i;
         for (i=0; i<nCount; i++) {
-            SdrPage* pPg=maPages[i];
+            SdrPage* pPg = maPages[i].get();
             pPg->SetPageNum(i);
         }
         m_bPagNumsDirty=false;
@@ -1245,33 +1245,32 @@ void SdrModel::InsertPage(SdrPage* pPage, sal_uInt16 
nPos)
 
 void SdrModel::DeletePage(sal_uInt16 nPgNum)
 {
-    SdrPage* pPg=RemovePage(nPgNum);
-    delete pPg;
+    RemovePage(nPgNum);
 }
 
-SdrPage* SdrModel::RemovePage(sal_uInt16 nPgNum)
+rtl::Reference<SdrPage> SdrModel::RemovePage(sal_uInt16 nPgNum)
 {
-    SdrPage* pPg=maPages[nPgNum];
+    rtl::Reference<SdrPage> pPg = maPages[nPgNum];
     maPages.erase(maPages.begin()+nPgNum);
     PageListChanged();
-    if (pPg!=nullptr) {
+    if (pPg) {
         pPg->SetInserted(false);
     }
     m_bPagNumsDirty=true;
     SetChanged();
-    SdrHint aHint(SdrHintKind::PageOrderChange, pPg);
+    SdrHint aHint(SdrHintKind::PageOrderChange, pPg.get());
     Broadcast(aHint);
     return pPg;
 }
 
 void SdrModel::MovePage(sal_uInt16 nPgNum, sal_uInt16 nNewPos)
 {
-    SdrPage* pPg=maPages[nPgNum];
-    if (pPg!=nullptr) {
+    rtl::Reference<SdrPage> pPg = std::move(maPages[nPgNum]);
+    if (pPg) {
         maPages.erase(maPages.begin()+nPgNum); // shortcut to avoid two 
broadcasts
         PageListChanged();
         pPg->SetInserted(false);
-        InsertPage(pPg,nNewPos);
+        InsertPage(pPg.get(), nNewPos);
     }
     else
         RemovePage(nPgNum);
@@ -1297,13 +1296,12 @@ void SdrModel::InsertMasterPage(SdrPage* pPage, 
sal_uInt16 nPos)
 
 void SdrModel::DeleteMasterPage(sal_uInt16 nPgNum)
 {
-    SdrPage* pPg=RemoveMasterPage(nPgNum);
-    delete pPg;
+    RemoveMasterPage(nPgNum);
 }
 
-SdrPage* SdrModel::RemoveMasterPage(sal_uInt16 nPgNum)
+rtl::Reference<SdrPage> SdrModel::RemoveMasterPage(sal_uInt16 nPgNum)
 {
-    SdrPage* pRetPg=maMaPag[nPgNum];
+    rtl::Reference<SdrPage> pRetPg = std::move(maMaPag[nPgNum]);
     maMaPag.erase(maMaPag.begin()+nPgNum);
     MasterPageListChanged();
 
@@ -1322,24 +1320,24 @@ SdrPage* SdrModel::RemoveMasterPage(sal_uInt16 nPgNum)
 
     m_bMPgNumsDirty=true;
     SetChanged();
-    SdrHint aHint(SdrHintKind::PageOrderChange, pRetPg);
+    SdrHint aHint(SdrHintKind::PageOrderChange, pRetPg.get());
     Broadcast(aHint);
     return pRetPg;
 }
 
 void SdrModel::MoveMasterPage(sal_uInt16 nPgNum, sal_uInt16 nNewPos)
 {
-    SdrPage* pPg=maMaPag[nPgNum];
+    rtl::Reference<SdrPage> pPg = std::move(maMaPag[nPgNum]);
     maMaPag.erase(maMaPag.begin()+nPgNum);
     MasterPageListChanged();
-    if (pPg!=nullptr) {
+    if (pPg) {
         pPg->SetInserted(false);
         maMaPag.insert(maMaPag.begin()+nNewPos,pPg);
         MasterPageListChanged();
     }
     m_bMPgNumsDirty=true;
     SetChanged();
-    SdrHint aHint(SdrHintKind::PageOrderChange, pPg);
+    SdrHint aHint(SdrHintKind::PageOrderChange, pPg.get());
     Broadcast(aHint);
 }
 
@@ -1385,7 +1383,7 @@ void SdrModel::CopyPages(sal_uInt16 nFirstPageNum, 
sal_uInt16 nLastPageNum,
     sal_uInt16 nDestNum=nDestPos;
     for (nCopyNum=0; nCopyNum<nCopyCnt; nCopyNum++)
     {
-        SdrPage* pPg=pPagePtrs[nCopyNum];
+        rtl::Reference<SdrPage> pPg = pPagePtrs[nCopyNum];
         sal_uInt16 nPageNum2=pPg->GetPageNum();
         if (!bMoveNoCopy)
         {
@@ -1394,7 +1392,7 @@ void SdrModel::CopyPages(sal_uInt16 nFirstPageNum, 
sal_uInt16 nLastPageNum,
             // Clone to local model
             pPg = pPg1->CloneSdrPage(*this);
 
-            InsertPage(pPg,nDestNum);
+            InsertPage(pPg.get(), nDestNum);
             if (bUndo)
                 AddUndo(GetSdrUndoFactory().CreateUndoCopyPage(*pPg));
             nDestNum++;
@@ -1409,7 +1407,7 @@ void SdrModel::CopyPages(sal_uInt16 nFirstPageNum, 
sal_uInt16 nLastPageNum,
                 
AddUndo(GetSdrUndoFactory().CreateUndoSetPageNum(*GetPage(nPageNum2),nPageNum2,nDestNum));
 
             pPg=RemovePage(nPageNum2);
-            InsertPage(pPg,nDestNum);
+            InsertPage(pPg.get(), nDestNum);
             nDestNum++;
         }
 
@@ -1498,12 +1496,12 @@ void SdrModel::Merge(SdrModel& rSourceModel,
             {
                 // Always Clone to new model
                 const SdrPage* pPg1(rSourceModel.GetMasterPage(i));
-                SdrPage* pPg(pPg1->CloneSdrPage(*this));
+                rtl::Reference<SdrPage> pPg = pPg1->CloneSdrPage(*this);
 
                 if(!bTreadSourceAsConst)
                 {
                     // if requested, delete original/modify original model
-                    delete rSourceModel.RemoveMasterPage(i);
+                    rSourceModel.RemoveMasterPage(i);
                 }
 
                 if (pPg!=nullptr) {
@@ -1531,16 +1529,16 @@ void SdrModel::Merge(SdrModel& rSourceModel,
         {
             // Always Clone to new model
             const SdrPage* pPg1(rSourceModel.GetPage(nSourcePos));
-            SdrPage* pPg(pPg1->CloneSdrPage(*this));
+            rtl::Reference<SdrPage> pPg = pPg1->CloneSdrPage(*this);
 
             if(!bTreadSourceAsConst)
             {
                 // if requested, delete original/modify original model
-                delete rSourceModel.RemovePage(nSourcePos);
+                rSourceModel.RemovePage(nSourcePos);
             }
 
             if (pPg!=nullptr) {
-                InsertPage(pPg,nDestPos);
+                InsertPage(pPg.get(),nDestPos);
                 if (bUndo) 
AddUndo(GetSdrUndoFactory().CreateUndoNewPage(*pPg));
 
                 if(pPg->TRG_HasMasterPage())
@@ -1842,13 +1840,13 @@ void SdrModel::WriteUserDataSequence(css::uno::Sequence 
< css::beans::PropertyVa
 const SdrPage* SdrModel::GetPage(sal_uInt16 nPgNum) const
 {
     DBG_ASSERT(nPgNum < maPages.size(), "SdrModel::GetPage: Access out of 
range (!)");
-    return nPgNum < maPages.size() ? maPages[nPgNum] : nullptr;
+    return nPgNum < maPages.size() ? maPages[nPgNum].get() : nullptr;
 }
 
 SdrPage* SdrModel::GetPage(sal_uInt16 nPgNum)
 {
     DBG_ASSERT(nPgNum < maPages.size(), "SdrModel::GetPage: Access out of 
range (!)");
-    return nPgNum < maPages.size() ? maPages[nPgNum] : nullptr;
+    return nPgNum < maPages.size() ? maPages[nPgNum].get() : nullptr;
 }
 
 sal_uInt16 SdrModel::GetPageCount() const
@@ -1868,13 +1866,13 @@ TextChain *SdrModel::GetTextChain() const
 const SdrPage* SdrModel::GetMasterPage(sal_uInt16 nPgNum) const
 {
     DBG_ASSERT(nPgNum < maMaPag.size(), "SdrModel::GetMasterPage: Access out 
of range (!)");
-    return maMaPag[nPgNum];
+    return maMaPag[nPgNum].get();
 }
 
 SdrPage* SdrModel::GetMasterPage(sal_uInt16 nPgNum)
 {
     DBG_ASSERT(nPgNum < maMaPag.size(), "SdrModel::GetMasterPage: Access out 
of range (!)");
-    return maMaPag[nPgNum];
+    return maMaPag[nPgNum].get();
 }
 
 sal_uInt16 SdrModel::GetMasterPageCount() const
diff --git a/svx/source/svdraw/svdpage.cxx b/svx/source/svdraw/svdpage.cxx
index e199d38b43c5..0c7c806ad4a8 100644
--- a/svx/source/svdraw/svdpage.cxx
+++ b/svx/source/svdraw/svdpage.cxx
@@ -1225,7 +1225,6 @@ void SdrPageProperties::SetStyleSheet(SfxStyleSheet* 
pStyleSheet)
 
 SdrPage::SdrPage(SdrModel& rModel, bool bMasterPage)
 :   SdrObjList(),
-    tools::WeakBase(),
     maPageUsers(),
     mrSdrModelFromSdrPage(rModel),
     mnWidth(10),
@@ -1329,9 +1328,9 @@ void SdrPage::lateInit(const SdrPage& rSrcPage)
     }
 }
 
-SdrPage* SdrPage::CloneSdrPage(SdrModel& rTargetModel) const
+rtl::Reference<SdrPage> SdrPage::CloneSdrPage(SdrModel& rTargetModel) const
 {
-    SdrPage* pClonedPage(new SdrPage(rTargetModel));
+    rtl::Reference<SdrPage> pClonedPage(new SdrPage(rTargetModel));
     pClonedPage->lateInit(*this);
     return pClonedPage;
 }
diff --git a/svx/source/svdraw/svdundo.cxx b/svx/source/svdraw/svdundo.cxx
index 6db2076a5589..f1a83d2b5739 100644
--- a/svx/source/svdraw/svdundo.cxx
+++ b/svx/source/svdraw/svdundo.cxx
@@ -1273,50 +1273,52 @@ OUString SdrUndoDelLayer::GetComment() const
 
 SdrUndoPage::SdrUndoPage(SdrPage& rNewPg)
 :   SdrUndoAction(rNewPg.getSdrModelFromSdrPage())
-    ,mrPage(rNewPg)
+    ,mxPage(&rNewPg)
 {
 }
 
+SdrUndoPage::~SdrUndoPage() {}
+
 void SdrUndoPage::ImpInsertPage(sal_uInt16 nNum)
 {
-    DBG_ASSERT(!mrPage.IsInserted(),"SdrUndoPage::ImpInsertPage(): mrPage is 
already inserted.");
-    if (!mrPage.IsInserted())
+    DBG_ASSERT(!mxPage->IsInserted(),"SdrUndoPage::ImpInsertPage(): mxPage is 
already inserted.");
+    if (!mxPage->IsInserted())
     {
-        if (mrPage.IsMasterPage())
+        if (mxPage->IsMasterPage())
         {
-            rMod.InsertMasterPage(&mrPage,nNum);
+            rMod.InsertMasterPage(mxPage.get(), nNum);
         }
         else
         {
-            rMod.InsertPage(&mrPage,nNum);
+            rMod.InsertPage(mxPage.get(), nNum);
         }
     }
 }
 
 void SdrUndoPage::ImpRemovePage(sal_uInt16 nNum)
 {
-    DBG_ASSERT(mrPage.IsInserted(),"SdrUndoPage::ImpRemovePage(): mrPage is 
not inserted.");
-    if (!mrPage.IsInserted())
+    DBG_ASSERT(mxPage->IsInserted(),"SdrUndoPage::ImpRemovePage(): mxPage is 
not inserted.");
+    if (!mxPage->IsInserted())
         return;
 
-    SdrPage* pChkPg=nullptr;
-    if (mrPage.IsMasterPage())
+    rtl::Reference<SdrPage> pChkPg;
+    if (mxPage->IsMasterPage())
     {
-        pChkPg=rMod.RemoveMasterPage(nNum);
+        pChkPg = rMod.RemoveMasterPage(nNum);
     }
     else
     {
-        pChkPg=rMod.RemovePage(nNum);
+        pChkPg = rMod.RemovePage(nNum);
     }
-    DBG_ASSERT(pChkPg==&mrPage,"SdrUndoPage::ImpRemovePage(): 
RemovePage!=&mrPage");
+    DBG_ASSERT(pChkPg==mxPage,"SdrUndoPage::ImpRemovePage(): 
RemovePage!=mxPage");
 }
 
 void SdrUndoPage::ImpMovePage(sal_uInt16 nOldNum, sal_uInt16 nNewNum)
 {
-    DBG_ASSERT(mrPage.IsInserted(),"SdrUndoPage::ImpMovePage(): mrPage is not 
inserted.");
-    if (mrPage.IsInserted())
+    DBG_ASSERT(mxPage->IsInserted(),"SdrUndoPage::ImpMovePage(): mxPage is not 
inserted.");
+    if (mxPage->IsInserted())
     {
-        if (mrPage.IsMasterPage())
+        if (mxPage->IsMasterPage())
         {
             rMod.MoveMasterPage(nOldNum,nNewNum);
         }
@@ -1335,17 +1337,12 @@ OUString SdrUndoPage::ImpGetDescriptionStr(const char* 
pStrCacheID)
 
 SdrUndoPageList::SdrUndoPageList(SdrPage& rNewPg)
     : SdrUndoPage(rNewPg)
-    , bItsMine(false)
 {
     nPageNum=rNewPg.GetPageNum();
 }
 
 SdrUndoPageList::~SdrUndoPageList()
 {
-    if(bItsMine)
-    {
-        delete &mrPage;
-    }
 }
 
 
@@ -1353,24 +1350,22 @@ SdrUndoDelPage::SdrUndoDelPage(SdrPage& rNewPg)
     : SdrUndoPageList(rNewPg)
     , mbHasFillBitmap(false)
 {
-    bItsMine = true;
-
     // keep fill bitmap separately to remove it from pool if not used elsewhere
-    if (mrPage.IsMasterPage())
+    if (mxPage->IsMasterPage())
     {
-        SfxStyleSheet* const pStyleSheet = 
mrPage.getSdrPageProperties().GetStyleSheet();
+        SfxStyleSheet* const pStyleSheet = 
mxPage->getSdrPageProperties().GetStyleSheet();
         if (pStyleSheet)
             queryFillBitmap(pStyleSheet->GetItemSet());
     }
     else
     {
-        queryFillBitmap(mrPage.getSdrPageProperties().GetItemSet());
+        queryFillBitmap(mxPage->getSdrPageProperties().GetItemSet());
     }
     if (bool(mpFillBitmapItem))
         clearFillBitmap();
 
     // now remember the master page relationships
-    if(!mrPage.IsMasterPage())
+    if(!mxPage->IsMasterPage())
         return;
 
     sal_uInt16 nPageCnt(rMod.GetPageCount());
@@ -1383,7 +1378,7 @@ SdrUndoDelPage::SdrUndoDelPage(SdrPage& rNewPg)
         {
             SdrPage& rMasterPage = pDrawPage->TRG_GetMasterPage();
 

... etc. - the rest is truncated
_______________________________________________
Libreoffice-commits mailing list
libreoffice-comm...@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits

Reply via email to