sc/inc/cellsuno.hxx                |    2 -
 sc/inc/chartarr.hxx                |    6 ++--
 sc/qa/unit/ucalc.cxx               |   34 ++++++++++++------------
 sc/qa/unit/ucalc.hxx               |    2 -
 sc/qa/unit/ucalc_sharedformula.cxx |    2 -
 sc/source/core/tool/chartarr.cxx   |   10 +++----
 sc/source/ui/docshell/dbdocfun.cxx |   26 +++++++++---------
 sc/source/ui/docshell/dbdocimp.cxx |   15 ++++------
 sc/source/ui/docshell/docfunc.cxx  |   52 ++++++++++++++++++-------------------
 sc/source/ui/docshell/docsh5.cxx   |    4 +-
 sc/source/ui/docshell/impex.cxx    |    4 +-
 sc/source/ui/docshell/olinefun.cxx |   30 ++++++++++-----------
 sc/source/ui/inc/undoblk.hxx       |   28 +++++++++----------
 sc/source/ui/inc/undodat.hxx       |   20 +++++++-------
 sc/source/ui/undo/undoblk.cxx      |   48 +++++++++++++++++-----------------
 sc/source/ui/undo/undoblk2.cxx     |    4 +-
 sc/source/ui/undo/undodat.cxx      |   44 +++++++++++++++----------------
 sc/source/ui/unoobj/cellsuno.cxx   |   14 ++++-----
 sc/source/ui/view/dbfunc3.cxx      |   12 ++++----
 sc/source/ui/view/viewfun3.cxx     |   22 +++++++--------
 sc/source/ui/view/viewfun4.cxx     |    8 ++---
 sc/source/ui/view/viewfunc.cxx     |   14 ++++-----
 22 files changed, 200 insertions(+), 201 deletions(-)

New commits:
commit 6100ef45d8c90f141d68011bba9d9745dd319174
Author:     Noel Grandin <noel.gran...@collabora.co.uk>
AuthorDate: Mon Aug 27 15:58:54 2018 +0200
Commit:     Noel Grandin <noel.gran...@collabora.co.uk>
CommitDate: Tue Aug 28 10:13:06 2018 +0200

    return ScMemChart by std::unique_ptr
    
    Change-Id: I5a5b54872ce6ae351c6550a1ec0b2f7c52c35e13
    Reviewed-on: https://gerrit.libreoffice.org/59683
    Tested-by: Jenkins
    Reviewed-by: Noel Grandin <noel.gran...@collabora.co.uk>

diff --git a/sc/inc/cellsuno.hxx b/sc/inc/cellsuno.hxx
index 625818ce5cfb..3bdb5e357fc4 100644
--- a/sc/inc/cellsuno.hxx
+++ b/sc/inc/cellsuno.hxx
@@ -202,7 +202,7 @@ private:
     void            PaintGridRanges_Impl();
     ScRangeListRef  GetLimitedChartRanges_Impl( long nDataColumns, long 
nDataRows ) const;
     void            ForceChartListener_Impl();
-    ScMemChart*     CreateMemChart_Impl() const;
+    std::unique_ptr<ScMemChart> CreateMemChart_Impl() const;
 
     const ScPatternAttr*    GetCurrentAttrsFlat();
     const ScPatternAttr*    GetCurrentAttrsDeep();
diff --git a/sc/inc/chartarr.hxx b/sc/inc/chartarr.hxx
index b24745bc7388..b342be84f7ac 100644
--- a/sc/inc/chartarr.hxx
+++ b/sc/inc/chartarr.hxx
@@ -61,8 +61,8 @@ class SC_DLLPUBLIC ScChartArray             // only 
parameter-struct
     ScChartPositioner aPositioner;
 
 private:
-    ScMemChart* CreateMemChartSingle();
-    ScMemChart* CreateMemChartMulti();
+    std::unique_ptr<ScMemChart> CreateMemChartSingle();
+    std::unique_ptr<ScMemChart> CreateMemChartMulti();
 public:
     ScChartArray( ScDocument* pDoc, const ScRangeListRef& rRangeList );
 
@@ -73,7 +73,7 @@ public:
     bool    HasColHeaders() const { return aPositioner.HasColHeaders(); }
     bool    HasRowHeaders() const { return aPositioner.HasRowHeaders(); }
 
-    ScMemChart* CreateMemChart();
+    std::unique_ptr<ScMemChart> CreateMemChart();
 };
 
 #endif
diff --git a/sc/source/core/tool/chartarr.cxx b/sc/source/core/tool/chartarr.cxx
index ba632ba1a386..76fcc2645839 100644
--- a/sc/source/core/tool/chartarr.cxx
+++ b/sc/source/core/tool/chartarr.cxx
@@ -60,7 +60,7 @@ ScChartArray::ScChartArray(
     pDocument( pDoc ),
     aPositioner(pDoc, rRangeList) {}
 
-ScMemChart* ScChartArray::CreateMemChart()
+std::unique_ptr<ScMemChart> ScChartArray::CreateMemChart()
 {
     ScRangeListRef aRangeListRef(GetRangeList());
     size_t nCount = aRangeListRef->size();
@@ -112,7 +112,7 @@ double getCellValue( ScDocument& rDoc, const ScAddress& 
rPos, double fDefault, b
 
 }
 
-ScMemChart* ScChartArray::CreateMemChartSingle()
+std::unique_ptr<ScMemChart> ScChartArray::CreateMemChartSingle()
 {
     SCSIZE nCol;
     SCSIZE nRow;
@@ -207,7 +207,7 @@ ScMemChart* ScChartArray::CreateMemChartSingle()
     }
 
     //  Data
-    ScMemChart* pMemChart = new ScMemChart( nColCount, nRowCount );
+    std::unique_ptr<ScMemChart> pMemChart(new ScMemChart( nColCount, nRowCount 
));
 
     if ( bValidData )
     {
@@ -275,7 +275,7 @@ ScMemChart* ScChartArray::CreateMemChartSingle()
     return pMemChart;
 }
 
-ScMemChart* ScChartArray::CreateMemChartMulti()
+std::unique_ptr<ScMemChart> ScChartArray::CreateMemChartMulti()
 {
     SCSIZE nColCount = GetPositionMap()->GetColCount();
     SCSIZE nRowCount = GetPositionMap()->GetRowCount();
@@ -300,7 +300,7 @@ ScMemChart* ScChartArray::CreateMemChartMulti()
     }
 
     //  Data
-    ScMemChart* pMemChart = new ScMemChart( nColCount, nRowCount );
+    std::unique_ptr<ScMemChart> pMemChart(new ScMemChart( nColCount, nRowCount 
));
 
     SCSIZE nCol = 0;
     SCSIZE nRow = 0;
diff --git a/sc/source/ui/unoobj/cellsuno.cxx b/sc/source/ui/unoobj/cellsuno.cxx
index e957cb22faef..4d31248c34e9 100644
--- a/sc/source/ui/unoobj/cellsuno.cxx
+++ b/sc/source/ui/unoobj/cellsuno.cxx
@@ -2995,7 +2995,7 @@ void SAL_CALL ScCellRangesBase::incrementIndent()
 
 // XChartData
 
-ScMemChart* ScCellRangesBase::CreateMemChart_Impl() const
+std::unique_ptr<ScMemChart> ScCellRangesBase::CreateMemChart_Impl() const
 {
     if ( pDocShell && !aRanges.empty() )
     {
commit b7239a1271d5af51163b4fcd93608b72db2616a4
Author:     Noel Grandin <noel.gran...@collabora.co.uk>
AuthorDate: Mon Aug 27 10:11:58 2018 +0200
Commit:     Noel Grandin <noel.gran...@collabora.co.uk>
CommitDate: Tue Aug 28 10:12:57 2018 +0200

    loplugin:useuniqueptr pass ScDocument by unique_ptr in undo code
    
    Change-Id: Ib05638865a42ad37c3382714e1790c7035ed8ebf
    Reviewed-on: https://gerrit.libreoffice.org/59638
    Reviewed-by: Noel Grandin <noel.gran...@collabora.co.uk>
    Tested-by: Noel Grandin <noel.gran...@collabora.co.uk>

diff --git a/sc/qa/unit/ucalc.cxx b/sc/qa/unit/ucalc.cxx
index c35e28623c2c..791b25ab85b2 100644
--- a/sc/qa/unit/ucalc.cxx
+++ b/sc/qa/unit/ucalc.cxx
@@ -3339,9 +3339,9 @@ void Test::testCopyPaste()
     copyToClip(m_pDoc, aRange, &aClipDoc);
 
     aRange = ScRange(0,1,1,2,1,1);//target: Sheet2.A2:C2
-    ScDocument* pUndoDoc = new ScDocument(SCDOCMODE_UNDO);
+    ScDocumentUniquePtr pUndoDoc(new ScDocument(SCDOCMODE_UNDO));
     pUndoDoc->InitUndo(m_pDoc, 1, 1, true, true);
-    std::unique_ptr<ScUndoPaste> pUndo(createUndoPaste(getDocShell(), aRange, 
pUndoDoc));
+    std::unique_ptr<ScUndoPaste> pUndo(createUndoPaste(getDocShell(), aRange, 
std::move(pUndoDoc)));
     ScMarkData aMark;
     aMark.SetMarkArea(aRange);
     m_pDoc->CopyFromClip(aRange, aMark, InsertDeleteFlags::ALL, nullptr, 
&aClipDoc);
@@ -3735,21 +3735,21 @@ void Test::testCopyPasteSkipEmpty()
     }
 
     // Create undo document.
-    ScDocument* pUndoDoc = new ScDocument(SCDOCMODE_UNDO);
+    ScDocumentUniquePtr pUndoDoc(new ScDocument(SCDOCMODE_UNDO));
     pUndoDoc->InitUndo(m_pDoc, 0, 0);
     m_pDoc->CopyToDocument(aDestRange, InsertDeleteFlags::ALL, false, 
*pUndoDoc, &aMark);
 
     // Paste clipboard content onto A1:A5 but skip empty cells.
-    m_pDoc->CopyFromClip(aDestRange, aMark, InsertDeleteFlags::ALL, pUndoDoc, 
&aClipDoc, true, false, false, true/*bSkipEmpty*/);
+    m_pDoc->CopyFromClip(aDestRange, aMark, InsertDeleteFlags::ALL, 
pUndoDoc.get(), &aClipDoc, true, false, false, true/*bSkipEmpty*/);
 
     // Create redo document.
-    ScDocument* pRedoDoc = new ScDocument(SCDOCMODE_UNDO);
+    ScDocumentUniquePtr pRedoDoc(new ScDocument(SCDOCMODE_UNDO));
     pRedoDoc->InitUndo(m_pDoc, 0, 0);
     m_pDoc->CopyToDocument(aDestRange, InsertDeleteFlags::ALL, false, 
*pRedoDoc, &aMark);
 
     // Create an undo object for this.
     ScRefUndoData* pRefUndoData = new ScRefUndoData(m_pDoc);
-    ScUndoPaste aUndo(&getDocShell(), aDestRange, aMark, pUndoDoc, pRedoDoc, 
InsertDeleteFlags::ALL, pRefUndoData);
+    ScUndoPaste aUndo(&getDocShell(), aDestRange, aMark, std::move(pUndoDoc), 
std::move(pRedoDoc), InsertDeleteFlags::ALL, pRefUndoData);
 
     // Check the content after the paste.
     {
@@ -3847,11 +3847,11 @@ void Test::testCutPasteRefUndo()
     aClipDoc.SetValue(ScAddress(1,1,0), 12.0);
 
     // Set up undo document for reference update.
-    ScDocument* pUndoDoc = new ScDocument(SCDOCMODE_UNDO);
+    ScDocumentUniquePtr pUndoDoc(new ScDocument(SCDOCMODE_UNDO));
     pUndoDoc->InitUndo(m_pDoc, 0, 0);
 
     // Do the pasting of 12 into C2.  This should update A2 to reference C2.
-    m_pDoc->CopyFromClip(ScAddress(2,1,0), aMark, InsertDeleteFlags::CONTENTS, 
pUndoDoc, &aClipDoc);
+    m_pDoc->CopyFromClip(ScAddress(2,1,0), aMark, InsertDeleteFlags::CONTENTS, 
pUndoDoc.get(), &aClipDoc);
     CPPUNIT_ASSERT_EQUAL(12.0, m_pDoc->GetValue(0,1,0));
 
     ASSERT_FORMULA_EQUAL(*m_pDoc, ScAddress(0,1,0), "C2", "A2 should be 
referencing C2.");
@@ -3859,7 +3859,7 @@ void Test::testCutPasteRefUndo()
     // At this point, the ref undo document should contain a formula cell at 
A2 that references B2.
     ASSERT_FORMULA_EQUAL(*pUndoDoc, ScAddress(0,1,0), "B2", "A2 in the undo 
document should be referencing B2.");
 
-    ScUndoPaste aUndo(&getDocShell(), ScRange(2,1,0), aMark, pUndoDoc, 
nullptr, InsertDeleteFlags::CONTENTS, nullptr, false, nullptr);
+    ScUndoPaste aUndo(&getDocShell(), ScRange(2,1,0), aMark, 
std::move(pUndoDoc), nullptr, InsertDeleteFlags::CONTENTS, nullptr, false, 
nullptr);
     aUndo.Undo();
 
     // Now A2 should be referencing B2 once again.
@@ -3935,7 +3935,7 @@ void Test::testCutPasteGroupRefUndo()
     aMark.SetMarkArea(aPasteRange);
     ScDocument* pPasteUndoDoc = new ScDocument(SCDOCMODE_UNDO);
     pPasteUndoDoc->InitUndoSelected( m_pDoc, aMark);
-    std::unique_ptr<ScUndoPaste> pUndoPaste( createUndoPaste( getDocShell(), 
aPasteRange, pPasteUndoDoc));
+    std::unique_ptr<ScUndoPaste> pUndoPaste( createUndoPaste( getDocShell(), 
aPasteRange, ScDocumentUniquePtr(pPasteUndoDoc)));
     m_pDoc->CopyFromClip( aPasteRange, aMark, InsertDeleteFlags::ALL, 
pPasteUndoDoc, &aClipDoc);
 
     // Check data after Paste.
@@ -4042,13 +4042,13 @@ void Test::testUndoCut()
     aMark.MarkToMulti();
 
     // Set up an undo object for cutting A1:A3.
-    ScDocument* pUndoDoc = new ScDocument(SCDOCMODE_UNDO);
+    ScDocumentUniquePtr pUndoDoc(new ScDocument(SCDOCMODE_UNDO));
     pUndoDoc->InitUndo(m_pDoc, 0 ,0);
     m_pDoc->CopyToDocument(aRange, InsertDeleteFlags::ALL, false, *pUndoDoc);
     ASSERT_DOUBLES_EQUAL(  1.0, pUndoDoc->GetValue(ScAddress(0,0,0)));
     ASSERT_DOUBLES_EQUAL( 10.0, pUndoDoc->GetValue(ScAddress(0,1,0)));
     ASSERT_DOUBLES_EQUAL(100.0, pUndoDoc->GetValue(ScAddress(0,2,0)));
-    ScUndoCut aUndo(&getDocShell(), aRange, aRange.aEnd, aMark, pUndoDoc);
+    ScUndoCut aUndo(&getDocShell(), aRange, aRange.aEnd, aMark, 
std::move(pUndoDoc));
 
     // "Cut" the selection.
     m_pDoc->DeleteSelection(InsertDeleteFlags::ALL, aMark);
@@ -6685,10 +6685,10 @@ ScUndoCut* Test::cutToClip(ScDocShell& rDocSh, const 
ScRange& rRange, ScDocument
     pSrcDoc->CopyToClip(aClipParam, pClipDoc, &aMark, false, false);
 
     // Taken from ScViewFunc::CutToClip()
-    ScDocument* pUndoDoc = nullptr;
+    ScDocumentUniquePtr pUndoDoc;
     if (bCreateUndo)
     {
-        pUndoDoc = new ScDocument( SCDOCMODE_UNDO );
+        pUndoDoc.reset(new ScDocument( SCDOCMODE_UNDO ));
         pUndoDoc->InitUndoSelected( pSrcDoc, aMark );
         // all sheets - CopyToDocument skips those that don't exist in pUndoDoc
         ScRange aCopyRange = rRange;
@@ -6704,7 +6704,7 @@ ScUndoCut* Test::cutToClip(ScDocShell& rDocSh, const 
ScRange& rRange, ScDocument
     aMark.MarkToSimple();
 
     if (pUndoDoc)
-        return new ScUndoCut( &rDocSh, rRange, rRange.aEnd, aMark, pUndoDoc );
+        return new ScUndoCut( &rDocSh, rRange, rRange.aEnd, aMark, 
std::move(pUndoDoc) );
 
     return nullptr;
 }
@@ -6736,7 +6736,7 @@ void Test::pasteOneCellFromClip(ScDocument* pDestDoc, 
const ScRange& rDestRange,
             rDestRange.aEnd.Col(), rDestRange.aEnd.Row());
 }
 
-ScUndoPaste* Test::createUndoPaste(ScDocShell& rDocSh, const ScRange& rRange, 
ScDocument* pUndoDoc)
+ScUndoPaste* Test::createUndoPaste(ScDocShell& rDocSh, const ScRange& rRange, 
ScDocumentUniquePtr pUndoDoc)
 {
     ScDocument& rDoc = rDocSh.GetDocument();
     ScMarkData aMarkData;
@@ -6744,7 +6744,7 @@ ScUndoPaste* Test::createUndoPaste(ScDocShell& rDocSh, 
const ScRange& rRange, Sc
     ScRefUndoData* pRefUndoData = new ScRefUndoData(&rDoc);
 
     return new ScUndoPaste(
-        &rDocSh, rRange, aMarkData, pUndoDoc, nullptr, InsertDeleteFlags::ALL, 
pRefUndoData, false);
+        &rDocSh, rRange, aMarkData, std::move(pUndoDoc), nullptr, 
InsertDeleteFlags::ALL, pRefUndoData, false);
 }
 
 void Test::setExpandRefs(bool bExpand)
diff --git a/sc/qa/unit/ucalc.hxx b/sc/qa/unit/ucalc.hxx
index 1d46b3d50800..25cfe3f6b48d 100644
--- a/sc/qa/unit/ucalc.hxx
+++ b/sc/qa/unit/ucalc.hxx
@@ -50,7 +50,7 @@ public:
     static void copyToClip(ScDocument* pSrcDoc, const ScRange& rRange, 
ScDocument* pClipDoc);
     static void pasteFromClip(ScDocument* pDestDoc, const ScRange& rDestRange, 
ScDocument* pClipDoc);
     static void pasteOneCellFromClip(ScDocument* pDestDoc, const ScRange& 
rDestRange, ScDocument* pClipDoc, InsertDeleteFlags eFlags = 
InsertDeleteFlags::ALL);
-    static ScUndoPaste* createUndoPaste(ScDocShell& rDocSh, const ScRange& 
rRange, ScDocument* pUndoDoc);
+    static ScUndoPaste* createUndoPaste(ScDocShell& rDocSh, const ScRange& 
rRange, ScDocumentUniquePtr pUndoDoc);
 
     /**
      * Enable or disable expand reference options which controls how
diff --git a/sc/qa/unit/ucalc_sharedformula.cxx 
b/sc/qa/unit/ucalc_sharedformula.cxx
index ffd00aede191..033f79ca8fdf 100644
--- a/sc/qa/unit/ucalc_sharedformula.cxx
+++ b/sc/qa/unit/ucalc_sharedformula.cxx
@@ -1296,7 +1296,7 @@ void Test::testSharedFormulasCopyPaste()
     ScDocument* pUndoDoc = new ScDocument(SCDOCMODE_UNDO);
     pUndoDoc->InitUndo(m_pDoc, 0, 0, true, true);
     m_pDoc->CopyToDocument(aRange, InsertDeleteFlags::CONTENTS, false, 
*pUndoDoc);
-    std::unique_ptr<ScUndoPaste> pUndo(createUndoPaste(getDocShell(), aRange, 
pUndoDoc));
+    std::unique_ptr<ScUndoPaste> pUndo(createUndoPaste(getDocShell(), aRange, 
ScDocumentUniquePtr(pUndoDoc)));
 
     // First, make sure the formula cells are shared in the undo document.
     aPos.SetCol(1);
diff --git a/sc/source/ui/docshell/dbdocfun.cxx 
b/sc/source/ui/docshell/dbdocfun.cxx
index 1157972ee407..5c324bab714a 100644
--- a/sc/source/ui/docshell/dbdocfun.cxx
+++ b/sc/source/ui/docshell/dbdocfun.cxx
@@ -341,7 +341,7 @@ bool ScDBDocFunc::RepeatDB( const OUString& rDBName, bool 
bApi, bool bIsUnnamed,
 
             //!     Undo needed data only ?
 
-            ScDocument* pUndoDoc = nullptr;
+            ScDocumentUniquePtr pUndoDoc;
             ScOutlineTable* pUndoTab = nullptr;
             ScRangeName* pUndoRange = nullptr;
             ScDBCollection* pUndoDB = nullptr;
@@ -349,7 +349,7 @@ bool ScDBDocFunc::RepeatDB( const OUString& rDBName, bool 
bApi, bool bIsUnnamed,
             if (bRecord)
             {
                 SCTAB nTabCount = rDoc.GetTableCount();
-                pUndoDoc = new ScDocument( SCDOCMODE_UNDO );
+                pUndoDoc.reset(new ScDocument( SCDOCMODE_UNDO ));
                 ScOutlineTable* pTable = rDoc.GetOutlineTable( nTab );
                 if (pTable)
                 {
@@ -448,7 +448,7 @@ bool ScDBDocFunc::RepeatDB( const OUString& rDBName, bool 
bApi, bool bIsUnnamed,
                                             nNewEndRow,
                                             //nCurX, nCurY,
                                             nStartCol, nStartRow,
-                                            pUndoDoc, pUndoTab,
+                                            std::move(pUndoDoc), pUndoTab,
                                             pUndoRange, pUndoDB,
                                             pOld, pNew ) );
             }
@@ -736,13 +736,13 @@ bool ScDBDocFunc::Query( SCTAB nTab, const ScQueryParam& 
rQueryParam,
             bKeepSub = true;
     }
 
-    ScDocument* pUndoDoc = nullptr;
+    ScDocumentUniquePtr pUndoDoc;
     ScDBCollection* pUndoDB = nullptr;
     const ScRange* pOld = nullptr;
 
     if ( bRecord )
     {
-        pUndoDoc = new ScDocument( SCDOCMODE_UNDO );
+        pUndoDoc.reset(new ScDocument( SCDOCMODE_UNDO ));
         if (bCopy)
         {
             pUndoDoc->InitUndo( &rDoc, nDestTab, nDestTab, false, true );
@@ -930,7 +930,7 @@ bool ScDBDocFunc::Query( SCTAB nTab, const ScQueryParam& 
rQueryParam,
     {
         // create undo action after executing, because of drawing layer undo
         rDocShell.GetUndoManager()->AddUndoAction(
-                    new ScUndoQuery( &rDocShell, nTab, rQueryParam, pUndoDoc, 
pUndoDB,
+                    new ScUndoQuery( &rDocShell, nTab, rQueryParam, 
std::move(pUndoDoc), pUndoDB,
                                         pOld, bDoSize, pAdvSource ) );
     }
 
@@ -1027,7 +1027,7 @@ void ScDBDocFunc::DoSubTotals( SCTAB nTab, const 
ScSubTotalParam& rParam,
         ScDocShellModificator aModificator( rDocShell );
 
         ScSubTotalParam aNewParam( rParam );        // end of range is being 
changed
-        ScDocument*     pUndoDoc = nullptr;
+        ScDocumentUniquePtr pUndoDoc;
         std::unique_ptr<ScOutlineTable> pUndoTab;
         ScRangeName*    pUndoRange = nullptr;
         ScDBCollection* pUndoDB = nullptr;
@@ -1037,7 +1037,7 @@ void ScDBDocFunc::DoSubTotals( SCTAB nTab, const 
ScSubTotalParam& rParam,
             bool bOldFilter = bDo && rParam.bDoSort;
 
             SCTAB nTabCount = rDoc.GetTableCount();
-            pUndoDoc = new ScDocument( SCDOCMODE_UNDO );
+            pUndoDoc.reset(new ScDocument( SCDOCMODE_UNDO ));
             ScOutlineTable* pTable = rDoc.GetOutlineTable( nTab );
             if (pTable)
             {
@@ -1110,7 +1110,7 @@ void ScDBDocFunc::DoSubTotals( SCTAB nTab, const 
ScSubTotalParam& rParam,
             rDocShell.GetUndoManager()->AddUndoAction(
                 new ScUndoSubTotals( &rDocShell, nTab,
                                         rParam, aNewParam.nRow2,
-                                        pUndoDoc, std::move(pUndoTab), // 
pUndoDBData,
+                                        std::move(pUndoDoc), 
std::move(pUndoTab), // pUndoDBData,
                                         pUndoRange, pUndoDB ) );
         }
 
@@ -1326,7 +1326,7 @@ bool ScDBDocFunc::DataPilotUpdate( ScDPObject* pOldObj, 
const ScDPObject* pNewOb
     {
         rDocShell.GetUndoManager()->AddUndoAction(
             new ScUndoDataPilot(
-                &rDocShell, pOldUndoDoc.release(), pNewUndoDoc.release(), 
&aUndoDPObj, pOldObj, bAllowMove));
+                &rDocShell, std::move(pOldUndoDoc), std::move(pNewUndoDoc), 
&aUndoDPObj, pOldObj, bAllowMove));
     }
 
     // notify API objects
@@ -1410,7 +1410,7 @@ bool ScDBDocFunc::RemovePivotTable(ScDPObject& rDPObj, 
bool bRecord, bool bApi)
     {
         rDocShell.GetUndoManager()->AddUndoAction(
             new ScUndoDataPilot(
-                &rDocShell, pOldUndoDoc.release(), nullptr, pUndoDPObj.get(), 
nullptr, false));
+                &rDocShell, std::move(pOldUndoDoc), nullptr, pUndoDPObj.get(), 
nullptr, false));
 
         // pUndoDPObj is copied
     }
@@ -1522,7 +1522,7 @@ bool ScDBDocFunc::CreatePivotTable(const ScDPObject& 
rDPObj, bool bRecord, bool
     if (bRecord)
     {
         rDocShell.GetUndoManager()->AddUndoAction(
-            new ScUndoDataPilot(&rDocShell, nullptr, pNewUndoDoc.release(), 
nullptr, &rDestObj, false));
+            new ScUndoDataPilot(&rDocShell, nullptr, std::move(pNewUndoDoc), 
nullptr, &rDestObj, false));
     }
 
     // notify API objects
@@ -1598,7 +1598,7 @@ bool ScDBDocFunc::UpdatePivotTable(ScDPObject& rDPObj, 
bool bRecord, bool bApi)
     {
         rDocShell.GetUndoManager()->AddUndoAction(
             new ScUndoDataPilot(
-                &rDocShell, pOldUndoDoc.release(), pNewUndoDoc.release(), 
&aUndoDPObj, &rDPObj, false));
+                &rDocShell, std::move(pOldUndoDoc), std::move(pNewUndoDoc), 
&aUndoDPObj, &rDPObj, false));
     }
 
     // notify API objects
diff --git a/sc/source/ui/docshell/dbdocimp.cxx 
b/sc/source/ui/docshell/dbdocimp.cxx
index 7fa81368ba04..b7362c0966b0 100644
--- a/sc/source/ui/docshell/dbdocimp.cxx
+++ b/sc/source/ui/docshell/dbdocimp.cxx
@@ -181,7 +181,7 @@ bool ScDBDocFunc::DoImport( SCTAB nTab, const 
ScImportParam& rParam,
         
xResultSet.set((*pDescriptor)[svx::DataAccessDescriptorProperty::Cursor], 
uno::UNO_QUERY);
 
     // ImportDoc - also used for Redo
-    ScDocument* pImportDoc = new ScDocument( SCDOCMODE_UNDO );
+    ScDocumentUniquePtr pImportDoc(new ScDocument( SCDOCMODE_UNDO ));
     pImportDoc->InitUndo( &rDoc, nTab, nTab );
 
     //  get data from database into import document
@@ -330,7 +330,7 @@ bool ScDBDocFunc::DoImport( SCTAB nTab, const 
ScImportParam& rParam,
                             nCol = rParam.nCol1;
                             for (long i=0; i<nColCount; i++)
                             {
-                                ScDatabaseDocUtil::PutData( pImportDoc, nCol, 
nRow, nTab,
+                                ScDatabaseDocUtil::PutData( pImportDoc.get(), 
nCol, nRow, nTab,
                                                 xRow, i+1, pTypeArr[i], 
pCurrArr[i] );
                                 ++nCol;
                             }
@@ -466,11 +466,11 @@ bool ScDBDocFunc::DoImport( SCTAB nTab, const 
ScImportParam& rParam,
         SCCOL nUndoEndCol = std::max( nEndCol, rParam.nCol2 );       // rParam 
= old end
         SCROW nUndoEndRow = std::max( nEndRow, rParam.nRow2 );
 
-        ScDocument* pUndoDoc = nullptr;
+        ScDocumentUniquePtr pUndoDoc;
         ScDBData* pUndoDBData = nullptr;
         if ( bRecord )
         {
-            pUndoDoc = new ScDocument( SCDOCMODE_UNDO );
+            pUndoDoc.reset(new ScDocument( SCDOCMODE_UNDO ));
             pUndoDoc->InitUndo( &rDoc, nTab, nTab );
 
             pUndoDBData = new ScDBData( *pDBData );
@@ -574,8 +574,7 @@ bool ScDBDocFunc::DoImport( SCTAB nTab, const 
ScImportParam& rParam,
 
         if (bRecord)
         {
-            ScDocument* pRedoDoc = pImportDoc;
-            pImportDoc = nullptr;
+            ScDocumentUniquePtr pRedoDoc = std::move(pImportDoc);
 
             if (nFormulaCols > 0)                   // include filled formulas 
for redo
                 rDoc.CopyToDocument(rParam.nCol1, rParam.nRow1, nTab,
@@ -588,7 +587,7 @@ bool ScDBDocFunc::DoImport( SCTAB nTab, const 
ScImportParam& rParam,
                 new ScUndoImportData( &rDocShell, nTab,
                                         rParam, nUndoEndCol, nUndoEndRow,
                                         nFormulaCols,
-                                        pUndoDoc, pRedoDoc, pUndoDBData, 
pRedoDBData ) );
+                                        std::move(pUndoDoc), 
std::move(pRedoDoc), pUndoDBData, pRedoDBData ) );
         }
 
         sc::SetFormulaDirtyContext aCxt;
@@ -623,7 +622,7 @@ bool ScDBDocFunc::DoImport( SCTAB nTab, const 
ScImportParam& rParam,
         xInfoBox->run();
     }
 
-    delete pImportDoc;
+    pImportDoc.reset();
 
     if (bSuccess && pChangeTrack)
         pChangeTrack->AppendInsert ( aChangedRange );
diff --git a/sc/source/ui/docshell/docfunc.cxx 
b/sc/source/ui/docshell/docfunc.cxx
index 94ec954867f8..f995de081037 100644
--- a/sc/source/ui/docshell/docfunc.cxx
+++ b/sc/source/ui/docshell/docfunc.cxx
@@ -732,7 +732,7 @@ bool ScDocFunc::TransliterateText( const ScMarkData& rMark, 
TransliterationFlags
         SCTAB nStartTab = aMarkRange.aStart.Tab();
         SCTAB nTabCount = rDoc.GetTableCount();
 
-        ScDocument* pUndoDoc = new ScDocument( SCDOCMODE_UNDO );
+        ScDocumentUniquePtr pUndoDoc(new ScDocument( SCDOCMODE_UNDO ));
         pUndoDoc->InitUndo( &rDoc, nStartTab, nStartTab );
         ScMarkData::const_iterator itr = rMark.begin(), itrEnd = rMark.end();
         for (; itr != itrEnd && *itr < nTabCount; ++itr)
@@ -745,7 +745,7 @@ bool ScDocFunc::TransliterateText( const ScMarkData& rMark, 
TransliterationFlags
         rDoc.CopyToDocument(aCopyRange, InsertDeleteFlags::CONTENTS, true, 
*pUndoDoc, &aMultiMark);
 
         rDocShell.GetUndoManager()->AddUndoAction(
-            new ScUndoTransliterate( &rDocShell, aMultiMark, pUndoDoc, nType ) 
);
+            new ScUndoTransliterate( &rDocShell, aMultiMark, 
std::move(pUndoDoc), nType ) );
     }
 
     rDoc.TransliterateText( aMultiMark, nType );
@@ -1411,7 +1411,7 @@ bool ScDocFunc::ApplyStyle( const ScMarkData& rMark, 
const OUString& rStyleName,
 
     if ( bRecord )
     {
-        ScDocument* pUndoDoc = new ScDocument( SCDOCMODE_UNDO );
+        ScDocumentUniquePtr pUndoDoc(new ScDocument( SCDOCMODE_UNDO ));
         SCTAB nStartTab = aMultiRange.aStart.Tab();
         SCTAB nTabCount = rDoc.GetTableCount();
         pUndoDoc->InitUndo( &rDoc, nStartTab, nStartTab );
@@ -1427,7 +1427,7 @@ bool ScDocFunc::ApplyStyle( const ScMarkData& rMark, 
const OUString& rStyleName,
 
         rDocShell.GetUndoManager()->AddUndoAction(
             new ScUndoSelectionStyle(
-                    &rDocShell, rMark, aMultiRange, rStyleName, pUndoDoc ) );
+                    &rDocShell, rMark, aMultiRange, rStyleName, 
std::move(pUndoDoc) ) );
 
     }
 
@@ -3530,7 +3530,7 @@ bool ScDocFunc::SetWidthOrHeight(
         //! Option "Show formulas" - but where to get them from?
     }
 
-    ScDocument*     pUndoDoc = nullptr;
+    ScDocumentUniquePtr pUndoDoc;
     std::unique_ptr<ScOutlineTable> pUndoTab;
     std::vector<sc::ColRowSpan> aUndoRanges;
 
@@ -3538,7 +3538,7 @@ bool ScDocFunc::SetWidthOrHeight(
     {
         rDoc.BeginDrawUndo();                          // Drawing Updates
 
-        pUndoDoc = new ScDocument( SCDOCMODE_UNDO );
+        pUndoDoc.reset(new ScDocument( SCDOCMODE_UNDO ));
         if (bWidth)
         {
             pUndoDoc->InitUndo( &rDoc, nTab, nTab, true );
@@ -3656,7 +3656,7 @@ bool ScDocFunc::SetWidthOrHeight(
         aMark.SelectOneTable( nTab );
         rDocShell.GetUndoManager()->AddUndoAction(
             new ScUndoWidthOrHeight(
-                &rDocShell, aMark, nStart, nTab, nEnd, nTab, pUndoDoc,
+                &rDocShell, aMark, nStart, nTab, nEnd, nTab, 
std::move(pUndoDoc),
                 aUndoRanges, std::move(pUndoTab), eMode, nSizeTwips, bWidth));
     }
 
@@ -4026,7 +4026,7 @@ bool ScDocFunc::ChangeIndent( const ScMarkData& rMark, 
bool bIncrement, bool bAp
         SCTAB nStartTab = aMarkRange.aStart.Tab();
         SCTAB nTabCount = rDoc.GetTableCount();
 
-        ScDocument* pUndoDoc = new ScDocument( SCDOCMODE_UNDO );
+        ScDocumentUniquePtr pUndoDoc(new ScDocument( SCDOCMODE_UNDO ));
         pUndoDoc->InitUndo( &rDoc, nStartTab, nStartTab );
         ScMarkData::const_iterator itr = rMark.begin(), itrEnd = rMark.end();
         for (; itr != itrEnd && *itr < nTabCount; ++itr)
@@ -4039,7 +4039,7 @@ bool ScDocFunc::ChangeIndent( const ScMarkData& rMark, 
bool bIncrement, bool bAp
         rDoc.CopyToDocument( aCopyRange, InsertDeleteFlags::ATTRIB, true, 
*pUndoDoc, &rMark );
 
         rDocShell.GetUndoManager()->AddUndoAction(
-            new ScUndoIndent( &rDocShell, rMark, pUndoDoc, bIncrement ) );
+            new ScUndoIndent( &rDocShell, rMark, std::move(pUndoDoc), 
bIncrement ) );
     }
 
     rDoc.ChangeSelectionIndent( bIncrement, rMark );
@@ -4206,13 +4206,13 @@ bool ScDocFunc::EnterMatrix( const ScRange& rRange, 
const ScMarkData* pTabMark,
     {
         WaitObject aWait( ScDocShell::GetActiveDialogParent() );
 
-        ScDocument* pUndoDoc = nullptr;
+        ScDocumentUniquePtr pUndoDoc;
 
         const bool bUndo(rDoc.IsUndoEnabled());
         if (bUndo)
         {
             //! take selected sheets into account also when undoing
-            pUndoDoc = new ScDocument( SCDOCMODE_UNDO );
+            pUndoDoc.reset(new ScDocument( SCDOCMODE_UNDO ));
             pUndoDoc->InitUndo( &rDoc, nStartTab, nEndTab );
             rDoc.CopyToDocument( rRange, InsertDeleteFlags::ALL & 
~InsertDeleteFlags::NOTE, false, *pUndoDoc );
         }
@@ -4248,7 +4248,7 @@ bool ScDocFunc::EnterMatrix( const ScRange& rRange, const 
ScMarkData* pTabMark,
         {
             //! take selected sheets into account also when undoing
             rDocShell.GetUndoManager()->AddUndoAction(
-                new ScUndoEnterMatrix( &rDocShell, rRange, pUndoDoc, rString ) 
);
+                new ScUndoEnterMatrix( &rDocShell, rRange, 
std::move(pUndoDoc), rString ) );
         }
 
         //  Err522 painting of DDE-Formulas will be intercepted during 
interpreting
@@ -5251,11 +5251,11 @@ bool ScDocFunc::InsertNameList( const ScAddress& 
rStartPos, bool bApi )
         ScEditableTester aTester( &rDoc, nTab, nStartCol,nStartRow, 
nEndCol,nEndRow );
         if (aTester.IsEditable())
         {
-            ScDocument* pUndoDoc = nullptr;
+            ScDocumentUniquePtr pUndoDoc;
 
             if (bRecord)
             {
-                pUndoDoc = new ScDocument( SCDOCMODE_UNDO );
+                pUndoDoc.reset(new ScDocument( SCDOCMODE_UNDO ));
                 pUndoDoc->InitUndo( &rDoc, nTab, nTab );
                 rDoc.CopyToDocument(nStartCol,nStartRow,nTab, 
nEndCol,nEndRow,nTab,
                                     InsertDeleteFlags::ALL, false, *pUndoDoc);
@@ -5301,7 +5301,7 @@ bool ScDocFunc::InsertNameList( const ScAddress& 
rStartPos, bool bApi )
 
             if (bRecord)
             {
-                ScDocument* pRedoDoc = new ScDocument( SCDOCMODE_UNDO );
+                ScDocumentUniquePtr pRedoDoc(new ScDocument( SCDOCMODE_UNDO ));
                 pRedoDoc->InitUndo( &rDoc, nTab, nTab );
                 rDoc.CopyToDocument(nStartCol,nStartRow,nTab, 
nEndCol,nEndRow,nTab,
                                     InsertDeleteFlags::ALL, false, *pRedoDoc);
@@ -5309,7 +5309,7 @@ bool ScDocFunc::InsertNameList( const ScAddress& 
rStartPos, bool bApi )
                 rDocShell.GetUndoManager()->AddUndoAction(
                     new ScUndoListNames( &rDocShell,
                                 ScRange( nStartCol,nStartRow,nTab, 
nEndCol,nEndRow,nTab ),
-                                pUndoDoc, pRedoDoc ) );
+                                std::move(pUndoDoc), std::move(pRedoDoc) ) );
             }
 
             if 
(!AdjustRowHeight(ScRange(0,nStartRow,nTab,MAXCOL,nEndRow,nTab)))
@@ -5464,12 +5464,12 @@ void ScDocFunc::ReplaceConditionalFormat( sal_uLong 
nOldFormat, ScConditionalFor
         return;
 
     bool bUndo = rDoc.IsUndoEnabled();
-    ScDocument* pUndoDoc = nullptr;
+    ScDocumentUniquePtr pUndoDoc;
     ScRange aCombinedRange = rRanges.Combine();
     ScRange aCompleteRange;
     if(bUndo)
     {
-        pUndoDoc = new ScDocument(SCDOCMODE_UNDO);
+        pUndoDoc.reset(new ScDocument(SCDOCMODE_UNDO));
         pUndoDoc->InitUndo( &rDoc, nTab, nTab );
 
         if(pFormat)
@@ -5516,13 +5516,13 @@ void ScDocFunc::ReplaceConditionalFormat( sal_uLong 
nOldFormat, ScConditionalFor
 
     if(bUndo)
     {
-        ScDocument* pRedoDoc = new ScDocument(SCDOCMODE_UNDO);
+        ScDocumentUniquePtr pRedoDoc(new ScDocument(SCDOCMODE_UNDO));
         pRedoDoc->InitUndo( &rDoc, nTab, nTab );
         
rDoc.CopyToDocument(aCompleteRange.aStart.Col(),aCompleteRange.aStart.Row(),nTab,
                             
aCompleteRange.aEnd.Col(),aCompleteRange.aEnd.Row(),nTab,
                             InsertDeleteFlags::ALL, false, *pRedoDoc);
         rDocShell.GetUndoManager()->AddUndoAction(
-                new ScUndoConditionalFormat(&rDocShell, pUndoDoc, pRedoDoc, 
aCompleteRange));
+                new ScUndoConditionalFormat(&rDocShell, std::move(pUndoDoc), 
std::move(pRedoDoc), aCompleteRange));
     }
 
     if(pRepaintRange)
@@ -5540,16 +5540,16 @@ void ScDocFunc::SetConditionalFormatList( 
ScConditionalFormatList* pList, SCTAB
         return;
 
     bool bUndo = rDoc.IsUndoEnabled();
-    ScDocument* pUndoDoc = nullptr;
+    ScDocumentUniquePtr pUndoDoc;
     if (bUndo)
     {
-        pUndoDoc = new ScDocument(SCDOCMODE_UNDO);
+        pUndoDoc.reset(new ScDocument(SCDOCMODE_UNDO));
         pUndoDoc->InitUndo( &rDoc, nTab, nTab );
 
         ScConditionalFormatList* pOld = rDoc.GetCondFormList(nTab);
 
         if (pOld)
-            pUndoDoc->SetCondFormList(new ScConditionalFormatList(pUndoDoc, 
*pOld), nTab);
+            pUndoDoc->SetCondFormList(new 
ScConditionalFormatList(pUndoDoc.get(), *pOld), nTab);
         else
             pUndoDoc->SetCondFormList(nullptr, nTab);
 
@@ -5567,12 +5567,12 @@ void ScDocFunc::SetConditionalFormatList( 
ScConditionalFormatList* pList, SCTAB
 
     if(bUndo)
     {
-        ScDocument* pRedoDoc = new ScDocument(SCDOCMODE_UNDO);
+        ScDocumentUniquePtr pRedoDoc(new ScDocument(SCDOCMODE_UNDO));
         pRedoDoc->InitUndo( &rDoc, nTab, nTab );
-        pRedoDoc->SetCondFormList(new ScConditionalFormatList(pRedoDoc, 
*pList), nTab);
+        pRedoDoc->SetCondFormList(new ScConditionalFormatList(pRedoDoc.get(), 
*pList), nTab);
 
         rDocShell.GetUndoManager()->AddUndoAction(
-                new ScUndoConditionalFormatList(&rDocShell, pUndoDoc, 
pRedoDoc, nTab));
+                new ScUndoConditionalFormatList(&rDocShell, 
std::move(pUndoDoc), std::move(pRedoDoc), nTab));
     }
 
     rDoc.SetStreamValid(nTab, false);
diff --git a/sc/source/ui/docshell/docsh5.cxx b/sc/source/ui/docshell/docsh5.cxx
index 5a7734749b5a..32f842080a0a 100644
--- a/sc/source/ui/docshell/docsh5.cxx
+++ b/sc/source/ui/docshell/docsh5.cxx
@@ -692,7 +692,7 @@ void ScDocShell::UseScenario( SCTAB nTab, const OUString& 
rName, bool bRecord )
 
                 if (bRecord)
                 {
-                    ScDocument* pUndoDoc = new ScDocument( SCDOCMODE_UNDO );
+                    ScDocumentUniquePtr pUndoDoc(new ScDocument( 
SCDOCMODE_UNDO ));
                     pUndoDoc->InitUndo( &m_aDocument, nTab,nEndTab );          
   // also all scenarios
                     //  shown table:
                     m_aDocument.CopyToDocument(nStartCol, nStartRow, nTab,
@@ -718,7 +718,7 @@ void ScDocShell::UseScenario( SCTAB nTab, const OUString& 
rName, bool bRecord )
                     GetUndoManager()->AddUndoAction(
                         new ScUndoUseScenario( this, aScenMark,
                                         ScArea( 
nTab,nStartCol,nStartRow,nEndCol,nEndRow ),
-                                        pUndoDoc, rName ) );
+                                        std::move(pUndoDoc), rName ) );
                 }
 
                 m_aDocument.CopyScenario( nSrcTab, nTab );
diff --git a/sc/source/ui/docshell/impex.cxx b/sc/source/ui/docshell/impex.cxx
index 9128a93f0195..8a36dded1fe2 100644
--- a/sc/source/ui/docshell/impex.cxx
+++ b/sc/source/ui/docshell/impex.cxx
@@ -256,13 +256,13 @@ void ScImportExport::EndPaste(bool bAutoRowHeight)
 
     if( pUndoDoc && pDoc->IsUndoEnabled() && pDocSh )
     {
-        ScDocument* pRedoDoc = new ScDocument( SCDOCMODE_UNDO );
+        ScDocumentUniquePtr pRedoDoc(new ScDocument( SCDOCMODE_UNDO ));
         pRedoDoc->InitUndo( pDoc, aRange.aStart.Tab(), aRange.aEnd.Tab() );
         pDoc->CopyToDocument(aRange, InsertDeleteFlags::ALL | 
InsertDeleteFlags::NOCAPTIONS, false, *pRedoDoc);
         ScMarkData aDestMark;
         aDestMark.SetMarkArea(aRange);
         pDocSh->GetUndoManager()->AddUndoAction(
-            new ScUndoPaste(pDocSh, aRange, aDestMark, pUndoDoc.release(), 
pRedoDoc, InsertDeleteFlags::ALL, nullptr));
+            new ScUndoPaste(pDocSh, aRange, aDestMark, std::move(pUndoDoc), 
std::move(pRedoDoc), InsertDeleteFlags::ALL, nullptr));
     }
     pUndoDoc.reset();
     if( pDocSh )
diff --git a/sc/source/ui/docshell/olinefun.cxx 
b/sc/source/ui/docshell/olinefun.cxx
index d22d5e3a11d8..b63de2cf27a4 100644
--- a/sc/source/ui/docshell/olinefun.cxx
+++ b/sc/source/ui/docshell/olinefun.cxx
@@ -220,7 +220,7 @@ bool ScOutlineDocFunc::RemoveAllOutlines( SCTAB nTab, bool 
bRecord )
             SCCOL nEndCol = static_cast<SCCOL>(nCol2);
             SCROW nEndRow = nRow2;
 
-            ScDocument* pUndoDoc = new ScDocument( SCDOCMODE_UNDO );
+            ScDocumentUniquePtr pUndoDoc(new ScDocument( SCDOCMODE_UNDO ));
             pUndoDoc->InitUndo( &rDoc, nTab, nTab, true, true );
             rDoc.CopyToDocument(nStartCol, 0, nTab, nEndCol, MAXROW, nTab, 
InsertDeleteFlags::NONE, false, *pUndoDoc);
             rDoc.CopyToDocument(0, nStartRow, nTab, MAXCOL, nEndRow, nTab, 
InsertDeleteFlags::NONE, false, *pUndoDoc);
@@ -231,7 +231,7 @@ bool ScOutlineDocFunc::RemoveAllOutlines( SCTAB nTab, bool 
bRecord )
                 new ScUndoRemoveAllOutlines( &rDocShell,
                                                 nStartCol, nStartRow, nTab,
                                                 nEndCol, nEndRow, nTab,
-                                                pUndoDoc, std::move(pUndoTab) 
) );
+                                                std::move(pUndoDoc), 
std::move(pUndoTab) ) );
         }
 
         SelectLevel( nTab, true,  pTable->GetColArray().GetDepth(), false, 
false );
@@ -266,7 +266,7 @@ void ScOutlineDocFunc::AutoOutline( const ScRange& rRange, 
bool bRecord )
         bRecord = false;
     ScOutlineTable* pTable = rDoc.GetOutlineTable( nTab );
 
-    ScDocument* pUndoDoc = nullptr;
+    ScDocumentUniquePtr pUndoDoc;
     std::unique_ptr<ScOutlineTable> pUndoTab;
 
     if ( pTable )
@@ -283,7 +283,7 @@ void ScOutlineDocFunc::AutoOutline( const ScRange& rRange, 
bool bRecord )
             SCCOL nOutEndCol = static_cast<SCCOL>(nCol2);
             SCROW nOutEndRow = nRow2;
 
-            pUndoDoc = new ScDocument( SCDOCMODE_UNDO );
+            pUndoDoc.reset(new ScDocument( SCDOCMODE_UNDO ));
             pUndoDoc->InitUndo( &rDoc, nTab, nTab, true, true );
             rDoc.CopyToDocument(nOutStartCol, 0, nTab, nOutEndCol, MAXROW, 
nTab, InsertDeleteFlags::NONE, false, *pUndoDoc);
             rDoc.CopyToDocument(0, nOutStartRow, nTab, MAXCOL, nOutEndRow, 
nTab, InsertDeleteFlags::NONE, false, *pUndoDoc);
@@ -303,7 +303,7 @@ void ScOutlineDocFunc::AutoOutline( const ScRange& rRange, 
bool bRecord )
             new ScUndoAutoOutline( &rDocShell,
                                     nStartCol, nStartRow, nTab,
                                     nEndCol, nEndRow, nTab,
-                                    pUndoDoc, std::move(pUndoTab) ) );
+                                    std::move(pUndoDoc), std::move(pUndoTab) ) 
);
     }
 
     rDoc.SetStreamValid(nTab, false);
@@ -333,7 +333,7 @@ bool ScOutlineDocFunc::SelectLevel( SCTAB nTab, bool 
bColumns, sal_uInt16 nLevel
     if (!comphelper::LibreOfficeKit::isActive() && bRecord )
     {
         std::unique_ptr<ScOutlineTable> pUndoTab(new ScOutlineTable( *pTable 
));
-        ScDocument* pUndoDoc = new ScDocument( SCDOCMODE_UNDO );
+        ScDocumentUniquePtr pUndoDoc(new ScDocument( SCDOCMODE_UNDO ));
         if (bColumns)
         {
             pUndoDoc->InitUndo( &rDoc, nTab, nTab, true );
@@ -350,7 +350,7 @@ bool ScOutlineDocFunc::SelectLevel( SCTAB nTab, bool 
bColumns, sal_uInt16 nLevel
         rDocShell.GetUndoManager()->AddUndoAction(
             new ScUndoOutlineLevel( &rDocShell,
                                     nStart, nEnd, nTab,             //! 
calculate start and end
-                                    pUndoDoc, std::move(pUndoTab),
+                                    std::move(pUndoDoc), std::move(pUndoTab),
                                     bColumns, nLevel ) );
     }
 
@@ -455,7 +455,7 @@ bool ScOutlineDocFunc::ShowMarkedOutlines( const ScRange& 
rRange, bool bRecord )
         if ( !comphelper::LibreOfficeKit::isActive() && bRecord )
         {
             std::unique_ptr<ScOutlineTable> pUndoTab(new ScOutlineTable( 
*pTable ));
-            ScDocument* pUndoDoc = new ScDocument( SCDOCMODE_UNDO );
+            ScDocumentUniquePtr pUndoDoc(new ScDocument( SCDOCMODE_UNDO ));
             pUndoDoc->InitUndo( &rDoc, nTab, nTab, true, true );
             rDoc.CopyToDocument(nStartCol, 0, nTab, nEndCol, MAXROW, nTab, 
InsertDeleteFlags::NONE, false, *pUndoDoc);
             rDoc.CopyToDocument(0, nStartRow, nTab, MAXCOL, nEndRow, nTab, 
InsertDeleteFlags::NONE, false, *pUndoDoc);
@@ -463,7 +463,7 @@ bool ScOutlineDocFunc::ShowMarkedOutlines( const ScRange& 
rRange, bool bRecord )
             rDocShell.GetUndoManager()->AddUndoAction(
                 new ScUndoOutlineBlock( &rDocShell,
                                         nStartCol, nStartRow, nTab, nEndCol, 
nEndRow, nTab,
-                                        pUndoDoc, std::move(pUndoTab), true ) 
);
+                                        std::move(pUndoDoc), 
std::move(pUndoTab), true ) );
         }
 
         //  Columns
@@ -581,7 +581,7 @@ bool ScOutlineDocFunc::HideMarkedOutlines( const ScRange& 
rRange, bool bRecord )
         if ( !comphelper::LibreOfficeKit::isActive() && bRecord )
         {
             std::unique_ptr<ScOutlineTable> pUndoTab(new ScOutlineTable( 
*pTable ));
-            ScDocument* pUndoDoc = new ScDocument( SCDOCMODE_UNDO );
+            ScDocumentUniquePtr pUndoDoc(new ScDocument( SCDOCMODE_UNDO ));
             pUndoDoc->InitUndo( &rDoc, nTab, nTab, true, true );
             rDoc.CopyToDocument(static_cast<SCCOL>(nEffStartCol), 0, nTab,
                                 static_cast<SCCOL>(nEffEndCol), MAXROW, nTab, 
InsertDeleteFlags::NONE,
@@ -591,7 +591,7 @@ bool ScOutlineDocFunc::HideMarkedOutlines( const ScRange& 
rRange, bool bRecord )
             rDocShell.GetUndoManager()->AddUndoAction(
                 new ScUndoOutlineBlock( &rDocShell,
                                         nStartCol, nStartRow, nTab, nEndCol, 
nEndRow, nTab,
-                                        pUndoDoc, std::move(pUndoTab), false ) 
);
+                                        std::move(pUndoDoc), 
std::move(pUndoTab), false ) );
         }
 
         //  Columns
@@ -651,7 +651,7 @@ void ScOutlineDocFunc::ShowOutline( SCTAB nTab, bool 
bColumns, sal_uInt16 nLevel
     // this is a temporarily workaround
     if ( !comphelper::LibreOfficeKit::isActive() && bRecord )
     {
-        ScDocument* pUndoDoc = new ScDocument( SCDOCMODE_UNDO );
+        ScDocumentUniquePtr pUndoDoc(new ScDocument( SCDOCMODE_UNDO ));
         if (bColumns)
         {
             pUndoDoc->InitUndo( &rDoc, nTab, nTab, true );
@@ -667,7 +667,7 @@ void ScOutlineDocFunc::ShowOutline( SCTAB nTab, bool 
bColumns, sal_uInt16 nLevel
 
         rDocShell.GetUndoManager()->AddUndoAction(
             new ScUndoDoOutline( &rDocShell,
-                                    nStart, nEnd, nTab, pUndoDoc,       //! 
calc start and end
+                                    nStart, nEnd, nTab, std::move(pUndoDoc),  
//! calc start and end
                                     bColumns, nLevel, nEntry, true ) );
     }
 
@@ -743,7 +743,7 @@ bool ScOutlineDocFunc::HideOutline( SCTAB nTab, bool 
bColumns, sal_uInt16 nLevel
     // this is a temporarily workaround
     if ( !comphelper::LibreOfficeKit::isActive() &&  bRecord )
     {
-        ScDocument* pUndoDoc = new ScDocument( SCDOCMODE_UNDO );
+        ScDocumentUniquePtr pUndoDoc(new ScDocument( SCDOCMODE_UNDO ));
         if (bColumns)
         {
             pUndoDoc->InitUndo( &rDoc, nTab, nTab, true );
@@ -759,7 +759,7 @@ bool ScOutlineDocFunc::HideOutline( SCTAB nTab, bool 
bColumns, sal_uInt16 nLevel
 
         rDocShell.GetUndoManager()->AddUndoAction(
             new ScUndoDoOutline( &rDocShell,
-                                    nStart, nEnd, nTab, pUndoDoc,
+                                    nStart, nEnd, nTab, std::move(pUndoDoc),
                                     bColumns, nLevel, nEntry, false ) );
     }
 
diff --git a/sc/source/ui/inc/undoblk.hxx b/sc/source/ui/inc/undoblk.hxx
index ed47fb2913ee..c0c5963c97d8 100644
--- a/sc/source/ui/inc/undoblk.hxx
+++ b/sc/source/ui/inc/undoblk.hxx
@@ -141,7 +141,7 @@ public:
                                ScRange aRange,              // adjusted for 
merged cells
                                const ScAddress& aOldEnd,           // end 
position without adjustment
                                const ScMarkData& rMark,     // selected sheets
-                               ScDocument* pNewUndoDoc );
+                               ScDocumentUniquePtr pNewUndoDoc );
     virtual         ~ScUndoCut() override;
 
     virtual void    Undo() override;
@@ -185,7 +185,7 @@ class ScUndoPaste: public ScMultiBlockUndo
 public:
     ScUndoPaste(ScDocShell* pNewDocShell, const ScRangeList& rRanges,
                 const ScMarkData& rMark,
-                ScDocument* pNewUndoDoc, ScDocument* pNewRedoDoc,
+                ScDocumentUniquePtr pNewUndoDoc, ScDocumentUniquePtr 
pNewRedoDoc,
                 InsertDeleteFlags nNewFlags,
                 ScRefUndoData* pRefData,
                 bool bRedoIsFilled = true,
@@ -201,8 +201,8 @@ public:
 
 private:
     ScMarkData      aMarkData;
-    std::unique_ptr<ScDocument> pUndoDoc;
-    std::unique_ptr<ScDocument> pRedoDoc;
+    ScDocumentUniquePtr pUndoDoc;
+    ScDocumentUniquePtr pRedoDoc;
     InsertDeleteFlags nFlags;
     std::unique_ptr<ScRefUndoData> pRefUndoData;
     std::unique_ptr<ScRefUndoData> pRefRedoData;
@@ -363,7 +363,7 @@ public:
                                          const ScMarkData& rMark,
                                          SCCOLROW nNewStart, SCTAB 
nNewStartTab,
                                          SCCOLROW nNewEnd, SCTAB nNewEndTab,
-                                         ScDocument* pNewUndoDoc,
+                                         ScDocumentUniquePtr pNewUndoDoc,
                                          const std::vector<sc::ColRowSpan>& 
rRanges,
                                          std::unique_ptr<ScOutlineTable> 
pNewUndoTab,
                                          ScSizeMode eNewMode, sal_uInt16 
nNewSizeTwips,
@@ -383,7 +383,7 @@ private:
     SCCOLROW        nEnd;
     SCTAB           nStartTab;
     SCTAB           nEndTab;
-    std::unique_ptr<ScDocument>     pUndoDoc;
+    ScDocumentUniquePtr pUndoDoc;
     std::unique_ptr<ScOutlineTable> pUndoTab;
     std::vector<sc::ColRowSpan> maRanges;
     sal_uInt16      nNewSize;
@@ -599,7 +599,7 @@ class ScUndoListNames: public ScBlockUndo
 public:
     ScUndoListNames(ScDocShell* pNewDocShell,
                     const ScRange& rRange,
-                    ScDocument* pNewUndoDoc, ScDocument* pNewRedoDoc);
+                    ScDocumentUniquePtr pNewUndoDoc, ScDocumentUniquePtr 
pNewRedoDoc);
 
     virtual void    Undo() override;
     virtual void    Redo() override;
@@ -619,7 +619,7 @@ class ScUndoConditionalFormat : public ScSimpleUndo
 {
 public:
     ScUndoConditionalFormat( ScDocShell* pNewDocShell,
-            ScDocument* pUndoDoc, ScDocument* pRedoDoc, const ScRange& rRange);
+            ScDocumentUniquePtr pUndoDoc, ScDocumentUniquePtr pRedoDoc, const 
ScRange& rRange);
     virtual         ~ScUndoConditionalFormat() override;
 
     virtual void    Undo() override;
@@ -640,7 +640,7 @@ class ScUndoConditionalFormatList : public ScSimpleUndo
 {
 public:
     ScUndoConditionalFormatList( ScDocShell* pNewDocShell,
-            ScDocument* pUndoDoc, ScDocument* pRedoDoc, SCTAB nTab);
+            ScDocumentUniquePtr pUndoDoc, ScDocumentUniquePtr pRedoDoc, SCTAB 
nTab);
     virtual         ~ScUndoConditionalFormatList() override;
 
     virtual void    Undo() override;
@@ -662,7 +662,7 @@ class ScUndoUseScenario: public ScSimpleUndo
 public:
                     ScUndoUseScenario( ScDocShell* pNewDocShell,
                                        const ScMarkData& rMark,
-                                       const ScArea& rDestArea, ScDocument* 
pNewUndoDoc,
+                                       const ScArea& rDestArea, 
ScDocumentUniquePtr pNewUndoDoc,
                                        const OUString& rNewName );
     virtual         ~ScUndoUseScenario() override;
 
@@ -688,7 +688,7 @@ public:
                                           const ScMarkData& rMark,
                                           const ScRange& rRange,
                                           const OUString& rName,
-                                          ScDocument* pNewUndoDoc );
+                                          ScDocumentUniquePtr pNewUndoDoc );
     virtual         ~ScUndoSelectionStyle() override;
 
     virtual void    Undo() override;
@@ -731,7 +731,7 @@ class ScUndoEnterMatrix: public ScBlockUndo
 public:
                     ScUndoEnterMatrix( ScDocShell* pNewDocShell,
                                        const ScRange& rArea,
-                                       ScDocument* pNewUndoDoc,
+                                       ScDocumentUniquePtr pNewUndoDoc,
                                        const OUString& rForm );
     virtual         ~ScUndoEnterMatrix() override;
 
@@ -850,7 +850,7 @@ class ScUndoIndent: public ScBlockUndo
 {
 public:
                     ScUndoIndent( ScDocShell* pNewDocShell, const ScMarkData& 
rMark,
-                                    ScDocument* pNewUndoDoc, bool bIncrement );
+                                  ScDocumentUniquePtr pNewUndoDoc, bool 
bIncrement );
     virtual         ~ScUndoIndent() override;
 
     virtual void    Undo() override;
@@ -871,7 +871,7 @@ class ScUndoTransliterate: public ScBlockUndo
 {
 public:
                     ScUndoTransliterate( ScDocShell* pNewDocShell, const 
ScMarkData& rMark,
-                                        ScDocument* pNewUndoDoc, 
TransliterationFlags nType );
+                                        ScDocumentUniquePtr pNewUndoDoc, 
TransliterationFlags nType );
     virtual         ~ScUndoTransliterate() override;
 
     virtual void    Undo() override;
diff --git a/sc/source/ui/inc/undodat.hxx b/sc/source/ui/inc/undodat.hxx
index b9155ddbeb8a..77d51e356825 100644
--- a/sc/source/ui/inc/undodat.hxx
+++ b/sc/source/ui/inc/undodat.hxx
@@ -45,7 +45,7 @@ class ScUndoDoOutline: public ScSimpleUndo
 public:
                     ScUndoDoOutline( ScDocShell* pNewDocShell,
                             SCCOLROW nNewStart, SCCOLROW nNewEnd, SCTAB 
nNewTab,
-                            ScDocument* pNewUndoDoc, bool bNewColumns,
+                            ScDocumentUniquePtr pNewUndoDoc, bool bNewColumns,
                             sal_uInt16 nNewLevel, sal_uInt16 nNewEntry, bool 
bNewShow );
     virtual         ~ScUndoDoOutline() override;
 
@@ -99,7 +99,7 @@ class ScUndoOutlineLevel: public ScSimpleUndo
 public:
     ScUndoOutlineLevel(ScDocShell* pNewDocShell,
                        SCCOLROW nNewStart, SCCOLROW nNewEnd, SCTAB nNewTab,
-                       ScDocument* pNewUndoDoc, 
std::unique_ptr<ScOutlineTable> pNewUndoTab,
+                       ScDocumentUniquePtr pNewUndoDoc, 
std::unique_ptr<ScOutlineTable> pNewUndoTab,
                        bool bNewColumns, sal_uInt16 nNewLevel);
 
     virtual void    Undo() override;
@@ -125,7 +125,7 @@ public:
     ScUndoOutlineBlock(ScDocShell* pNewDocShell,
                        SCCOL nStartX, SCROW nStartY, SCTAB nStartZ,
                        SCCOL nEndX, SCROW nEndY, SCTAB nEndZ,
-                       ScDocument* pNewUndoDoc, 
std::unique_ptr<ScOutlineTable> pNewUndoTab,
+                       ScDocumentUniquePtr pNewUndoDoc, 
std::unique_ptr<ScOutlineTable> pNewUndoTab,
                        bool bNewShow);
 
     virtual void    Undo() override;
@@ -149,7 +149,7 @@ public:
     ScUndoRemoveAllOutlines(ScDocShell* pNewDocShell,
             SCCOL nStartX, SCROW nStartY, SCTAB nStartZ,
             SCCOL nEndX, SCROW nEndY, SCTAB nEndZ,
-            ScDocument* pNewUndoDoc, std::unique_ptr<ScOutlineTable> 
pNewUndoTab);
+            ScDocumentUniquePtr pNewUndoDoc, std::unique_ptr<ScOutlineTable> 
pNewUndoTab);
 
     virtual void    Undo() override;
     virtual void    Redo() override;
@@ -171,7 +171,7 @@ public:
     ScUndoAutoOutline(ScDocShell* pNewDocShell,
                       SCCOL nStartX, SCROW nStartY, SCTAB nStartZ,
                       SCCOL nEndX, SCROW nEndY, SCTAB nEndZ,
-                      ScDocument* pNewUndoDoc, std::unique_ptr<ScOutlineTable> 
pNewUndoTab);
+                      ScDocumentUniquePtr pNewUndoDoc, 
std::unique_ptr<ScOutlineTable> pNewUndoTab);
 
     virtual void    Undo() override;
     virtual void    Redo() override;
@@ -192,7 +192,7 @@ class ScUndoSubTotals: public ScDBFuncUndo
 public:
     ScUndoSubTotals(ScDocShell* pNewDocShell, SCTAB nNewTab,
                     const ScSubTotalParam& rNewParam, SCROW nNewEndY,
-                    ScDocument* pNewUndoDoc, std::unique_ptr<ScOutlineTable> 
pNewUndoTab,
+                    ScDocumentUniquePtr pNewUndoDoc, 
std::unique_ptr<ScOutlineTable> pNewUndoTab,
                     ScRangeName* pNewUndoRange, ScDBCollection* pNewUndoDB);
 
     virtual void    Undo() override;
@@ -216,7 +216,7 @@ class ScUndoQuery: public ScDBFuncUndo
 {
 public:
                     ScUndoQuery( ScDocShell* pNewDocShell, SCTAB nNewTab,
-                            const ScQueryParam& rParam, ScDocument* 
pNewUndoDoc,
+                            const ScQueryParam& rParam, ScDocumentUniquePtr 
pNewUndoDoc,
                             ScDBCollection* pNewUndoDB, const ScRange* pOld,
                             bool bSize, const ScRange* pAdvSrc );
     virtual         ~ScUndoQuery() override;
@@ -288,7 +288,7 @@ public:
     ScUndoImportData(ScDocShell* pNewDocShell, SCTAB nNewTab,
                      const ScImportParam& rParam, SCCOL nNewEndX, SCROW 
nNewEndY,
                      SCCOL nNewFormula,
-                     ScDocument* pNewUndoDoc, ScDocument* pNewRedoDoc,
+                     ScDocumentUniquePtr pNewUndoDoc, ScDocumentUniquePtr 
pNewRedoDoc,
                      ScDBData* pNewUndoData, ScDBData* pNewRedoData);
 
     virtual void    Undo() override;
@@ -317,7 +317,7 @@ public:
     ScUndoRepeatDB(ScDocShell* pNewDocShell, SCTAB nNewTab,
                    SCCOL nStartX, SCROW nStartY, SCCOL nEndX, SCROW nEndY,
                    SCROW nResultEndRow, SCCOL nCurX, SCROW nCurY,
-                   ScDocument* pNewUndoDoc, ScOutlineTable* pNewUndoTab,
+                   ScDocumentUniquePtr pNewUndoDoc, ScOutlineTable* 
pNewUndoTab,
                    ScRangeName* pNewUndoRange, ScDBCollection* pNewUndoDB,
                    const ScRange* pOldQ, const ScRange* pNewQ);
 
@@ -346,7 +346,7 @@ class ScUndoDataPilot: public ScSimpleUndo
 {
 public:
     ScUndoDataPilot(ScDocShell* pNewDocShell,
-                    ScDocument* pOldDoc, ScDocument* pNewDoc,
+                    ScDocumentUniquePtr pOldDoc, ScDocumentUniquePtr pNewDoc,
                     const ScDPObject* pOldObj, const ScDPObject* pNewObj,
                     bool bMove);
 
diff --git a/sc/source/ui/undo/undoblk.cxx b/sc/source/ui/undo/undoblk.cxx
index df97db382da3..480c04d8e2ea 100644
--- a/sc/source/ui/undo/undoblk.cxx
+++ b/sc/source/ui/undo/undoblk.cxx
@@ -799,10 +799,10 @@ bool ScUndoDeleteMulti::CanRepeat(SfxRepeatTarget& 
rTarget) const
 
 ScUndoCut::ScUndoCut( ScDocShell* pNewDocShell,
                 ScRange aRange, const ScAddress& aOldEnd, const ScMarkData& 
rMark,
-                ScDocument* pNewUndoDoc ) :
+                ScDocumentUniquePtr pNewUndoDoc ) :
     ScBlockUndo( pNewDocShell, ScRange(aRange.aStart, aOldEnd), 
SC_UNDO_AUTOHEIGHT ),
     aMarkData( rMark ),
-    pUndoDoc( pNewUndoDoc ),
+    pUndoDoc( std::move(pNewUndoDoc) ),
     aExtendedRange( aRange )
 {
     SetChangeTrack();
@@ -899,14 +899,14 @@ bool ScUndoCut::CanRepeat(SfxRepeatTarget& rTarget) const
 
 ScUndoPaste::ScUndoPaste( ScDocShell* pNewDocShell, const ScRangeList& rRanges,
                 const ScMarkData& rMark,
-                ScDocument* pNewUndoDoc, ScDocument* pNewRedoDoc,
+                ScDocumentUniquePtr pNewUndoDoc, ScDocumentUniquePtr 
pNewRedoDoc,
                 InsertDeleteFlags nNewFlags,
                 ScRefUndoData* pRefData,
                 bool bRedoIsFilled, const ScUndoPasteOptions* pOptions ) :
     ScMultiBlockUndo( pNewDocShell, rRanges ),
     aMarkData( rMark ),
-    pUndoDoc( pNewUndoDoc ),
-    pRedoDoc( pNewRedoDoc ),
+    pUndoDoc( std::move(pNewUndoDoc) ),
+    pRedoDoc( std::move(pNewRedoDoc) ),
     nFlags( nNewFlags ),
     pRefUndoData( pRefData ),
     pRefRedoData( nullptr ),
@@ -1506,10 +1506,10 @@ bool ScUndoDragDrop::CanRepeat(SfxRepeatTarget& /* 
rTarget */) const
 // Insert list containing range names
 // (Insert|Name|Insert =>[List])
 ScUndoListNames::ScUndoListNames(ScDocShell* pNewDocShell, const ScRange& 
rRange,
-                                 ScDocument* pNewUndoDoc, ScDocument* 
pNewRedoDoc)
+                                 ScDocumentUniquePtr pNewUndoDoc, 
ScDocumentUniquePtr pNewRedoDoc)
     : ScBlockUndo(pNewDocShell, rRange, SC_UNDO_AUTOHEIGHT)
-    , xUndoDoc(pNewUndoDoc)
-    , xRedoDoc(pNewRedoDoc)
+    , xUndoDoc(std::move(pNewUndoDoc))
+    , xRedoDoc(std::move(pNewRedoDoc))
 {
 }
 
@@ -1557,10 +1557,10 @@ bool ScUndoListNames::CanRepeat(SfxRepeatTarget& 
rTarget) const
 }
 
 ScUndoConditionalFormat::ScUndoConditionalFormat(ScDocShell* pNewDocShell,
-        ScDocument* pUndoDoc, ScDocument* pRedoDoc, const ScRange& rRange):
+        ScDocumentUniquePtr pUndoDoc, ScDocumentUniquePtr pRedoDoc, const 
ScRange& rRange):
     ScSimpleUndo( pNewDocShell ),
-    mpUndoDoc(pUndoDoc),
-    mpRedoDoc(pRedoDoc),
+    mpUndoDoc(std::move(pUndoDoc)),
+    mpRedoDoc(std::move(pRedoDoc)),
     maRange(rRange)
 {
 }
@@ -1607,10 +1607,10 @@ bool 
ScUndoConditionalFormat::CanRepeat(SfxRepeatTarget& ) const
 }
 
 ScUndoConditionalFormatList::ScUndoConditionalFormatList(ScDocShell* 
pNewDocShell,
-        ScDocument* pUndoDoc, ScDocument* pRedoDoc, SCTAB nTab):
+        ScDocumentUniquePtr pUndoDoc, ScDocumentUniquePtr pRedoDoc, SCTAB 
nTab):
     ScSimpleUndo( pNewDocShell ),
-    mpUndoDoc(pUndoDoc),
-    mpRedoDoc(pRedoDoc),
+    mpUndoDoc(std::move(pUndoDoc)),
+    mpRedoDoc(std::move(pRedoDoc)),
     mnTab(nTab)
 {
 }
@@ -1669,10 +1669,10 @@ bool 
ScUndoConditionalFormatList::CanRepeat(SfxRepeatTarget& ) const
 ScUndoUseScenario::ScUndoUseScenario( ScDocShell* pNewDocShell,
                         const ScMarkData& rMark,
 /*C*/                   const ScArea& rDestArea,
-                              ScDocument* pNewUndoDoc,
+                              ScDocumentUniquePtr pNewUndoDoc,
                         const OUString& rNewName ) :
     ScSimpleUndo( pNewDocShell ),
-    pUndoDoc( pNewUndoDoc ),
+    pUndoDoc( std::move(pNewUndoDoc) ),
     aMarkData( rMark ),
     aName( rNewName )
 {
@@ -1789,10 +1789,10 @@ ScUndoSelectionStyle::ScUndoSelectionStyle( ScDocShell* 
pNewDocShell,
                                       const ScMarkData& rMark,
                                       const ScRange& rRange,
                                       const OUString& rName,
-                                            ScDocument* pNewUndoDoc ) :
+                                            ScDocumentUniquePtr pNewUndoDoc ) :
     ScSimpleUndo( pNewDocShell ),
     aMarkData( rMark ),
-    pUndoDoc( pNewUndoDoc ),
+    pUndoDoc( std::move(pNewUndoDoc) ),
     aStyleName( rName ),
     aRange( rRange )
 {
@@ -1890,9 +1890,9 @@ bool ScUndoSelectionStyle::CanRepeat(SfxRepeatTarget& 
rTarget) const
 }
 
 ScUndoEnterMatrix::ScUndoEnterMatrix( ScDocShell* pNewDocShell, const ScRange& 
rArea,
-                                      ScDocument* pNewUndoDoc, const OUString& 
rForm ) :
+                                      ScDocumentUniquePtr pNewUndoDoc, const 
OUString& rForm ) :
     ScBlockUndo( pNewDocShell, rArea, SC_UNDO_SIMPLE ),
-    pUndoDoc( pNewUndoDoc ),
+    pUndoDoc( std::move(pNewUndoDoc) ),
     aFormula( rForm )
 {
     SetChangeTrack();
@@ -1983,10 +1983,10 @@ static ScRange lcl_GetMultiMarkRange( const ScMarkData& 
rMark )
 }
 
 ScUndoIndent::ScUndoIndent( ScDocShell* pNewDocShell, const ScMarkData& rMark,
-                            ScDocument* pNewUndoDoc, bool bIncrement ) :
+                            ScDocumentUniquePtr pNewUndoDoc, bool bIncrement ) 
:
     ScBlockUndo( pNewDocShell, lcl_GetMultiMarkRange(rMark), 
SC_UNDO_AUTOHEIGHT ),
     aMarkData( rMark ),
-    pUndoDoc( pNewUndoDoc ),
+    pUndoDoc( std::move(pNewUndoDoc) ),
     bIsIncrement( bIncrement )
 {
 }
@@ -2039,10 +2039,10 @@ bool ScUndoIndent::CanRepeat(SfxRepeatTarget& rTarget) 
const
 }
 
 ScUndoTransliterate::ScUndoTransliterate( ScDocShell* pNewDocShell, const 
ScMarkData& rMark,
-                            ScDocument* pNewUndoDoc, TransliterationFlags 
nType ) :
+                            ScDocumentUniquePtr pNewUndoDoc, 
TransliterationFlags nType ) :
     ScBlockUndo( pNewDocShell, lcl_GetMultiMarkRange(rMark), 
SC_UNDO_AUTOHEIGHT ),
     aMarkData( rMark ),
-    pUndoDoc( pNewUndoDoc ),
+    pUndoDoc( std::move(pNewUndoDoc) ),
     nTransliterationType( nType )
 {
 }
diff --git a/sc/source/ui/undo/undoblk2.cxx b/sc/source/ui/undo/undoblk2.cxx
index 78a6d898a8c3..24f1ee9a24c0 100644
--- a/sc/source/ui/undo/undoblk2.cxx
+++ b/sc/source/ui/undo/undoblk2.cxx
@@ -38,7 +38,7 @@
 ScUndoWidthOrHeight::ScUndoWidthOrHeight( ScDocShell* pNewDocShell,
                 const ScMarkData& rMark,
                 SCCOLROW nNewStart, SCTAB nNewStartTab, SCCOLROW nNewEnd, 
SCTAB nNewEndTab,
-                ScDocument* pNewUndoDoc, const std::vector<sc::ColRowSpan>& 
rRanges,
+                ScDocumentUniquePtr pNewUndoDoc, const 
std::vector<sc::ColRowSpan>& rRanges,
                 std::unique_ptr<ScOutlineTable> pNewUndoTab,
                 ScSizeMode eNewMode, sal_uInt16 nNewSizeTwips, bool bNewWidth 
) :
     ScSimpleUndo( pNewDocShell ),
@@ -47,7 +47,7 @@ ScUndoWidthOrHeight::ScUndoWidthOrHeight( ScDocShell* 
pNewDocShell,
     nEnd( nNewEnd ),
     nStartTab( nNewStartTab ),
     nEndTab( nNewEndTab ),
-    pUndoDoc( pNewUndoDoc ),
+    pUndoDoc( std::move(pNewUndoDoc) ),
     pUndoTab( std::move(pNewUndoTab) ),
     maRanges(rRanges),
     nNewSize( nNewSizeTwips ),
diff --git a/sc/source/ui/undo/undodat.cxx b/sc/source/ui/undo/undodat.cxx
index 5bd7e6adf551..d0dec03b7fd8 100644
--- a/sc/source/ui/undo/undodat.cxx
+++ b/sc/source/ui/undo/undodat.cxx
@@ -52,13 +52,13 @@
 
 ScUndoDoOutline::ScUndoDoOutline( ScDocShell* pNewDocShell,
                             SCCOLROW nNewStart, SCCOLROW nNewEnd, SCTAB 
nNewTab,
-                            ScDocument* pNewUndoDoc, bool bNewColumns,
+                            ScDocumentUniquePtr pNewUndoDoc, bool bNewColumns,
                             sal_uInt16 nNewLevel, sal_uInt16 nNewEntry, bool 
bNewShow ) :
     ScSimpleUndo( pNewDocShell ),
     nStart( nNewStart ),
     nEnd( nNewEnd ),
     nTab( nNewTab ),
-    pUndoDoc( pNewUndoDoc ),
+    pUndoDoc( std::move(pNewUndoDoc) ),
     bColumns( bNewColumns ),
     nLevel( nNewLevel ),
     nEntry( nNewEntry ),
@@ -226,13 +226,13 @@ bool ScUndoMakeOutline::CanRepeat(SfxRepeatTarget& 
rTarget) const
 
 ScUndoOutlineLevel::ScUndoOutlineLevel( ScDocShell* pNewDocShell,
                         SCCOLROW nNewStart, SCCOLROW nNewEnd, SCTAB nNewTab,
-                        ScDocument* pNewUndoDoc, 
std::unique_ptr<ScOutlineTable> pNewUndoTab,
+                        ScDocumentUniquePtr pNewUndoDoc, 
std::unique_ptr<ScOutlineTable> pNewUndoTab,
                         bool bNewColumns, sal_uInt16 nNewLevel )
     : ScSimpleUndo(pNewDocShell)
     , nStart(nNewStart)
     , nEnd(nNewEnd)
     , nTab(nNewTab)
-    , xUndoDoc(pNewUndoDoc)
+    , xUndoDoc(std::move(pNewUndoDoc))
     , xUndoTable(std::move(pNewUndoTab))
     , bColumns(bNewColumns)
     , nLevel(nNewLevel)
@@ -308,11 +308,11 @@ bool ScUndoOutlineLevel::CanRepeat(SfxRepeatTarget& 
rTarget) const
 ScUndoOutlineBlock::ScUndoOutlineBlock( ScDocShell* pNewDocShell,
                         SCCOL nStartX, SCROW nStartY, SCTAB nStartZ,
                         SCCOL nEndX, SCROW nEndY, SCTAB nEndZ,
-                        ScDocument* pNewUndoDoc, 
std::unique_ptr<ScOutlineTable> pNewUndoTab, bool bNewShow ) :
+                        ScDocumentUniquePtr pNewUndoDoc, 
std::unique_ptr<ScOutlineTable> pNewUndoTab, bool bNewShow ) :
     ScSimpleUndo( pNewDocShell ),
     aBlockStart( nStartX, nStartY, nStartZ ),
     aBlockEnd( nEndX, nEndY, nEndZ ),
-    xUndoDoc(pNewUndoDoc),
+    xUndoDoc(std::move(pNewUndoDoc)),
     xUndoTable(std::move(pNewUndoTab)),
     bShow( bNewShow )
 {
@@ -408,11 +408,11 @@ bool ScUndoOutlineBlock::CanRepeat(SfxRepeatTarget& 
rTarget) const
 ScUndoRemoveAllOutlines::ScUndoRemoveAllOutlines(ScDocShell* pNewDocShell,
                                     SCCOL nStartX, SCROW nStartY, SCTAB 
nStartZ,
                                     SCCOL nEndX, SCROW nEndY, SCTAB nEndZ,
-                                    ScDocument* pNewUndoDoc, 
std::unique_ptr<ScOutlineTable> pNewUndoTab)
+                                    ScDocumentUniquePtr pNewUndoDoc, 
std::unique_ptr<ScOutlineTable> pNewUndoTab)
     : ScSimpleUndo(pNewDocShell)
     , aBlockStart(nStartX, nStartY, nStartZ)
     , aBlockEnd(nEndX, nEndY, nEndZ)
-    , xUndoDoc(pNewUndoDoc)
+    , xUndoDoc(std::move(pNewUndoDoc))
     , xUndoTable(std::move(pNewUndoTab))
 {
 }
@@ -489,11 +489,11 @@ bool ScUndoRemoveAllOutlines::CanRepeat(SfxRepeatTarget& 
rTarget) const
 ScUndoAutoOutline::ScUndoAutoOutline(ScDocShell* pNewDocShell,
                                      SCCOL nStartX, SCROW nStartY, SCTAB 
nStartZ,
                                      SCCOL nEndX, SCROW nEndY, SCTAB nEndZ,
-                                     ScDocument* pNewUndoDoc, 
std::unique_ptr<ScOutlineTable> pNewUndoTab)
+                                     ScDocumentUniquePtr pNewUndoDoc, 
std::unique_ptr<ScOutlineTable> pNewUndoTab)
     : ScSimpleUndo(pNewDocShell)
     , aBlockStart(nStartX, nStartY, nStartZ)
     , aBlockEnd(nEndX, nEndY, nEndZ)
-    , xUndoDoc(pNewUndoDoc)
+    , xUndoDoc(std::move(pNewUndoDoc))
     , xUndoTable(std::move(pNewUndoTab))
 {
 }
@@ -585,14 +585,14 @@ bool ScUndoAutoOutline::CanRepeat(SfxRepeatTarget& 
rTarget) const
 
 ScUndoSubTotals::ScUndoSubTotals(ScDocShell* pNewDocShell, SCTAB nNewTab,
                                  const ScSubTotalParam& rNewParam, SCROW 
nNewEndY,
-                                 ScDocument* pNewUndoDoc, 
std::unique_ptr<ScOutlineTable> pNewUndoTab,
+                                 ScDocumentUniquePtr pNewUndoDoc, 
std::unique_ptr<ScOutlineTable> pNewUndoTab,
                                  ScRangeName* pNewUndoRange, ScDBCollection* 
pNewUndoDB)
     : ScDBFuncUndo(pNewDocShell, ScRange(rNewParam.nCol1, rNewParam.nRow1, 
nNewTab,
                                          rNewParam.nCol2, rNewParam.nRow2, 
nNewTab))
     , nTab(nNewTab)
     , aParam(rNewParam)
     , nNewEndRow(nNewEndY)
-    , xUndoDoc(pNewUndoDoc)
+    , xUndoDoc(std::move(pNewUndoDoc))
     , xUndoTable(std::move(pNewUndoTab))
     , xUndoRange(pNewUndoRange)
     , xUndoDB(pNewUndoDB)
@@ -698,14 +698,14 @@ bool ScUndoSubTotals::CanRepeat(SfxRepeatTarget& /* 
rTarget */) const
 }
 
 ScUndoQuery::ScUndoQuery( ScDocShell* pNewDocShell, SCTAB nNewTab, const 
ScQueryParam& rParam,
-                            ScDocument* pNewUndoDoc, ScDBCollection* 
pNewUndoDB,
+                            ScDocumentUniquePtr pNewUndoDoc, ScDBCollection* 
pNewUndoDB,
                             const ScRange* pOld, bool bSize, const ScRange* 
pAdvSrc ) :
     ScDBFuncUndo( pNewDocShell, ScRange( rParam.nCol1, rParam.nRow1, nNewTab,
                                          rParam.nCol2, rParam.nRow2, nNewTab ) 
),
     pDrawUndo( nullptr ),
     nTab( nNewTab ),
     aQueryParam( rParam ),
-    xUndoDoc( pNewUndoDoc ),
+    xUndoDoc( std::move(pNewUndoDoc) ),
     xUndoDB( pNewUndoDB ),
     bIsAdvanced( false ),
     bDestArea( false ),
@@ -1019,15 +1019,15 @@ bool ScUndoDBData::CanRepeat(SfxRepeatTarget& /* 
rTarget */) const
 ScUndoImportData::ScUndoImportData( ScDocShell* pNewDocShell, SCTAB nNewTab,
                                 const ScImportParam& rParam, SCCOL nNewEndX, 
SCROW nNewEndY,
                                 SCCOL nNewFormula,
-                                ScDocument* pNewUndoDoc, ScDocument* 
pNewRedoDoc,
+                                ScDocumentUniquePtr pNewUndoDoc, 
ScDocumentUniquePtr pNewRedoDoc,
                                 ScDBData* pNewUndoData, ScDBData* pNewRedoData 
) :
     ScSimpleUndo( pNewDocShell ),
     nTab( nNewTab ),
     aImportParam( rParam ),
     nEndCol( nNewEndX ),
     nEndRow( nNewEndY ),
-    xUndoDoc(pNewUndoDoc),
-    xRedoDoc(pNewRedoDoc),
+    xUndoDoc(std::move(pNewUndoDoc)),
+    xRedoDoc(std::move(pNewRedoDoc)),
     xUndoDBData(pNewUndoData),
     xRedoDBData(pNewRedoData),
     nFormulaCols( nNewFormula ),
@@ -1223,7 +1223,7 @@ bool ScUndoImportData::CanRepeat(SfxRepeatTarget& 
rTarget) const
 ScUndoRepeatDB::ScUndoRepeatDB( ScDocShell* pNewDocShell, SCTAB nNewTab,
                                 SCCOL nStartX, SCROW nStartY, SCCOL nEndX, 
SCROW nEndY,
                                 SCROW nResultEndRow, SCCOL nCurX, SCROW nCurY,
-                                ScDocument* pNewUndoDoc, ScOutlineTable* 
pNewUndoTab,
+                                ScDocumentUniquePtr pNewUndoDoc, 
ScOutlineTable* pNewUndoTab,
                                 ScRangeName* pNewUndoRange, ScDBCollection* 
pNewUndoDB,
                                 const ScRange* pOldQ, const ScRange* pNewQ ) :
     ScSimpleUndo( pNewDocShell ),
@@ -1231,7 +1231,7 @@ ScUndoRepeatDB::ScUndoRepeatDB( ScDocShell* pNewDocShell, 
SCTAB nNewTab,
     aBlockEnd( nEndX,nEndY,nNewTab ),
     nNewEndRow( nResultEndRow ),
     aCursorPos( nCurX,nCurY,nNewTab ),
-    xUndoDoc(pNewUndoDoc),
+    xUndoDoc(std::move(pNewUndoDoc)),
     xUndoTable(pNewUndoTab),
     xUndoRange(pNewUndoRange),
     xUndoDB(pNewUndoDB),
@@ -1379,11 +1379,11 @@ bool ScUndoRepeatDB::CanRepeat(SfxRepeatTarget& 
rTarget) const
 }
 
 ScUndoDataPilot::ScUndoDataPilot( ScDocShell* pNewDocShell,
-                            ScDocument* pOldDoc, ScDocument* pNewDoc,
+                            ScDocumentUniquePtr pOldDoc, ScDocumentUniquePtr 
pNewDoc,
                             const ScDPObject* pOldObj, const ScDPObject* 
pNewObj, bool bMove )
     : ScSimpleUndo(pNewDocShell)
-    , xOldUndoDoc(pOldDoc)
-    , xNewUndoDoc(pNewDoc)
+    , xOldUndoDoc(std::move(pOldDoc))
+    , xNewUndoDoc(std::move(pNewDoc))
     , bAllowMove( bMove)
 {
     if (pOldObj)
diff --git a/sc/source/ui/unoobj/cellsuno.cxx b/sc/source/ui/unoobj/cellsuno.cxx
index 645c6bc4aefd..e957cb22faef 100644
--- a/sc/source/ui/unoobj/cellsuno.cxx
+++ b/sc/source/ui/unoobj/cellsuno.cxx
@@ -1126,10 +1126,10 @@ static bool lcl_PutDataArray( ScDocShell& rDocShell, 
const ScRange& rRange,
         return false;
     }
 
-    ScDocument* pUndoDoc = nullptr;
+    ScDocumentUniquePtr pUndoDoc;
     if ( bUndo )
     {
-        pUndoDoc = new ScDocument( SCDOCMODE_UNDO );
+        pUndoDoc.reset(new ScDocument( SCDOCMODE_UNDO ));
         pUndoDoc->InitUndo( &rDoc, nTab, nTab );
         rDoc.CopyToDocument(rRange, 
InsertDeleteFlags::CONTENTS|InsertDeleteFlags::NOCAPTIONS, false, *pUndoDoc);
     }
@@ -1224,7 +1224,7 @@ static bool lcl_PutDataArray( ScDocShell& rDocShell, 
const ScRange& rRange,
         rDocShell.GetUndoManager()->AddUndoAction(
             new ScUndoPaste(
                 &rDocShell, ScRange(nStartCol, nStartRow, nTab, nEndCol, 
nEndRow, nTab),
-                aDestMark, pUndoDoc, nullptr, InsertDeleteFlags::CONTENTS, 
nullptr, false));
+                aDestMark, std::move(pUndoDoc), nullptr, 
InsertDeleteFlags::CONTENTS, nullptr, false));
     }
 
     if (!bHeight)
@@ -1265,10 +1265,10 @@ static bool lcl_PutFormulaArray( ScDocShell& rDocShell, 
const ScRange& rRange,
         return false;
     }
 
-    ScDocument* pUndoDoc = nullptr;
+    ScDocumentUniquePtr pUndoDoc;
     if ( bUndo )
     {
-        pUndoDoc = new ScDocument( SCDOCMODE_UNDO );
+        pUndoDoc.reset(new ScDocument( SCDOCMODE_UNDO ));
         pUndoDoc->InitUndo( &rDoc, nTab, nTab );
         rDoc.CopyToDocument(rRange, InsertDeleteFlags::CONTENTS, false, 
*pUndoDoc);
     }
@@ -1325,7 +1325,7 @@ static bool lcl_PutFormulaArray( ScDocShell& rDocShell, 
const ScRange& rRange,
         rDocShell.GetUndoManager()->AddUndoAction(
             new ScUndoPaste( &rDocShell,
                 ScRange(nStartCol, nStartRow, nTab, nEndCol, nEndRow, nTab), 
aDestMark,
-                pUndoDoc, nullptr, InsertDeleteFlags::CONTENTS, nullptr, 
false));
+                std::move(pUndoDoc), nullptr, InsertDeleteFlags::CONTENTS, 
nullptr, false));
     }
 
     if (!bHeight)
diff --git a/sc/source/ui/view/dbfunc3.cxx b/sc/source/ui/view/dbfunc3.cxx
index b00359412132..57608b8bb091 100644
--- a/sc/source/ui/view/dbfunc3.cxx
+++ b/sc/source/ui/view/dbfunc3.cxx
@@ -437,7 +437,7 @@ void ScDBFunc::DoSubTotals( const ScSubTotalParam& rParam, 
bool bRecord,
         ScDocShellModificator aModificator( *pDocSh );
 
         ScSubTotalParam aNewParam( rParam );        // change end of range
-        ScDocument*     pUndoDoc = nullptr;
+        ScDocumentUniquePtr pUndoDoc;
         std::unique_ptr<ScOutlineTable> pUndoTab;
         ScRangeName*    pUndoRange = nullptr;
         ScDBCollection* pUndoDB = nullptr;
@@ -446,7 +446,7 @@ void ScDBFunc::DoSubTotals( const ScSubTotalParam& rParam, 
bool bRecord,
         {
             bool bOldFilter = bDo && rParam.bDoSort;
             SCTAB nTabCount = rDoc.GetTableCount();
-            pUndoDoc = new ScDocument( SCDOCMODE_UNDO );
+            pUndoDoc.reset(new ScDocument( SCDOCMODE_UNDO ));
             ScOutlineTable* pTable = rDoc.GetOutlineTable( nTab );
             if (pTable)
             {
@@ -526,7 +526,7 @@ void ScDBFunc::DoSubTotals( const ScSubTotalParam& rParam, 
bool bRecord,
             pDocSh->GetUndoManager()->AddUndoAction(
                 new ScUndoSubTotals( pDocSh, nTab,
                                         rParam, aNewParam.nRow2,
-                                        pUndoDoc, std::move(pUndoTab), // 
pUndoDBData,
+                                        std::move(pUndoDoc), 
std::move(pUndoTab), // pUndoDBData,
                                         pUndoRange, pUndoDB ) );
         }
 
@@ -2130,7 +2130,7 @@ void ScDBFunc::RepeatDB( bool bRecord )
 
         //! undo only needed data ?
 
-        ScDocument* pUndoDoc = nullptr;
+        ScDocumentUniquePtr pUndoDoc;
         ScOutlineTable* pUndoTab = nullptr;
         ScRangeName* pUndoRange = nullptr;
         ScDBCollection* pUndoDB = nullptr;
@@ -2138,7 +2138,7 @@ void ScDBFunc::RepeatDB( bool bRecord )
         if (bRecord)
         {
             SCTAB nTabCount = pDoc->GetTableCount();
-            pUndoDoc = new ScDocument( SCDOCMODE_UNDO );
+            pUndoDoc.reset(new ScDocument( SCDOCMODE_UNDO ));
             ScOutlineTable* pTable = pDoc->GetOutlineTable( nTab );
             if (pTable)
             {
@@ -2235,7 +2235,7 @@ void ScDBFunc::RepeatDB( bool bRecord )
                                         nStartCol, nStartRow, nEndCol, nEndRow,
                                         nNewEndRow,
                                         nCurX, nCurY,
-                                        pUndoDoc, pUndoTab,
+                                        std::move(pUndoDoc), pUndoTab,
                                         pUndoRange, pUndoDB,
                                         pOld, pNew ) );
         }
diff --git a/sc/source/ui/view/viewfun3.cxx b/sc/source/ui/view/viewfun3.cxx
index d052717e7a57..0453c0c0bd85 100644
--- a/sc/source/ui/view/viewfun3.cxx
+++ b/sc/source/ui/view/viewfun3.cxx
@@ -113,10 +113,10 @@ void ScViewFunc::CutToClip()
         ScAddress aOldEnd( aRange.aEnd );       //  combined cells in this 
range?
         pDoc->ExtendMerge( aRange, true );
 
-        ScDocument* pUndoDoc = nullptr;
+        ScDocumentUniquePtr pUndoDoc;
         if ( bRecord )
         {
-            pUndoDoc = new ScDocument( SCDOCMODE_UNDO );
+            pUndoDoc.reset(new ScDocument( SCDOCMODE_UNDO ));
             pUndoDoc->InitUndoSelected( pDoc, rMark );
             // all sheets - CopyToDocument skips those that don't exist in 
pUndoDoc
             ScRange aCopyRange = aRange;
@@ -139,7 +139,7 @@ void ScViewFunc::CutToClip()
 
         if ( bRecord )                          // Draw-Undo now available
             pDocSh->GetUndoManager()->AddUndoAction(
-                new ScUndoCut( pDocSh, aRange, aOldEnd, rMark, pUndoDoc ) );
+                new ScUndoCut( pDocSh, aRange, aOldEnd, rMark, 
std::move(pUndoDoc) ) );
 
         aModificator.SetDocumentModified();
         pDocSh->UpdateOle(&GetViewData());
@@ -1241,13 +1241,13 @@ bool ScViewFunc::PasteFromClip( InsertDeleteFlags 
nFlags, ScDocument* pClipDoc,
     bool bColInfo = ( nStartRow==0 && nEndRow==MAXROW );
     bool bRowInfo = ( nStartCol==0 && nEndCol==MAXCOL );
 
-    ScDocument* pUndoDoc    = nullptr;
+    ScDocumentUniquePtr pUndoDoc;
     ScDocument* pRefUndoDoc = nullptr;
     ScRefUndoData* pUndoData = nullptr;
 
     if ( bRecord )
     {
-        pUndoDoc = new ScDocument( SCDOCMODE_UNDO );
+        pUndoDoc.reset(new ScDocument( SCDOCMODE_UNDO ));
         pUndoDoc->InitUndoSelected( pDoc, aFilteredMark, bColInfo, bRowInfo );
 
         // all sheets - CopyToDocument skips those that don't exist in pUndoDoc
@@ -1376,20 +1376,20 @@ bool ScViewFunc::PasteFromClip( InsertDeleteFlags 
nFlags, ScDocument* pClipDoc,
 
     if ( bRecord )
     {
-        ScDocument* pRedoDoc    = nullptr;
+        ScDocumentUniquePtr pRedoDoc;
         // copy redo data after appearance of the first undo
         // don't create Redo-Doc without RefUndoDoc
 
         if (pRefUndoDoc)
         {
-            pRedoDoc = new ScDocument( SCDOCMODE_UNDO );
+            pRedoDoc.reset(new ScDocument( SCDOCMODE_UNDO ));
             pRedoDoc->InitUndo( pDoc, nStartTab, nEndTab, bColInfo, bRowInfo );
 
             //      move adapted refs to Redo-Doc
 
             SCTAB nTabCount = pDoc->GetTableCount();
             pRedoDoc->AddUndoTab( 0, nTabCount-1 );
-            pDoc->CopyUpdated( pRefUndoDoc, pRedoDoc );
+            pDoc->CopyUpdated( pRefUndoDoc, pRedoDoc.get() );
 
             //      move old refs to Undo-Doc
 
@@ -1413,7 +1413,7 @@ bool ScViewFunc::PasteFromClip( InsertDeleteFlags nFlags, 
ScDocument* pClipDoc,
 
         SfxUndoAction* pUndo = new ScUndoPaste(
             pDocSh, ScRange(nStartCol, nStartRow, nStartTab, nUndoEndCol, 
nUndoEndRow, nEndTab),
-            aFilteredMark, pUndoDoc, pRedoDoc, nFlags | nUndoFlags, pUndoData,
+            aFilteredMark, std::move(pUndoDoc), std::move(pRedoDoc), nFlags | 
nUndoFlags, pUndoData,
             false, &aOptions );     // false = Redo data not yet copied
 
         if ( bInsertCells )
@@ -1611,7 +1611,7 @@ bool ScViewFunc::PasteMultiRangesFromClip(
         aOptions.eMoveMode  = eMoveMode;
 
         ScUndoPaste* pUndo = new ScUndoPaste(pDocSh,
-            aMarkedRange, aMark, pUndoDoc.release(), nullptr, 
nFlags|nUndoFlags, nullptr, false, &aOptions);
+            aMarkedRange, aMark, std::move(pUndoDoc), nullptr, 
nFlags|nUndoFlags, nullptr, false, &aOptions);
 
         if (bInsertCells)
             pUndoMgr->AddUndoAction(new ScUndoWrapper(pUndo), true);
@@ -1776,7 +1776,7 @@ bool ScViewFunc::PasteFromClipToMultiRanges(
         aOptions.eMoveMode  = eMoveMode;
 
         ScUndoPaste* pUndo = new ScUndoPaste(
-            pDocSh, aRanges, aMark, pUndoDoc.release(), nullptr, 
nFlags|nUndoFlags, nullptr, false, &aOptions);
+            pDocSh, aRanges, aMark, std::move(pUndoDoc), nullptr, 
nFlags|nUndoFlags, nullptr, false, &aOptions);
 
         pUndoMgr->AddUndoAction(pUndo);
         pUndoMgr->LeaveListAction();
diff --git a/sc/source/ui/view/viewfun4.cxx b/sc/source/ui/view/viewfun4.cxx
index dbcf8352622e..df21a4940e50 100644
--- a/sc/source/ui/view/viewfun4.cxx
+++ b/sc/source/ui/view/viewfun4.cxx
@@ -112,10 +112,10 @@ void ScViewFunc::PasteRTF( SCCOL nStartCol, SCROW 
nStartRow,
             if (nEndRow > MAXROW)
                 nEndRow = MAXROW;
 
-            ScDocument* pUndoDoc = nullptr;
+            ScDocumentUniquePtr pUndoDoc;
             if (bRecord)
             {
-                pUndoDoc = new ScDocument( SCDOCMODE_UNDO );
+                pUndoDoc.reset(new ScDocument( SCDOCMODE_UNDO ));
                 pUndoDoc->InitUndo( &rDoc, nTab, nTab );
                 rDoc.CopyToDocument( nStartCol,nStartRow,nTab, 
nStartCol,nEndRow,nTab, InsertDeleteFlags::ALL, false, *pUndoDoc );
             }
@@ -136,7 +136,7 @@ void ScViewFunc::PasteRTF( SCCOL nStartCol, SCROW nStartRow,
 
             if (bRecord)
             {
-                ScDocument* pRedoDoc = new ScDocument( SCDOCMODE_UNDO );
+                ScDocumentUniquePtr pRedoDoc(new ScDocument( SCDOCMODE_UNDO ));
                 pRedoDoc->InitUndo( &rDoc, nTab, nTab );
                 rDoc.CopyToDocument( nStartCol,nStartRow,nTab, 
nStartCol,nEndRow,nTab, InsertDeleteFlags::ALL|InsertDeleteFlags::NOCAPTIONS, 
false, *pRedoDoc );
 
@@ -145,7 +145,7 @@ void ScViewFunc::PasteRTF( SCCOL nStartCol, SCROW nStartRow,
                 aDestMark.SetMarkArea( aMarkRange );
                 pDocSh->GetUndoManager()->AddUndoAction(
                     new ScUndoPaste( pDocSh, aMarkRange, aDestMark,
-                                     pUndoDoc, pRedoDoc, 
InsertDeleteFlags::ALL, nullptr));
+                                     std::move(pUndoDoc), std::move(pRedoDoc), 
InsertDeleteFlags::ALL, nullptr));
             }
         }
 
diff --git a/sc/source/ui/view/viewfunc.cxx b/sc/source/ui/view/viewfunc.cxx
index e433d96fd2ab..7ba426a61828 100644
--- a/sc/source/ui/view/viewfunc.cxx
+++ b/sc/source/ui/view/viewfunc.cxx
@@ -1353,7 +1353,7 @@ void ScViewFunc::SetStyleSheetToMarked( const 
SfxStyleSheet* pStyleSheet )
         if ( bRecord )
         {
             SCTAB nTab = rViewData.GetTabNo();
-            ScDocument* pUndoDoc = new ScDocument( SCDOCMODE_UNDO );
+            ScDocumentUniquePtr pUndoDoc(new ScDocument( SCDOCMODE_UNDO ));
             pUndoDoc->InitUndo( &rDoc, nTab, nTab );
             ScMarkData::iterator itr = aFuncMark.begin(), itrEnd = 
aFuncMark.end();
             for (; itr != itrEnd; ++itr)
@@ -1368,7 +1368,7 @@ void ScViewFunc::SetStyleSheetToMarked( const 
SfxStyleSheet* pStyleSheet )
 
             OUString aName = pStyleSheet->GetName();
             pDocSh->GetUndoManager()->AddUndoAction(
-                new ScUndoSelectionStyle( pDocSh, aFuncMark, aMarkRange, 
aName, pUndoDoc ) );
+                new ScUndoSelectionStyle( pDocSh, aFuncMark, aMarkRange, 
aName, std::move(pUndoDoc) ) );
         }
 
         rDoc.ApplySelectionStyle( static_cast<const 
ScStyleSheet&>(*pStyleSheet), aFuncMark );
@@ -1386,7 +1386,7 @@ void ScViewFunc::SetStyleSheetToMarked( const 
SfxStyleSheet* pStyleSheet )
 
         if ( bRecord )
         {
-            ScDocument* pUndoDoc = new ScDocument( SCDOCMODE_UNDO );
+            ScDocumentUniquePtr pUndoDoc(new ScDocument( SCDOCMODE_UNDO ));
             pUndoDoc->InitUndo( &rDoc, nTab, nTab );
             ScMarkData::iterator itr = aFuncMark.begin(), itrEnd = 
aFuncMark.end();
             for (; itr != itrEnd; ++itr)
@@ -1402,7 +1402,7 @@ void ScViewFunc::SetStyleSheetToMarked( const 
SfxStyleSheet* pStyleSheet )
 
             OUString aName = pStyleSheet->GetName();
             pDocSh->GetUndoManager()->AddUndoAction(
-                new ScUndoSelectionStyle( pDocSh, aUndoMark, aMarkRange, 
aName, pUndoDoc ) );
+                new ScUndoSelectionStyle( pDocSh, aUndoMark, aMarkRange, 
aName, std::move(pUndoDoc) ) );
         }
 
         ScMarkData::iterator itr = aFuncMark.begin(), itrEnd = aFuncMark.end();
@@ -2062,7 +2062,7 @@ void ScViewFunc::SetWidthOrHeight(
         bFormula = rOpts.GetOption( VOPT_FORMULAS );
     }
 
-    ScDocument*     pUndoDoc = nullptr;
+    ScDocumentUniquePtr pUndoDoc;
     std::unique_ptr<ScOutlineTable> pUndoTab;
     std::vector<sc::ColRowSpan> aUndoRanges;
 
@@ -2070,7 +2070,7 @@ void ScViewFunc::SetWidthOrHeight(
     {
         rDoc.BeginDrawUndo();                          // Drawing Updates
 
-        pUndoDoc = new ScDocument( SCDOCMODE_UNDO );
+        pUndoDoc.reset(new ScDocument( SCDOCMODE_UNDO ));
         itr = aMarkData.begin();
         for (; itr != itrEnd; ++itr)
         {
@@ -2232,7 +2232,7 @@ void ScViewFunc::SetWidthOrHeight(
         pDocSh->GetUndoManager()->AddUndoAction(
             new ScUndoWidthOrHeight(
                 pDocSh, aMarkData, nStart, nCurTab, nEnd, nCurTab,
-                pUndoDoc, aUndoRanges, std::move(pUndoTab), eMode, nSizeTwips, 
bWidth));
+                std::move(pUndoDoc), aUndoRanges, std::move(pUndoTab), eMode, 
nSizeTwips, bWidth));
     }
 
     if (nCurX < 0)
_______________________________________________
Libreoffice-commits mailing list
libreoffice-comm...@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits

Reply via email to