[Libreoffice-commits] core.git: sw/inc sw/source

Noel Grandin noel.grandin at collabora.co.uk
Tue Jul 10 10:28:20 UTC 2018


 sw/inc/SwUndoField.hxx                       |    2 
 sw/inc/authfld.hxx                           |    2 
 sw/inc/chpfld.hxx                            |    2 
 sw/inc/dbfld.hxx                             |   10 +--
 sw/inc/ddefld.hxx                            |    2 
 sw/inc/docufld.hxx                           |   32 ++++-----
 sw/inc/expfld.hxx                            |    8 +-
 sw/inc/fldbas.hxx                            |    4 -
 sw/inc/flddat.hxx                            |    2 
 sw/inc/flddropdown.hxx                       |    2 
 sw/inc/fmtfld.hxx                            |    8 +-
 sw/inc/reffld.hxx                            |    2 
 sw/inc/usrfld.hxx                            |    2 
 sw/source/core/doc/DocumentFieldsManager.cxx |    4 -
 sw/source/core/fields/authfld.cxx            |    4 -
 sw/source/core/fields/chpfld.cxx             |    8 +-
 sw/source/core/fields/dbfld.cxx              |   36 +++++-----
 sw/source/core/fields/ddefld.cxx             |    5 -
 sw/source/core/fields/docufld.cxx            |   90 ++++++++++++---------------
 sw/source/core/fields/expfld.cxx             |   24 +++----
 sw/source/core/fields/fldbas.cxx             |    4 -
 sw/source/core/fields/flddat.cxx             |    8 +-
 sw/source/core/fields/flddropdown.cxx        |    5 -
 sw/source/core/fields/macrofld.cxx           |    5 -
 sw/source/core/fields/reffld.cxx             |    8 +-
 sw/source/core/fields/scrptfld.cxx           |    5 -
 sw/source/core/fields/tblcalc.cxx            |    8 +-
 sw/source/core/fields/usrfld.cxx             |    4 -
 sw/source/core/txtnode/atrfld.cxx            |   22 +++---
 sw/source/core/undo/SwUndoField.cxx          |    2 
 sw/source/core/unocore/unofield.cxx          |    4 -
 sw/source/filter/ww8/docxattributeoutput.cxx |    2 
 sw/source/ui/dbui/dbinsdlg.cxx               |    2 
 sw/source/ui/fldui/DropDownFieldDialog.cxx   |    2 
 sw/source/ui/fldui/fldpage.cxx               |   10 +--
 sw/source/uibase/docvw/AnnotationWin.cxx     |    4 -
 sw/source/uibase/fldui/fldmgr.cxx            |   30 +++------
 sw/source/uibase/inc/fldmgr.hxx              |    5 -
 38 files changed, 183 insertions(+), 196 deletions(-)

New commits:
commit 1a33947a91685808fd5f6d4903b6ae896686066d
Author: Noel Grandin <noel.grandin at collabora.co.uk>
Date:   Wed Jul 4 10:07:46 2018 +0200

    pass SwField around by std::unique_ptr
    
    Fix leak in SwFieldPage::InsertField, where it interacts badly with
    SwFieldMgr::UpdateCurField
    
    The awkward
       return std::unique_ptr<SwField>(pTmp.release());
    code is because clang 3.8.0 doesn't seem able to do auto-upcast of
    std::unique_ptr.
    
    Change-Id: I05b748e9cda145fd972a01bfa343239df32368b9
    Reviewed-on: https://gerrit.libreoffice.org/57197
    Tested-by: Jenkins
    Reviewed-by: Noel Grandin <noel.grandin at collabora.co.uk>

diff --git a/sw/inc/SwUndoField.hxx b/sw/inc/SwUndoField.hxx
index e9ef5d8dcb38..f27aed1a830c 100644
--- a/sw/inc/SwUndoField.hxx
+++ b/sw/inc/SwUndoField.hxx
@@ -43,7 +43,7 @@ public:
 
 class SwUndoFieldFromDoc : public SwUndoField
 {
-    SwField * pOldField, * pNewField;
+    std::unique_ptr<SwField> pOldField, pNewField;
     SwMsgPoolItem * pHint;
     bool bUpdate;
 
diff --git a/sw/inc/authfld.hxx b/sw/inc/authfld.hxx
index ad50de2487fc..60e7f1533100 100644
--- a/sw/inc/authfld.hxx
+++ b/sw/inc/authfld.hxx
@@ -148,7 +148,7 @@ class SwAuthorityField : public SwField
     mutable sal_IntPtr  m_nTempSequencePos;
 
     virtual OUString    Expand() const override;
-    virtual SwField*    Copy() const override;
+    virtual std::unique_ptr<SwField> Copy() const override;
 
 public:
     /// For internal use only, in general continue using ExpandField() instead.
diff --git a/sw/inc/chpfld.hxx b/sw/inc/chpfld.hxx
index 561836850c5d..695a21c395c4 100644
--- a/sw/inc/chpfld.hxx
+++ b/sw/inc/chpfld.hxx
@@ -55,7 +55,7 @@ class SW_DLLPUBLIC SwChapterField : public SwField
     OUString sPost;
 
     virtual OUString Expand() const override;
-    virtual SwField* Copy() const override;
+    virtual std::unique_ptr<SwField> Copy() const override;
 
 public:
     SwChapterField(SwChapterFieldType*, sal_uInt32 nFormat = 0);
diff --git a/sw/inc/dbfld.hxx b/sw/inc/dbfld.hxx
index 214d3d9acec3..5d2c51af7880 100644
--- a/sw/inc/dbfld.hxx
+++ b/sw/inc/dbfld.hxx
@@ -63,7 +63,7 @@ class SW_DLLPUBLIC SwDBField : public SwValueField
     bool    bInitialized    : 1;
 
     virtual OUString    Expand() const override;
-    virtual SwField*    Copy() const override;
+    virtual std::unique_ptr<SwField> Copy() const override;
 
 public:
     SwDBField(SwDBFieldType*, sal_uInt32 nFormat = 0);
@@ -165,7 +165,7 @@ public:
                       const OUString& rCond, const SwDBData& rDBData);
 
     virtual OUString        Expand() const override;
-    virtual SwField*        Copy() const override;
+    virtual std::unique_ptr<SwField> Copy() const override;
 
     void                    Evaluate(SwDoc const *);
     inline void             SetCondValid(bool bCond);
@@ -205,7 +205,7 @@ public:
     SwDBNumSetField(SwDBNumSetFieldType*, const OUString& rCond, const OUString& rDBNum, const SwDBData& rDBData);
 
     virtual OUString        Expand() const override;
-    virtual SwField*        Copy() const override;
+    virtual std::unique_ptr<SwField> Copy() const override;
 
     inline bool             IsCondValid() const;
     inline void             SetCondValid(bool bCond);
@@ -248,7 +248,7 @@ public:
     SwDBNameField(SwDBNameFieldType*, const SwDBData& rDBData);
 
     virtual OUString Expand() const override;
-    virtual SwField* Copy() const override;
+    virtual std::unique_ptr<SwField> Copy() const override;
     virtual bool        QueryValue( css::uno::Any& rVal, sal_uInt16 nWhich ) const override;
     virtual bool        PutValue( const css::uno::Any& rVal, sal_uInt16 nWhich ) override;
 };
@@ -270,7 +270,7 @@ public:
     SwDBSetNumberField(SwDBSetNumberFieldType*, const SwDBData& rDBData, sal_uInt32 nFormat = 0);
 
     virtual OUString Expand() const override;
-    virtual         SwField* Copy() const override;
+    virtual std::unique_ptr<SwField> Copy() const override;
     void            Evaluate(SwDoc const *);
 
     inline long     GetSetNumber() const;
diff --git a/sw/inc/ddefld.hxx b/sw/inc/ddefld.hxx
index 0bd96a8cbccb..510f6223874e 100644
--- a/sw/inc/ddefld.hxx
+++ b/sw/inc/ddefld.hxx
@@ -106,7 +106,7 @@ class SwDDEField : public SwField
 {
 private:
     virtual OUString Expand() const override;
-    virtual SwField* Copy() const override;
+    virtual std::unique_ptr<SwField> Copy() const override;
 
 public:
     SwDDEField(SwDDEFieldType*);
diff --git a/sw/inc/docufld.hxx b/sw/inc/docufld.hxx
index 8afc0a1e425a..5a6545f712b6 100644
--- a/sw/inc/docufld.hxx
+++ b/sw/inc/docufld.hxx
@@ -157,7 +157,7 @@ public:
             sal_uInt16 const nMaxPage);
 
     virtual OUString    Expand() const override;
-    virtual SwField*    Copy() const override;
+    virtual std::unique_ptr<SwField> Copy() const override;
 
     virtual OUString GetPar2() const override;
     virtual void        SetPar2(const OUString& rStr) override;
@@ -187,7 +187,7 @@ public:
     SwAuthorField(SwAuthorFieldType*, sal_uInt32 nFormat);
 
     virtual OUString    Expand() const override;
-    virtual SwField*    Copy() const override;
+    virtual std::unique_ptr<SwField> Copy() const override;
 
     void         SetExpansion(const OUString& rStr) { m_aContent = rStr; }
 
@@ -213,7 +213,7 @@ public:
     SwFileNameField(SwFileNameFieldType*, sal_uInt32 nFormat);
 
     virtual OUString    Expand() const override;
-    virtual SwField*    Copy() const override;
+    virtual std::unique_ptr<SwField> Copy() const override;
 
     void         SetExpansion(const OUString& rStr) { m_aContent = rStr; }
 
@@ -237,7 +237,7 @@ public:
     SwTemplNameField(SwTemplNameFieldType*, sal_uInt32 nFormat);
 
     virtual OUString    Expand() const override;
-    virtual SwField*    Copy() const override;
+    virtual std::unique_ptr<SwField> Copy() const override;
     virtual bool        QueryValue( css::uno::Any& rVal, sal_uInt16 nWhich ) const override;
     virtual bool        PutValue( const css::uno::Any& rVal, sal_uInt16 nWhich ) override;
 };
@@ -267,7 +267,7 @@ public:
     void ChangeExpansion( const SwFrame* pFrame );
 
     virtual OUString    Expand() const override;
-    virtual SwField*    Copy() const override;
+    virtual std::unique_ptr<SwField> Copy() const override;
 
     virtual sal_uInt16      GetSubType() const override;
     virtual void        SetSubType(sal_uInt16 nSub) override;
@@ -301,7 +301,7 @@ class SW_DLLPUBLIC SwHiddenTextField : public SwField
     bool m_bValid : 1; ///< Is DB-field evaluated?
 
     virtual OUString    Expand() const override;
-    virtual SwField*    Copy() const override;
+    virtual std::unique_ptr<SwField> Copy() const override;
 
 public:
     SwHiddenTextField( SwHiddenTextFieldType*,
@@ -362,7 +362,7 @@ public:
     SwHiddenParaField(SwHiddenParaFieldType*, const OUString& rCond);
 
     virtual OUString    Expand() const override;
-    virtual SwField*    Copy() const override;
+    virtual std::unique_ptr<SwField> Copy() const override;
 
     void                SetHidden(bool bHidden)     { m_bIsHidden = bHidden; }
     bool                IsHidden() const            { return m_bIsHidden;    }
@@ -391,7 +391,7 @@ class SW_DLLPUBLIC SwMacroField : public SwField
     bool      m_bIsScriptURL;
 
     virtual OUString Expand() const override;
-    virtual SwField* Copy() const override;
+    virtual std::unique_ptr<SwField> Copy() const override;
 
 public:
     /// Direct input, delete old value.
@@ -465,7 +465,7 @@ public:
     virtual ~SwPostItField() override;
 
     virtual OUString        Expand() const override;
-    virtual SwField*        Copy() const override;
+    virtual std::unique_ptr<SwField> Copy() const override;
 
     const DateTime&         GetDateTime() const             { return m_aDateTime; }
     const Date       GetDate() const                 { return Date(m_aDateTime.GetDate()); }
@@ -511,7 +511,7 @@ class SW_DLLPUBLIC SwDocInfoField : public SwValueField
     OUString  m_aName;
 
     virtual OUString        Expand() const override;
-    virtual SwField*        Copy() const override;
+    virtual std::unique_ptr<SwField> Copy() const override;
 
 public:
     SwDocInfoField(SwDocInfoFieldType*, sal_uInt16 nSub, const OUString& rName, sal_uInt32 nFormat=0);
@@ -547,7 +547,7 @@ public:
     SwExtUserField(SwExtUserFieldType*, sal_uInt16 nSub, sal_uInt32 nFormat);
 
     virtual OUString    Expand() const override;
-    virtual SwField*    Copy() const override;
+    virtual std::unique_ptr<SwField> Copy() const override;
 
     virtual sal_uInt16      GetSubType() const override;
     virtual void        SetSubType(sal_uInt16 nSub) override;
@@ -581,7 +581,7 @@ public:
     SwRefPageSetField( SwRefPageSetFieldType*, short nOff, bool bOn );
 
     virtual OUString    Expand() const override;
-    virtual SwField*    Copy() const override;
+    virtual std::unique_ptr<SwField> Copy() const override;
 
     virtual OUString  GetPar2() const override;
     virtual void    SetPar2(const OUString& rStr) override;
@@ -619,7 +619,7 @@ public:
     SwRefPageGetField( SwRefPageGetFieldType*, sal_uInt32 nFormat );
 
     virtual OUString    Expand() const override;
-    virtual SwField*    Copy() const override;
+    virtual std::unique_ptr<SwField> Copy() const override;
 
     void SetText( const OUString& rText )      { m_sText = rText; }
 
@@ -650,7 +650,7 @@ public:
                      const OUString& sText, const OUString& sHelp );
 
     virtual OUString    Expand() const override;
-    virtual SwField*    Copy() const override;
+    virtual std::unique_ptr<SwField> Copy() const override;
 
     /// Placeholder-Text
     virtual OUString GetPar1() const override;
@@ -690,7 +690,7 @@ public:
     virtual OUString        GetDescription() const override;
 
     virtual OUString        Expand() const override;
-    virtual SwField*        Copy() const override;
+    virtual std::unique_ptr<SwField> Copy() const override;
 
     /// Type
     virtual OUString        GetPar1() const override;
@@ -725,7 +725,7 @@ public:
     SwCombinedCharField( SwCombinedCharFieldType*, const OUString& rChars );
 
     virtual OUString        Expand() const override;
-    virtual SwField*        Copy() const override;
+    virtual std::unique_ptr<SwField> Copy() const override;
 
     /// Characters
     virtual OUString    GetPar1() const override;
diff --git a/sw/inc/expfld.hxx b/sw/inc/expfld.hxx
index aefbbdbcbe0e..952e7fcc5402 100644
--- a/sw/inc/expfld.hxx
+++ b/sw/inc/expfld.hxx
@@ -92,7 +92,7 @@ class SW_DLLPUBLIC SwGetExpField : public SwFormulaField
     bool            bLateInitialization; // #i82544#
 
     virtual OUString            Expand() const override;
-    virtual SwField*            Copy() const override;
+    virtual std::unique_ptr<SwField> Copy() const override;
 
 public:
     SwGetExpField( SwGetExpFieldType*, const OUString& rFormel,
@@ -214,7 +214,7 @@ class SW_DLLPUBLIC SwSetExpField : public SwFormulaField
     SwFormatField * mpFormatField; /// pool item to which the SwSetExpField belongs
 
     virtual OUString            Expand() const override;
-    virtual SwField*            Copy() const override;
+    virtual std::unique_ptr<SwField> Copy() const override;
 
 public:
     SwSetExpField(SwSetExpFieldType*, const OUString& rFormel, sal_uLong nFormat = 0);
@@ -299,7 +299,7 @@ class SW_DLLPUBLIC SwInputField : public SwField
     SwFormatField* mpFormatField; // attribute to which the <SwInputField> belongs to
 
     virtual OUString        Expand() const override;
-    virtual SwField*        Copy() const override;
+    virtual std::unique_ptr<SwField> Copy() const override;
 
     // Accessing Input Field's content
     const OUString& getContent() const { return aContent;}
@@ -385,7 +385,7 @@ class SwTableField : public SwValueField, public SwTableFormula
     sal_uInt16      nSubType;
 
     virtual OUString    Expand() const override;
-    virtual SwField*    Copy() const override;
+    virtual std::unique_ptr<SwField> Copy() const override;
 
     /// Search TextNode containing the field.
     virtual const SwNode* GetNodeOfFormula() const override;
diff --git a/sw/inc/fldbas.hxx b/sw/inc/fldbas.hxx
index fcb55df88b4c..0093ce66b337 100644
--- a/sw/inc/fldbas.hxx
+++ b/sw/inc/fldbas.hxx
@@ -283,7 +283,7 @@ private:
     SwFieldType*        m_pType;
 
     virtual OUString    Expand() const = 0;
-    virtual SwField*    Copy() const = 0;
+    virtual std::unique_ptr<SwField> Copy() const = 0;
 
 protected:
     void                SetFormat(sal_uInt32 const nSet) {
@@ -316,7 +316,7 @@ public:
     /// @return name or content.
     virtual OUString    GetFieldName() const;
 
-    SwField *           CopyField() const;
+    std::unique_ptr<SwField> CopyField() const;
 
     /// ResId
     SwFieldIds          Which() const
diff --git a/sw/inc/flddat.hxx b/sw/inc/flddat.hxx
index dc5d2d6ee0db..9ffdc71c63ed 100644
--- a/sw/inc/flddat.hxx
+++ b/sw/inc/flddat.hxx
@@ -46,7 +46,7 @@ class SW_DLLPUBLIC SwDateTimeField : public SwValueField
         long                nOffset;    // Offset in minutes.
 
         virtual OUString    Expand() const override;
-        virtual SwField*    Copy() const override;
+        virtual std::unique_ptr<SwField> Copy() const override;
 
 public:
         SwDateTimeField(SwDateTimeFieldType* pType, sal_uInt16 nSubType = DATEFLD,
diff --git a/sw/inc/flddropdown.hxx b/sw/inc/flddropdown.hxx
index 36aa09fe0b3d..f53e727cfe24 100644
--- a/sw/inc/flddropdown.hxx
+++ b/sw/inc/flddropdown.hxx
@@ -97,7 +97,7 @@ class SW_DLLPUBLIC SwDropDownField : public SwField
 
        @return the copy of this field
     */
-    virtual SwField * Copy() const override;
+    virtual std::unique_ptr<SwField> Copy() const override;
 
 public:
     /**
diff --git a/sw/inc/fmtfld.hxx b/sw/inc/fmtfld.hxx
index cc4c365b1238..1b72d3e6b83c 100644
--- a/sw/inc/fmtfld.hxx
+++ b/sw/inc/fmtfld.hxx
@@ -44,7 +44,7 @@ class SW_DLLPUBLIC SwFormatField
 
     css::uno::WeakReference<css::text::XTextField> m_wXTextField;
 
-    SwField* mpField;
+    std::unique_ptr<SwField> mpField;
     SwTextField* mpTextField; // the TextAttribute
 
 protected:
@@ -70,11 +70,11 @@ public:
 
     const SwField* GetField() const
     {
-        return mpField;
+        return mpField.get();
     }
     SwField* GetField()
     {
-        return mpField;
+        return mpField.get();
     }
 
     /**
@@ -84,7 +84,7 @@ public:
 
        @attention The current field will be destroyed before setting the new field.
      */
-    void SetField( SwField * pField );
+    void SetField( std::unique_ptr<SwField> pField );
 
     const SwTextField* GetTextField() const
     {
diff --git a/sw/inc/reffld.hxx b/sw/inc/reffld.hxx
index 8a9987b9ab87..45ce7ddbc11c 100644
--- a/sw/inc/reffld.hxx
+++ b/sw/inc/reffld.hxx
@@ -88,7 +88,7 @@ private:
     sal_uInt16 nSeqNo;
 
     virtual OUString    Expand() const override;
-    virtual SwField*    Copy() const override;
+    virtual std::unique_ptr<SwField> Copy() const override;
 
     // #i81002#
     static OUString MakeRefNumStr( const SwTextNode& rTextNodeOfField,
diff --git a/sw/inc/usrfld.hxx b/sw/inc/usrfld.hxx
index d523964b97c3..c776f5b200c4 100644
--- a/sw/inc/usrfld.hxx
+++ b/sw/inc/usrfld.hxx
@@ -88,7 +88,7 @@ class SW_DLLPUBLIC SwUserField : public SwValueField
     sal_uInt16  nSubType;
 
     virtual OUString        Expand() const override;
-    virtual SwField*        Copy() const override;
+    virtual std::unique_ptr<SwField> Copy() const override;
 
 public:
     SwUserField(SwUserFieldType*, sal_uInt16 nSub, sal_uInt32 nFormat);
diff --git a/sw/source/core/doc/DocumentFieldsManager.cxx b/sw/source/core/doc/DocumentFieldsManager.cxx
index 9d56929ff792..a37d713fd105 100644
--- a/sw/source/core/doc/DocumentFieldsManager.cxx
+++ b/sw/source/core/doc/DocumentFieldsManager.cxx
@@ -495,8 +495,8 @@ bool DocumentFieldsManager::UpdateField(SwTextField * pDstTextField, SwField & r
             m_rDoc.GetIDocumentUndoRedo().AppendUndo(pUndo);
         }
 
-        SwField * pNewField = rSrcField.CopyField();
-        pDstFormatField->SetField(pNewField);
+        pDstFormatField->SetField(rSrcField.CopyField());
+        SwField* pNewField = pDstFormatField->GetField();
 
         switch( nFieldWhich )
         {
diff --git a/sw/source/core/fields/authfld.cxx b/sw/source/core/fields/authfld.cxx
index 4c6803e2b1c2..2cd6afe06cc4 100644
--- a/sw/source/core/fields/authfld.cxx
+++ b/sw/source/core/fields/authfld.cxx
@@ -569,10 +569,10 @@ OUString SwAuthorityField::ExpandCitation(ToxAuthorityField eField) const
     return sRet;
 }
 
-SwField* SwAuthorityField::Copy() const
+std::unique_ptr<SwField> SwAuthorityField::Copy() const
 {
     SwAuthorityFieldType* pAuthType = static_cast<SwAuthorityFieldType*>(GetTyp());
-    return new SwAuthorityField(pAuthType, m_nHandle);
+    return o3tl::make_unique<SwAuthorityField>(pAuthType, m_nHandle);
 }
 
 OUString SwAuthorityField::GetFieldText(ToxAuthorityField eField) const
diff --git a/sw/source/core/fields/chpfld.cxx b/sw/source/core/fields/chpfld.cxx
index 1333b6bff464..68b2749cedc6 100644
--- a/sw/source/core/fields/chpfld.cxx
+++ b/sw/source/core/fields/chpfld.cxx
@@ -84,17 +84,17 @@ OUString SwChapterField::Expand() const
     return sNumber;
 }
 
-SwField* SwChapterField::Copy() const
+std::unique_ptr<SwField> SwChapterField::Copy() const
 {
-    SwChapterField *pTmp =
-        new SwChapterField(static_cast<SwChapterFieldType*>(GetTyp()), GetFormat());
+    std::unique_ptr<SwChapterField> pTmp(
+        new SwChapterField(static_cast<SwChapterFieldType*>(GetTyp()), GetFormat()));
     pTmp->nLevel = nLevel;
     pTmp->sTitle = sTitle;
     pTmp->sNumber = sNumber;
     pTmp->sPost = sPost;
     pTmp->sPre = sPre;
 
-    return pTmp;
+    return std::unique_ptr<SwField>(pTmp.release());
 }
 
 // #i53420#
diff --git a/sw/source/core/fields/dbfld.cxx b/sw/source/core/fields/dbfld.cxx
index f74ac04bb843..9b2f5940ee1b 100644
--- a/sw/source/core/fields/dbfld.cxx
+++ b/sw/source/core/fields/dbfld.cxx
@@ -213,9 +213,9 @@ OUString SwDBField::Expand() const
     return OUString();
 }
 
-SwField* SwDBField::Copy() const
+std::unique_ptr<SwField> SwDBField::Copy() const
 {
-    SwDBField *pTmp = new SwDBField(static_cast<SwDBFieldType*>(GetTyp()), GetFormat());
+    std::unique_ptr<SwDBField> pTmp(new SwDBField(static_cast<SwDBFieldType*>(GetTyp()), GetFormat()));
     pTmp->aContent      = aContent;
     pTmp->bIsInBodyText  = bIsInBodyText;
     pTmp->bValidValue   = bValidValue;
@@ -224,7 +224,7 @@ SwField* SwDBField::Copy() const
     pTmp->SetValue(GetValue());
     pTmp->sFieldCode = sFieldCode;
 
-    return pTmp;
+    return std::unique_ptr<SwField>(pTmp.release());
 }
 
 OUString SwDBField::GetFieldName() const
@@ -571,13 +571,13 @@ OUString SwDBNextSetField::Expand() const
     return OUString();
 }
 
-SwField* SwDBNextSetField::Copy() const
+std::unique_ptr<SwField> SwDBNextSetField::Copy() const
 {
-    SwDBNextSetField *pTmp = new SwDBNextSetField(static_cast<SwDBNextSetFieldType*>(GetTyp()),
-                                         aCond, GetDBData());
+    std::unique_ptr<SwDBNextSetField> pTmp(new SwDBNextSetField(static_cast<SwDBNextSetFieldType*>(GetTyp()),
+                                         aCond, GetDBData()));
     pTmp->SetSubType(GetSubType());
     pTmp->bCondValid = bCondValid;
-    return pTmp;
+    return std::unique_ptr<SwField>(pTmp.release());
 }
 
 void SwDBNextSetField::Evaluate(SwDoc const * pDoc)
@@ -658,13 +658,13 @@ OUString SwDBNumSetField::Expand() const
     return OUString();
 }
 
-SwField* SwDBNumSetField::Copy() const
+std::unique_ptr<SwField> SwDBNumSetField::Copy() const
 {
-    SwDBNumSetField *pTmp = new SwDBNumSetField(static_cast<SwDBNumSetFieldType*>(GetTyp()),
-                                         aCond, aPar2, GetDBData());
+    std::unique_ptr<SwDBNumSetField> pTmp(new SwDBNumSetField(static_cast<SwDBNumSetFieldType*>(GetTyp()),
+                                         aCond, aPar2, GetDBData()));
     pTmp->bCondValid = bCondValid;
     pTmp->SetSubType(GetSubType());
-    return pTmp;
+    return std::unique_ptr<SwField>(pTmp.release());
 }
 
 void SwDBNumSetField::Evaluate(SwDoc const * pDoc)
@@ -772,13 +772,13 @@ OUString SwDBNameField::Expand() const
     return OUString();
 }
 
-SwField* SwDBNameField::Copy() const
+std::unique_ptr<SwField> SwDBNameField::Copy() const
 {
-    SwDBNameField *pTmp = new SwDBNameField(static_cast<SwDBNameFieldType*>(GetTyp()), GetDBData());
+    std::unique_ptr<SwDBNameField> pTmp(new SwDBNameField(static_cast<SwDBNameFieldType*>(GetTyp()), GetDBData()));
     pTmp->ChangeFormat(GetFormat());
     pTmp->SetLanguage(GetLanguage());
     pTmp->SetSubType(GetSubType());
-    return pTmp;
+    return std::unique_ptr<SwField>(pTmp.release());
 }
 
 bool SwDBNameField::QueryValue( uno::Any& rAny, sal_uInt16 nWhichId ) const
@@ -828,14 +828,14 @@ void SwDBSetNumberField::Evaluate(SwDoc const * pDoc)
     nNumber = pMgr->GetSelectedRecordId();
 }
 
-SwField* SwDBSetNumberField::Copy() const
+std::unique_ptr<SwField> SwDBSetNumberField::Copy() const
 {
-    SwDBSetNumberField *pTmp =
-        new SwDBSetNumberField(static_cast<SwDBSetNumberFieldType*>(GetTyp()), GetDBData(), GetFormat());
+    std::unique_ptr<SwDBSetNumberField> pTmp(
+        new SwDBSetNumberField(static_cast<SwDBSetNumberFieldType*>(GetTyp()), GetDBData(), GetFormat()));
     pTmp->SetLanguage(GetLanguage());
     pTmp->SetSetNumber(nNumber);
     pTmp->SetSubType(GetSubType());
-    return pTmp;
+    return std::unique_ptr<SwField>(pTmp.release());
 }
 
 bool SwDBSetNumberField::QueryValue( uno::Any& rAny, sal_uInt16 nWhichId ) const
diff --git a/sw/source/core/fields/ddefld.cxx b/sw/source/core/fields/ddefld.cxx
index 96b4f28324ff..a552ffecc131 100644
--- a/sw/source/core/fields/ddefld.cxx
+++ b/sw/source/core/fields/ddefld.cxx
@@ -20,6 +20,7 @@
 #include <sal/config.h>
 
 #include <o3tl/any.hxx>
+#include <o3tl/make_unique.hxx>
 #include <osl/thread.h>
 #include <sfx2/linkmgr.hxx>
 #include <doc.hxx>
@@ -354,9 +355,9 @@ OUString SwDDEField::Expand() const
     return aStr;
 }
 
-SwField* SwDDEField::Copy() const
+std::unique_ptr<SwField> SwDDEField::Copy() const
 {
-    return new SwDDEField(static_cast<SwDDEFieldType*>(GetTyp()));
+    return o3tl::make_unique<SwDDEField>(static_cast<SwDDEFieldType*>(GetTyp()));
 }
 
 /// get field type name
diff --git a/sw/source/core/fields/docufld.cxx b/sw/source/core/fields/docufld.cxx
index 051c5f06b457..5cf8372d9794 100644
--- a/sw/source/core/fields/docufld.cxx
+++ b/sw/source/core/fields/docufld.cxx
@@ -211,14 +211,14 @@ OUString SwPageNumberField::Expand() const
     return sRet;
 }
 
-SwField* SwPageNumberField::Copy() const
+std::unique_ptr<SwField> SwPageNumberField::Copy() const
 {
-    SwPageNumberField *pTmp = new SwPageNumberField(
+    std::unique_ptr<SwPageNumberField> pTmp(new SwPageNumberField(
                 static_cast<SwPageNumberFieldType*>(GetTyp()), m_nSubType,
-                GetFormat(), m_nOffset, m_nPageNumber, m_nMaxPage);
+                GetFormat(), m_nOffset, m_nPageNumber, m_nMaxPage));
     pTmp->SetLanguage( GetLanguage() );
     pTmp->SetUserString( m_sUserStr );
-    return pTmp;
+    return std::unique_ptr<SwField>(pTmp.release());
 }
 
 OUString SwPageNumberField::GetPar2() const
@@ -344,12 +344,12 @@ OUString SwAuthorField::Expand() const
     return m_aContent;
 }
 
-SwField* SwAuthorField::Copy() const
+std::unique_ptr<SwField> SwAuthorField::Copy() const
 {
-    SwAuthorField *pTmp = new SwAuthorField( static_cast<SwAuthorFieldType*>(GetTyp()),
-                                                GetFormat());
+    std::unique_ptr<SwAuthorField> pTmp(new SwAuthorField( static_cast<SwAuthorFieldType*>(GetTyp()),
+                                                GetFormat()));
     pTmp->SetExpansion(m_aContent);
-    return pTmp;
+    return std::unique_ptr<SwField>(pTmp.release());
 }
 
 bool SwAuthorField::QueryValue( uno::Any& rAny, sal_uInt16 nWhichId ) const
@@ -477,13 +477,13 @@ OUString SwFileNameField::Expand() const
     return m_aContent;
 }
 
-SwField* SwFileNameField::Copy() const
+std::unique_ptr<SwField> SwFileNameField::Copy() const
 {
-    SwFileNameField *pTmp =
-        new SwFileNameField(static_cast<SwFileNameFieldType*>(GetTyp()), GetFormat());
+    std::unique_ptr<SwFileNameField> pTmp(
+        new SwFileNameField(static_cast<SwFileNameFieldType*>(GetTyp()), GetFormat()));
     pTmp->SetExpansion(m_aContent);
 
-    return pTmp;
+    return std::unique_ptr<SwField>(pTmp.release());
 }
 
 bool SwFileNameField::QueryValue( uno::Any& rAny, sal_uInt16 nWhichId ) const
@@ -643,11 +643,9 @@ OUString SwTemplNameField::Expand() const
     return static_cast<SwTemplNameFieldType*>(GetTyp())->Expand(GetFormat());
 }
 
-SwField* SwTemplNameField::Copy() const
+std::unique_ptr<SwField> SwTemplNameField::Copy() const
 {
-    SwTemplNameField *pTmp =
-        new SwTemplNameField(static_cast<SwTemplNameFieldType*>(GetTyp()), GetFormat());
-    return pTmp;
+    return o3tl::make_unique<SwTemplNameField>(static_cast<SwTemplNameFieldType*>(GetTyp()), GetFormat());
 }
 
 bool SwTemplNameField::QueryValue( uno::Any& rAny, sal_uInt16 nWhichId ) const
@@ -772,11 +770,10 @@ OUString SwDocStatField::Expand() const
     return static_cast<SwDocStatFieldType*>(GetTyp())->Expand(m_nSubType, static_cast<SvxNumType>(GetFormat()));
 }
 
-SwField* SwDocStatField::Copy() const
+std::unique_ptr<SwField> SwDocStatField::Copy() const
 {
-    SwDocStatField *pTmp = new SwDocStatField(
+    return o3tl::make_unique<SwDocStatField>(
                     static_cast<SwDocStatFieldType*>(GetTyp()), m_nSubType, GetFormat() );
-    return pTmp;
 }
 
 sal_uInt16 SwDocStatField::GetSubType() const
@@ -1135,13 +1132,13 @@ OUString SwDocInfoField::GetFieldName() const
     return aStr;
 }
 
-SwField* SwDocInfoField::Copy() const
+std::unique_ptr<SwField> SwDocInfoField::Copy() const
 {
-    SwDocInfoField* pField = new SwDocInfoField(static_cast<SwDocInfoFieldType*>(GetTyp()), m_nSubType, m_aName, GetFormat());
+    std::unique_ptr<SwDocInfoField> pField(new SwDocInfoField(static_cast<SwDocInfoFieldType*>(GetTyp()), m_nSubType, m_aName, GetFormat()));
     pField->SetAutomaticLanguage(IsAutomaticLanguage());
     pField->m_aContent = m_aContent;
 
-    return pField;
+    return std::unique_ptr<SwField>(pField.release());
 }
 
 sal_uInt16 SwDocInfoField::GetSubType() const
@@ -1402,17 +1399,17 @@ OUString SwHiddenTextField::GetFieldName() const
     return aStr;
 }
 
-SwField* SwHiddenTextField::Copy() const
+std::unique_ptr<SwField> SwHiddenTextField::Copy() const
 {
-    SwHiddenTextField* pField =
+    std::unique_ptr<SwHiddenTextField> pField(
         new SwHiddenTextField(static_cast<SwHiddenTextFieldType*>(GetTyp()), m_aCond,
-                              m_aTRUEText, m_aFALSEText);
+                              m_aTRUEText, m_aFALSEText));
     pField->m_bIsHidden = m_bIsHidden;
     pField->m_bValid    = m_bValid;
     pField->m_aContent  = m_aContent;
     pField->SetFormat(GetFormat());
     pField->m_nSubType  = m_nSubType;
-    return pField;
+    return std::unique_ptr<SwField>(pField.release());
 }
 
 /// set condition
@@ -1669,12 +1666,11 @@ OUString SwHiddenParaField::Expand() const
     return OUString();
 }
 
-SwField* SwHiddenParaField::Copy() const
+std::unique_ptr<SwField> SwHiddenParaField::Copy() const
 {
-    SwHiddenParaField* pField = new SwHiddenParaField(static_cast<SwHiddenParaFieldType*>(GetTyp()), m_aCond);
+    std::unique_ptr<SwHiddenParaField> pField(new SwHiddenParaField(static_cast<SwHiddenParaFieldType*>(GetTyp()), m_aCond));
     pField->m_bIsHidden = m_bIsHidden;
-
-    return pField;
+    return std::unique_ptr<SwField>(pField.release());
 }
 
 bool SwHiddenParaField::QueryValue( uno::Any& rAny, sal_uInt16 nWhichId ) const
@@ -1776,16 +1772,16 @@ OUString SwPostItField::GetDescription() const
     return SwResId(STR_NOTE);
 }
 
-SwField* SwPostItField::Copy() const
+std::unique_ptr<SwField> SwPostItField::Copy() const
 {
-    SwPostItField* pRet = new SwPostItField( static_cast<SwPostItFieldType*>(GetTyp()), m_sAuthor, m_sText, m_sInitials, m_sName,
-                                             m_aDateTime, m_nPostItId);
+    std::unique_ptr<SwPostItField> pRet(new SwPostItField( static_cast<SwPostItFieldType*>(GetTyp()), m_sAuthor, m_sText, m_sInitials, m_sName,
+                                             m_aDateTime, m_nPostItId));
     if (mpText)
         pRet->SetTextObject( o3tl::make_unique<OutlinerParaObject>(*mpText) );
 
     // Note: member <m_xTextObject> not copied.
 
-    return pRet;
+    return std::unique_ptr<SwField>(pRet.release());
 }
 
 /// set author
@@ -2001,12 +1997,12 @@ OUString SwExtUserField::Expand() const
     return m_aContent;
 }
 
-SwField* SwExtUserField::Copy() const
+std::unique_ptr<SwField> SwExtUserField::Copy() const
 {
-    SwExtUserField* pField = new SwExtUserField(static_cast<SwExtUserFieldType*>(GetTyp()), m_nType, GetFormat());
+    std::unique_ptr<SwExtUserField> pField(new SwExtUserField(static_cast<SwExtUserFieldType*>(GetTyp()), m_nType, GetFormat()));
     pField->SetExpansion(m_aContent);
 
-    return pField;
+    return std::unique_ptr<SwField>(pField.release());
 }
 
 sal_uInt16 SwExtUserField::GetSubType() const
@@ -2099,9 +2095,9 @@ OUString SwRefPageSetField::Expand() const
     return OUString();
 }
 
-SwField* SwRefPageSetField::Copy() const
+std::unique_ptr<SwField> SwRefPageSetField::Copy() const
 {
-    return new SwRefPageSetField( static_cast<SwRefPageSetFieldType*>(GetTyp()), m_nOffset, m_bOn );
+    return o3tl::make_unique<SwRefPageSetField>( static_cast<SwRefPageSetFieldType*>(GetTyp()), m_nOffset, m_bOn );
 }
 
 OUString SwRefPageSetField::GetPar2() const
@@ -2287,12 +2283,12 @@ OUString SwRefPageGetField::Expand() const
     return m_sText;
 }
 
-SwField* SwRefPageGetField::Copy() const
+std::unique_ptr<SwField> SwRefPageGetField::Copy() const
 {
-    SwRefPageGetField* pCpy = new SwRefPageGetField(
-                        static_cast<SwRefPageGetFieldType*>(GetTyp()), GetFormat() );
+    std::unique_ptr<SwRefPageGetField> pCpy(new SwRefPageGetField(
+                        static_cast<SwRefPageGetFieldType*>(GetTyp()), GetFormat() ));
     pCpy->SetText( m_sText );
-    return pCpy;
+    return std::unique_ptr<SwField>(pCpy.release());
 }
 
 void SwRefPageGetField::ChangeExpansion( const SwFrame* pFrame,
@@ -2419,9 +2415,9 @@ OUString SwJumpEditField::Expand() const
     return "<" + m_sText + ">";
 }
 
-SwField* SwJumpEditField::Copy() const
+std::unique_ptr<SwField> SwJumpEditField::Copy() const
 {
-    return new SwJumpEditField( static_cast<SwJumpEditFieldType*>(GetTyp()), GetFormat(),
+    return o3tl::make_unique<SwJumpEditField>( static_cast<SwJumpEditFieldType*>(GetTyp()), GetFormat(),
                                 m_sText, m_sHelp );
 }
 
@@ -2539,9 +2535,9 @@ OUString SwCombinedCharField::Expand() const
     return m_sCharacters;
 }
 
-SwField* SwCombinedCharField::Copy() const
+std::unique_ptr<SwField> SwCombinedCharField::Copy() const
 {
-    return new SwCombinedCharField( static_cast<SwCombinedCharFieldType*>(GetTyp()),
+    return o3tl::make_unique<SwCombinedCharField>( static_cast<SwCombinedCharFieldType*>(GetTyp()),
                                         m_sCharacters );
 }
 
diff --git a/sw/source/core/fields/expfld.cxx b/sw/source/core/fields/expfld.cxx
index 1b3637526a3d..e06bafe103be 100644
--- a/sw/source/core/fields/expfld.cxx
+++ b/sw/source/core/fields/expfld.cxx
@@ -309,10 +309,10 @@ OUString SwGetExpField::GetFieldName() const
     return SwFieldType::GetTypeStr(nType) + " " + GetFormula();
 }
 
-SwField* SwGetExpField::Copy() const
+std::unique_ptr<SwField> SwGetExpField::Copy() const
 {
-    SwGetExpField *pTmp = new SwGetExpField(static_cast<SwGetExpFieldType*>(GetTyp()),
-                                            GetFormula(), nSubType, GetFormat());
+    std::unique_ptr<SwGetExpField> pTmp(new SwGetExpField(static_cast<SwGetExpFieldType*>(GetTyp()),
+                                            GetFormula(), nSubType, GetFormat()));
     pTmp->SetLanguage(GetLanguage());
     pTmp->SwValueField::SetValue(GetValue());
     pTmp->sExpand       = sExpand;
@@ -321,7 +321,7 @@ SwField* SwGetExpField::Copy() const
     if( bLateInitialization )
         pTmp->SetLateInitialization();
 
-    return pTmp;
+    return std::unique_ptr<SwField>(pTmp.release());
 }
 
 void SwGetExpField::ChangeExpansion( const SwFrame& rFrame, const SwTextField& rField )
@@ -831,10 +831,10 @@ OUString SwSetExpField::GetFieldName() const
     return aStr;
 }
 
-SwField* SwSetExpField::Copy() const
+std::unique_ptr<SwField> SwSetExpField::Copy() const
 {
-    SwSetExpField *pTmp = new SwSetExpField(static_cast<SwSetExpFieldType*>(GetTyp()),
-                                            GetFormula(), GetFormat());
+    std::unique_ptr<SwSetExpField> pTmp(new SwSetExpField(static_cast<SwSetExpFieldType*>(GetTyp()),
+                                            GetFormula(), GetFormat()));
     pTmp->SwValueField::SetValue(GetValue());
     pTmp->sExpand       = sExpand;
     pTmp->SetAutomaticLanguage(IsAutomaticLanguage());
@@ -844,7 +844,7 @@ SwField* SwSetExpField::Copy() const
     pTmp->nSeqNo        = nSeqNo;
     pTmp->SetSubType(GetSubType());
 
-    return pTmp;
+    return std::unique_ptr<SwField>(pTmp.release());
 }
 
 void SwSetExpField::SetSubType(sal_uInt16 nSub)
@@ -1192,22 +1192,22 @@ OUString SwInputField::GetFieldName() const
     return aStr;
 }
 
-SwField* SwInputField::Copy() const
+std::unique_ptr<SwField> SwInputField::Copy() const
 {
-    SwInputField* pField =
+    std::unique_ptr<SwInputField> pField(
         new SwInputField(
             static_cast<SwInputFieldType*>(GetTyp()),
             getContent(),
             aPText,
             GetSubType(),
             GetFormat(),
-            mbIsFormField );
+            mbIsFormField ));
 
     pField->SetHelp( aHelp );
     pField->SetToolTip( aToolTip );
 
     pField->SetAutomaticLanguage(IsAutomaticLanguage());
-    return pField;
+    return std::unique_ptr<SwField>(pField.release());
 }
 
 OUString SwInputField::Expand() const
diff --git a/sw/source/core/fields/fldbas.cxx b/sw/source/core/fields/fldbas.cxx
index 3087bc382eb8..887bf3a008de 100644
--- a/sw/source/core/fields/fldbas.cxx
+++ b/sw/source/core/fields/fldbas.cxx
@@ -421,9 +421,9 @@ OUString SwField::ExpandField(bool const bCached) const
     return Expand();
 }
 
-SwField * SwField::CopyField() const
+std::unique_ptr<SwField> SwField::CopyField() const
 {
-    SwField *const pNew = Copy();
+    std::unique_ptr<SwField> pNew = Copy();
     // #i85766# cache expansion of source (for clipboard)
     // use this->cache, not this->Expand(): only text formatting calls Expand()
     pNew->m_Cache = m_Cache;
diff --git a/sw/source/core/fields/flddat.cxx b/sw/source/core/fields/flddat.cxx
index 6cb2f930b492..b8350e0500b2 100644
--- a/sw/source/core/fields/flddat.cxx
+++ b/sw/source/core/fields/flddat.cxx
@@ -79,17 +79,17 @@ OUString SwDateTimeField::Expand() const
     return ExpandValue(fVal, GetFormat(), GetLanguage());
 }
 
-SwField* SwDateTimeField::Copy() const
+std::unique_ptr<SwField> SwDateTimeField::Copy() const
 {
-    SwDateTimeField *pTmp =
+    std::unique_ptr<SwDateTimeField> pTmp(
         new SwDateTimeField(static_cast<SwDateTimeFieldType*>(GetTyp()), nSubType,
-                                            GetFormat(), GetLanguage());
+                                            GetFormat(), GetLanguage()) );
 
     pTmp->SetValue(GetValue());
     pTmp->SetOffset(nOffset);
     pTmp->SetAutomaticLanguage(IsAutomaticLanguage());
 
-    return pTmp;
+    return std::unique_ptr<SwField>(pTmp.release());
 }
 
 sal_uInt16 SwDateTimeField::GetSubType() const
diff --git a/sw/source/core/fields/flddropdown.cxx b/sw/source/core/fields/flddropdown.cxx
index 3ebad4c236cc..97b5cb5a338c 100644
--- a/sw/source/core/fields/flddropdown.cxx
+++ b/sw/source/core/fields/flddropdown.cxx
@@ -23,6 +23,7 @@
 
 #include <svl/poolitem.hxx>
 #include <comphelper/sequence.hxx>
+#include <o3tl/make_unique.hxx>
 
 #include <unofldmid.h>
 #include <unoprnms.hxx>
@@ -76,9 +77,9 @@ OUString SwDropDownField::Expand() const
     return sSelect;
 }
 
-SwField * SwDropDownField::Copy() const
+std::unique_ptr<SwField> SwDropDownField::Copy() const
 {
-    return new SwDropDownField(*this);
+    return o3tl::make_unique<SwDropDownField>(*this);
 }
 
 OUString SwDropDownField::GetPar1() const
diff --git a/sw/source/core/fields/macrofld.cxx b/sw/source/core/fields/macrofld.cxx
index a0bd9edd5560..d51816cde458 100644
--- a/sw/source/core/fields/macrofld.cxx
+++ b/sw/source/core/fields/macrofld.cxx
@@ -24,6 +24,7 @@
 #include <com/sun/star/uri/UriReferenceFactory.hpp>
 #include <com/sun/star/uri/XVndSunStarScriptUrl.hpp>
 #include <comphelper/processfactory.hxx>
+#include <o3tl/make_unique.hxx>
 
 using namespace ::com::sun::star;
 
@@ -51,9 +52,9 @@ OUString SwMacroField::Expand() const
     return m_aText ;
 }
 
-SwField* SwMacroField::Copy() const
+std::unique_ptr<SwField> SwMacroField::Copy() const
 {
-    return new SwMacroField(static_cast<SwMacroFieldType*>(GetTyp()), m_aMacro, m_aText);
+    return o3tl::make_unique<SwMacroField>(static_cast<SwMacroFieldType*>(GetTyp()), m_aMacro, m_aText);
 }
 
 OUString SwMacroField::GetFieldName() const
diff --git a/sw/source/core/fields/reffld.cxx b/sw/source/core/fields/reffld.cxx
index 97ac139d53cf..da737a26d342 100644
--- a/sw/source/core/fields/reffld.cxx
+++ b/sw/source/core/fields/reffld.cxx
@@ -742,13 +742,13 @@ OUString SwGetRefField::MakeRefNumStr( const SwTextNode& rTextNodeOfField,
     return OUString();
 }
 
-SwField* SwGetRefField::Copy() const
+std::unique_ptr<SwField> SwGetRefField::Copy() const
 {
-    SwGetRefField* pField = new SwGetRefField( static_cast<SwGetRefFieldType*>(GetTyp()),
+    std::unique_ptr<SwGetRefField> pField( new SwGetRefField( static_cast<SwGetRefFieldType*>(GetTyp()),
                                                 sSetRefName, sSetReferenceLanguage, nSubType,
-                                                nSeqNo, GetFormat() );
+                                                nSeqNo, GetFormat() ) );
     pField->sText = sText;
-    return pField;
+    return std::unique_ptr<SwField>(pField.release());
 }
 
 /// get reference name
diff --git a/sw/source/core/fields/scrptfld.cxx b/sw/source/core/fields/scrptfld.cxx
index fd951fb7d825..84b115b4e7c9 100644
--- a/sw/source/core/fields/scrptfld.cxx
+++ b/sw/source/core/fields/scrptfld.cxx
@@ -21,6 +21,7 @@
 #include <unofldmid.h>
 #include <strings.hrc>
 #include <o3tl/any.hxx>
+#include <o3tl/make_unique.hxx>
 #include <swtypes.hxx>
 
 using namespace ::com::sun::star;
@@ -51,9 +52,9 @@ OUString SwScriptField::Expand() const
     return OUString();
 }
 
-SwField* SwScriptField::Copy() const
+std::unique_ptr<SwField> SwScriptField::Copy() const
 {
-    return new SwScriptField( static_cast<SwScriptFieldType*>(GetTyp()), m_sType, m_sCode, m_bCodeURL );
+    return o3tl::make_unique<SwScriptField>( static_cast<SwScriptFieldType*>(GetTyp()), m_sType, m_sCode, m_bCodeURL );
 }
 
 /// set type
diff --git a/sw/source/core/fields/tblcalc.cxx b/sw/source/core/fields/tblcalc.cxx
index f5a1181903ad..8ca2334aecb6 100644
--- a/sw/source/core/fields/tblcalc.cxx
+++ b/sw/source/core/fields/tblcalc.cxx
@@ -63,15 +63,15 @@ SwTableField::SwTableField( SwTableFieldType* pInitType, const OUString& rFormel
     sExpand = "0";
 }
 
-SwField* SwTableField::Copy() const
+std::unique_ptr<SwField> SwTableField::Copy() const
 {
-    SwTableField* pTmp = new SwTableField( static_cast<SwTableFieldType*>(GetTyp()),
-                                        SwTableFormula::GetFormula(), nSubType, GetFormat() );
+    std::unique_ptr<SwTableField> pTmp(new SwTableField( static_cast<SwTableFieldType*>(GetTyp()),
+                                        SwTableFormula::GetFormula(), nSubType, GetFormat() ));
     pTmp->sExpand     = sExpand;
     pTmp->SwValueField::SetValue(GetValue());
     pTmp->SwTableFormula::operator=( *this );
     pTmp->SetAutomaticLanguage(IsAutomaticLanguage());
-    return pTmp;
+    return std::unique_ptr<SwField>(pTmp.release());
 }
 
 OUString SwTableField::GetFieldName() const
diff --git a/sw/source/core/fields/usrfld.cxx b/sw/source/core/fields/usrfld.cxx
index fe7265df7982..809884e163d2 100644
--- a/sw/source/core/fields/usrfld.cxx
+++ b/sw/source/core/fields/usrfld.cxx
@@ -55,9 +55,9 @@ OUString SwUserField::Expand() const
     return OUString();
 }
 
-SwField* SwUserField::Copy() const
+std::unique_ptr<SwField> SwUserField::Copy() const
 {
-    SwField* pTmp = new SwUserField(static_cast<SwUserFieldType*>(GetTyp()), nSubType, GetFormat());
+    std::unique_ptr<SwField> pTmp(new SwUserField(static_cast<SwUserFieldType*>(GetTyp()), nSubType, GetFormat()));
     pTmp->SetAutomaticLanguage(IsAutomaticLanguage());
     return pTmp;
 }
diff --git a/sw/source/core/txtnode/atrfld.cxx b/sw/source/core/txtnode/atrfld.cxx
index 8ebffec8b24c..05fc94387539 100644
--- a/sw/source/core/txtnode/atrfld.cxx
+++ b/sw/source/core/txtnode/atrfld.cxx
@@ -63,12 +63,12 @@ SwFormatField::SwFormatField( const SwField &rField )
     {
         // input field in-place editing
         SetWhich( RES_TXTATR_INPUTFIELD );
-        static_cast<SwInputField*>(mpField)->SetFormatField( *this );
+        static_cast<SwInputField*>(mpField.get())->SetFormatField( *this );
     }
     else if (mpField->GetTyp()->Which() == SwFieldIds::SetExp)
     {
         // see SwWrtShell::StartInputFieldDlg
-        static_cast<SwSetExpField *>(mpField)->SetFormatField(*this);
+        static_cast<SwSetExpField *>(mpField.get())->SetFormatField(*this);
     }
     else if ( mpField->GetTyp()->Which() == SwFieldIds::Postit )
     {
@@ -96,7 +96,7 @@ SwFormatField::SwFormatField( const SwFormatField& rAttr )
         {
             // input field in-place editing
             SetWhich( RES_TXTATR_INPUTFIELD );
-            SwInputField *pField = dynamic_cast<SwInputField*>(mpField);
+            SwInputField *pField = dynamic_cast<SwInputField*>(mpField.get());
             assert(pField);
             if (pField)
                 pField->SetFormatField( *this );
@@ -104,7 +104,7 @@ SwFormatField::SwFormatField( const SwFormatField& rAttr )
         else if (mpField->GetTyp()->Which() == SwFieldIds::SetExp)
         {
             // see SwWrtShell::StartInputFieldDlg
-            static_cast<SwSetExpField *>(mpField)->SetFormatField(*this);
+            static_cast<SwSetExpField *>(mpField.get())->SetFormatField(*this);
         }
         else if ( mpField->GetTyp()->Which() == SwFieldIds::Postit )
         {
@@ -122,7 +122,7 @@ SwFormatField::~SwFormatField()
         pType = nullptr;  // DB field types destroy themselves
 
     Broadcast( SwFormatFieldHint( this, SwFormatFieldHintWhich::REMOVED ) );
-    delete mpField;
+    mpField.reset();
 
     // some fields need to delete their field type
     if( pType && pType->HasOnlyOneListener() )
@@ -158,19 +158,17 @@ void SwFormatField::RegisterToFieldType( SwFieldType& rType )
     rType.Add(this);
 }
 
-void SwFormatField::SetField(SwField * _pField)
+void SwFormatField::SetField(std::unique_ptr<SwField> _pField)
 {
-    delete mpField;
-
-    mpField = _pField;
+    mpField = std::move(_pField);
     if ( mpField->GetTyp()->Which() == SwFieldIds::Input )
     {
-        static_cast<SwInputField* >(mpField)->SetFormatField( *this );
+        static_cast<SwInputField* >(mpField.get())->SetFormatField( *this );
     }
     else if (mpField->GetTyp()->Which() == SwFieldIds::SetExp)
     {
         // see SwWrtShell::StartInputFieldDlg
-        static_cast<SwSetExpField *>(mpField)->SetFormatField(*this);
+        static_cast<SwSetExpField *>(mpField.get())->SetFormatField(*this);
     }
     Broadcast( SwFormatFieldHint( this, SwFormatFieldHintWhich::CHANGED ) );
 }
@@ -260,7 +258,7 @@ void SwFormatField::Modify( const SfxPoolItem* pOld, const SfxPoolItem* pNew )
                 if( SwFieldIds::GetRef == mpField->GetTyp()->Which() )
                 {
                     // #i81002#
-                    static_cast<SwGetRefField*>(mpField)->UpdateField( mpTextField );
+                    static_cast<SwGetRefField*>(mpField.get())->UpdateField( mpTextField );
                 }
                 break;
         case RES_DOCPOS_UPDATE:
diff --git a/sw/source/core/undo/SwUndoField.cxx b/sw/source/core/undo/SwUndoField.cxx
index 10193dd89135..1724baec70d7 100644
--- a/sw/source/core/undo/SwUndoField.cxx
+++ b/sw/source/core/undo/SwUndoField.cxx
@@ -72,8 +72,6 @@ SwUndoFieldFromDoc::SwUndoFieldFromDoc(const SwPosition & rPos,
 
 SwUndoFieldFromDoc::~SwUndoFieldFromDoc()
 {
-    delete pOldField;
-    delete pNewField;
 }
 
 void SwUndoFieldFromDoc::UndoImpl(::sw::UndoRedoContext &)
diff --git a/sw/source/core/unocore/unofield.cxx b/sw/source/core/unocore/unofield.cxx
index a758f6523295..620143150e8a 100644
--- a/sw/source/core/unocore/unofield.cxx
+++ b/sw/source/core/unocore/unofield.cxx
@@ -1984,9 +1984,9 @@ void SAL_CALL SwXTextField::attach(
         {
             UnoActionContext aCont( m_pImpl->m_pDoc );
             // insert copy of annotation at new text range
-            SwPostItField* pPostItField = static_cast< SwPostItField* >(m_pImpl->m_pFormatField->GetField()->CopyField());
+            std::unique_ptr<SwPostItField> pPostItField(static_cast< SwPostItField* >(m_pImpl->m_pFormatField->GetField()->CopyField().release()));
             SwFormatField aFormatField( *pPostItField );
-            delete pPostItField;
+            pPostItField.reset();
             SwPaM aEnd( *aIntPam.End(), *aIntPam.End() );
             m_pImpl->m_pDoc->getIDocumentContentOperations().InsertPoolItem( aEnd, aFormatField );
             // delete former annotation
diff --git a/sw/source/filter/ww8/docxattributeoutput.cxx b/sw/source/filter/ww8/docxattributeoutput.cxx
index 3671060f4d52..1709cd036739 100644
--- a/sw/source/filter/ww8/docxattributeoutput.cxx
+++ b/sw/source/filter/ww8/docxattributeoutput.cxx
@@ -7255,7 +7255,7 @@ void DocxAttributeOutput::WriteField_Impl( const SwField* pField, ww::eField eTy
 
     struct FieldInfos infos;
     if (pField)
-        infos.pField.reset(pField->CopyField());
+        infos.pField = pField->CopyField();
     infos.sCmd = rFieldCmd;
     infos.eType = eType;
     infos.bClose = bool(FieldFlags::Close & nMode);
diff --git a/sw/source/ui/dbui/dbinsdlg.cxx b/sw/source/ui/dbui/dbinsdlg.cxx
index 98accb38e18e..42d6728c880c 100644
--- a/sw/source/ui/dbui/dbinsdlg.cxx
+++ b/sw/source/ui/dbui/dbinsdlg.cxx
@@ -1314,7 +1314,7 @@ void SwInsertDBColAutoPilot::DataToDoc( const Sequence<Any>& rSelection,
                     case DB_Column::Type::COL_FIELD:
                         {
                             std::unique_ptr<SwDBField> pField(static_cast<SwDBField *>(
-                                pDBCol->pField->CopyField()));
+                                pDBCol->pField->CopyField().release()));
                             double nValue = DBL_MAX;
 
                             Reference< XPropertySet > xColumnProps;
diff --git a/sw/source/ui/fldui/DropDownFieldDialog.cxx b/sw/source/ui/fldui/DropDownFieldDialog.cxx
index d467cfa3470e..7514b85bb8a0 100644
--- a/sw/source/ui/fldui/DropDownFieldDialog.cxx
+++ b/sw/source/ui/fldui/DropDownFieldDialog.cxx
@@ -93,7 +93,7 @@ void sw::DropDownFieldDialog::Apply()
             m_rSh.StartAllAction();
 
             std::unique_ptr<SwDropDownField> const pCopy(
-                static_cast<SwDropDownField*>(m_pDropField->CopyField()));
+                static_cast<SwDropDownField*>(m_pDropField->CopyField().release()));
 
             pCopy->SetPar1(sSelect);
             m_rSh.SwEditShell::UpdateFields(*pCopy);
diff --git a/sw/source/ui/fldui/fldpage.cxx b/sw/source/ui/fldui/fldpage.cxx
index c490b5e72c05..4c22c34a07ac 100644
--- a/sw/source/ui/fldui/fldpage.cxx
+++ b/sw/source/ui/fldui/fldpage.cxx
@@ -169,7 +169,7 @@ void SwFieldPage::InsertField(sal_uInt16 nTypeId, sal_uInt16 nSubType, const OUS
     }
     else    // change field
     {
-        SwField *const pTmpField = m_pCurField->CopyField();
+        std::unique_ptr<SwField> pTmpField = m_pCurField->CopyField();
 
         OUString sPar1(rPar1);
         OUString sPar2(rPar2);
@@ -194,7 +194,7 @@ void SwFieldPage::InsertField(sal_uInt16 nTypeId, sal_uInt16 nSubType, const OUS
                 aData.nCommandType = rPar1.getToken(0, DB_DELIM, nPos).toInt32();
                 sPar1 = rPar1.copy(nPos);
 
-                static_cast<SwDBNameInfField*>(pTmpField)->SetDBData(aData);
+                static_cast<SwDBNameInfField*>(pTmpField.get())->SetDBData(aData);
             }
             break;
 
@@ -240,7 +240,7 @@ void SwFieldPage::InsertField(sal_uInt16 nTypeId, sal_uInt16 nSubType, const OUS
                 if (m_aMgr.GetFieldType(SwFieldIds::User, sPar1) == nullptr &&
                 !(pTmpField->GetSubType() & INP_TXT)) // SETEXPFLD
                 {
-                    SwSetExpField* pField = static_cast<SwSetExpField*>(pTmpField);
+                    SwSetExpField* pField = static_cast<SwSetExpField*>(pTmpField.get());
                     pField->SetPromptText(sPar2);
                     sPar2 = pField->GetPar2();
                 }
@@ -250,7 +250,7 @@ void SwFieldPage::InsertField(sal_uInt16 nTypeId, sal_uInt16 nSubType, const OUS
             {
                 if( nSubType == nsSwDocInfoSubType::DI_CUSTOM )
                 {
-                    SwDocInfoField* pDocInfo = static_cast<SwDocInfoField*>( pTmpField );
+                    SwDocInfoField* pDocInfo = static_cast<SwDocInfoField*>( pTmpField.get() );
                     pDocInfo->SetName( rPar1 );
                 }
             }
@@ -262,7 +262,7 @@ void SwFieldPage::InsertField(sal_uInt16 nTypeId, sal_uInt16 nSubType, const OUS
         pTmpField->SetSubType(nSubType);
         pTmpField->SetAutomaticLanguage(bIsAutomaticLanguage);
 
-        m_aMgr.UpdateCurField( nFormatId, sPar1, sPar2, pTmpField );
+        m_aMgr.UpdateCurField( nFormatId, sPar1, sPar2, std::move(pTmpField) );
 
         m_pCurField = m_aMgr.GetCurField();
 
diff --git a/sw/source/uibase/docvw/AnnotationWin.cxx b/sw/source/uibase/docvw/AnnotationWin.cxx
index 7fef02cdf522..ac97f4f9b11c 100644
--- a/sw/source/uibase/docvw/AnnotationWin.cxx
+++ b/sw/source/uibase/docvw/AnnotationWin.cxx
@@ -238,7 +238,7 @@ void SwAnnotationWin::UpdateData()
         std::unique_ptr<SwField> pOldField;
         if (rUndoRedo.DoesUndo())
         {
-            pOldField.reset(mpField->Copy());
+            pOldField = mpField->Copy();
         }
         mpField->SetPar2(mpOutliner->GetEditEngine().GetText());
         mpField->SetTextObject(mpOutliner->CreateParaObject());
@@ -401,7 +401,7 @@ void SwAnnotationWin::InitAnswer(OutlinerParaObject const * pText)
     std::unique_ptr<SwField> pOldField;
     if (rUndoRedo.DoesUndo())
     {
-        pOldField.reset(mpField->Copy());
+        pOldField = mpField->Copy();
     }
     mpField->SetPar2(mpOutliner->GetEditEngine().GetText());
     mpField->SetTextObject(mpOutliner->CreateParaObject());
diff --git a/sw/source/uibase/fldui/fldmgr.cxx b/sw/source/uibase/fldui/fldmgr.cxx
index dd70f0bfa9b9..4fe742b92cf6 100644
--- a/sw/source/uibase/fldui/fldmgr.cxx
+++ b/sw/source/uibase/fldui/fldmgr.cxx
@@ -1540,22 +1540,13 @@ bool SwFieldMgr::InsertField(
 void SwFieldMgr::UpdateCurField(sal_uInt32 nFormat,
                             const OUString& rPar1,
                             const OUString& rPar2,
-                            SwField * _pTmpField)
+                            std::unique_ptr<SwField> pTmpField)
 {
     // change format
     OSL_ENSURE(m_pCurField, "no field at CursorPos");
 
-    bool bDelete = false;
-    SwField *pTmpField;       // mb: fixed memory leak
-    if (nullptr != _pTmpField)
-    {
-        pTmpField = _pTmpField;
-    }
-    else
-    {
+    if (!pTmpField)
         pTmpField = m_pCurField->CopyField();
-        bDelete = true;
-    }
 
     SwFieldType* pType   = pTmpField->GetTyp();
     const sal_uInt16 nTypeId = pTmpField->GetTypeId();
@@ -1592,13 +1583,13 @@ void SwFieldMgr::UpdateCurField(sal_uInt32 nFormat,
             nByte = std::max(sal_uInt16(1), nByte);
             nByte = std::min(nByte, sal_uInt16(MAXLEVEL));
             nByte -= 1;
-            static_cast<SwChapterField*>(pTmpField)->SetLevel(static_cast<sal_uInt8>(nByte));
+            static_cast<SwChapterField*>(pTmpField.get())->SetLevel(static_cast<sal_uInt8>(nByte));
             bSetPar2 = false;
             break;
         }
 
         case TYP_SCRIPTFLD:
-            static_cast<SwScriptField*>(pTmpField)->SetCodeURL(static_cast<bool>(nFormat));
+            static_cast<SwScriptField*>(pTmpField.get())->SetCodeURL(static_cast<bool>(nFormat));
             break;
 
         case TYP_NEXTPAGEFLD:
@@ -1642,10 +1633,10 @@ void SwFieldMgr::UpdateCurField(sal_uInt32 nFormat,
         case TYP_GETREFFLD:
             {
                 bSetPar2 = false;
-                static_cast<SwGetRefField*>(pTmpField)->SetSubType( static_cast<sal_uInt16>(rPar2.toInt32()) );
+                static_cast<SwGetRefField*>(pTmpField.get())->SetSubType( static_cast<sal_uInt16>(rPar2.toInt32()) );
                 const sal_Int32 nPos = rPar2.indexOf( '|' );
                 if( nPos>=0 )
-                    static_cast<SwGetRefField*>(pTmpField)->SetSeqNo( static_cast<sal_uInt16>(rPar2.copy( nPos + 1 ).toInt32()));
+                    static_cast<SwGetRefField*>(pTmpField.get())->SetSeqNo( static_cast<sal_uInt16>(rPar2.copy( nPos + 1 ).toInt32()));
             }
             break;
         case TYP_DROPDOWN:
@@ -1655,15 +1646,15 @@ void SwFieldMgr::UpdateCurField(sal_uInt32 nFormat,
             OUString* pArray = aEntries.getArray();
             for(sal_Int32 nToken = 0; nToken < nTokenCount; nToken++)
                 pArray[nToken] = sPar2.getToken(nToken, DB_DELIM);
-            static_cast<SwDropDownField*>(pTmpField)->SetItems(aEntries);
-            static_cast<SwDropDownField*>(pTmpField)->SetName(rPar1);
+            static_cast<SwDropDownField*>(pTmpField.get())->SetItems(aEntries);
+            static_cast<SwDropDownField*>(pTmpField.get())->SetName(rPar1);
             bSetPar1 = bSetPar2 = false;
         }
         break;
         case TYP_AUTHORITY :
         {
             //#i99069# changes to a bibliography field should change the field type
-            SwAuthorityField* pAuthorityField = static_cast<SwAuthorityField*>(pTmpField);
+            SwAuthorityField* pAuthorityField = static_cast<SwAuthorityField*>(pTmpField.get());
             SwAuthorityFieldType* pAuthorityType = static_cast<SwAuthorityFieldType*>(pType);
             SwAuthEntry aTempEntry;
             for( sal_uInt16 i = 0; i < AUTH_FIELD_END; ++i )
@@ -1706,8 +1697,7 @@ void SwFieldMgr::UpdateCurField(sal_uInt32 nFormat,
         GetCurField();
     }
 
-    if (bDelete)
-        delete pTmpField;
+    pTmpField.reset();
 
     pSh->EndAllAction();
 }
diff --git a/sw/source/uibase/inc/fldmgr.hxx b/sw/source/uibase/inc/fldmgr.hxx
index 21153ab96823..c6a29827a06c 100644
--- a/sw/source/uibase/inc/fldmgr.hxx
+++ b/sw/source/uibase/inc/fldmgr.hxx
@@ -23,8 +23,9 @@
 #include <swtypes.hxx>
 #include <com/sun/star/uno/Reference.h>
 #include <com/sun/star/uno/Any.h>
-#include <vector>
 #include <vcl/vclptr.hxx>
+#include <memory>
+#include <vector>
 
 namespace com{namespace sun{namespace star{
     namespace container{
@@ -126,7 +127,7 @@ public:
     void            UpdateCurField(sal_uInt32 nFormat,
                                  const OUString& rPar1,
                                  const OUString& rPar2,
-                                 SwField * _pField = nullptr);
+                                 std::unique_ptr<SwField> _pField = nullptr);
 
     const OUString& GetCurFieldPar1() const { return m_aCurPar1; }
     const OUString& GetCurFieldPar2() const { return m_aCurPar2; }


More information about the Libreoffice-commits mailing list