Kaydet (Commit) 8629850e authored tarafından Noel Grandin's avatar Noel Grandin

loplugin:useuniqueptr pass SwTextBlocks around by unique_ptr

fix leaks in SwGlossaryHdl::NewGlossary and SwGlossaryHdl::DelGlossary
and SwGlossaryHdl::InsertGlossary error paths

Change-Id: Iaf98fea08cd44bf68885e053854cf65372fcfc2c
Reviewed-on: https://gerrit.libreoffice.org/60495
Tested-by: Jenkins
Reviewed-by: 's avatarNoel Grandin <noel.grandin@collabora.co.uk>
üst 4e59ef98
...@@ -771,8 +771,8 @@ IMPL_LINK_NOARG(SwGlossaryDlg, EditHdl, MenuButton *, void) ...@@ -771,8 +771,8 @@ IMPL_LINK_NOARG(SwGlossaryDlg, EditHdl, MenuButton *, void)
// EndDialog must not be called in MenuHdl // EndDialog must not be called in MenuHdl
if (m_pEditBtn->GetCurItemIdent() == "edit") if (m_pEditBtn->GetCurItemIdent() == "edit")
{ {
SwTextBlocks *pGroup = ::GetGlossaries()->GetGroupDoc ( GetCurrGrpName () ); std::unique_ptr<SwTextBlocks> pGroup = ::GetGlossaries()->GetGroupDoc ( GetCurrGrpName () );
delete pGroup; pGroup.reset();
EndDialog(RET_EDIT); EndDialog(RET_EDIT);
} }
} }
......
...@@ -51,7 +51,7 @@ class SW_DLLPUBLIC SwGlossaries ...@@ -51,7 +51,7 @@ class SW_DLLPUBLIC SwGlossaries
std::vector<OUString> m_GlosArr; std::vector<OUString> m_GlosArr;
bool m_bError; bool m_bError;
SAL_DLLPRIVATE SwTextBlocks* GetGlosDoc(const OUString &rName, bool bCreate = true) const; SAL_DLLPRIVATE std::unique_ptr<SwTextBlocks> GetGlosDoc(const OUString &rName, bool bCreate = true) const;
SAL_DLLPRIVATE std::vector<OUString> & GetNameList(); SAL_DLLPRIVATE std::vector<OUString> & GetNameList();
// implementation in unoatxt.cxx // implementation in unoatxt.cxx
...@@ -102,7 +102,8 @@ public: ...@@ -102,7 +102,8 @@ public:
bool FindGroupName(OUString& rGroup); bool FindGroupName(OUString& rGroup);
SwTextBlocks* GetGroupDoc(const OUString &rName, std::unique_ptr<SwTextBlocks>
GetGroupDoc(const OUString &rName,
bool bCreate = false); bool bCreate = false);
static OUString GetDefName(); static OUString GetDefName();
static OUString GetExtension(); static OUString GetExtension();
......
...@@ -41,7 +41,7 @@ class SW_DLLPUBLIC SwGlossaryHdl ...@@ -41,7 +41,7 @@ class SW_DLLPUBLIC SwGlossaryHdl
SAL_DLLPRIVATE bool Expand( const OUString& rShortName, SAL_DLLPRIVATE bool Expand( const OUString& rShortName,
SwGlossaries* pGlossaries, SwGlossaries* pGlossaries,
SwTextBlocks *pGlossary ); std::unique_ptr<SwTextBlocks> pGlossary );
public: public:
void GlossaryDlg(); void GlossaryDlg();
......
...@@ -145,17 +145,16 @@ OUString SwGlossaries::GetGroupTitle( const OUString& rGroupName ) ...@@ -145,17 +145,16 @@ OUString SwGlossaries::GetGroupTitle( const OUString& rGroupName )
OUString sGroup(rGroupName); OUString sGroup(rGroupName);
if (sGroup.indexOf(GLOS_DELIM)<0) if (sGroup.indexOf(GLOS_DELIM)<0)
FindGroupName(sGroup); FindGroupName(sGroup);
SwTextBlocks* pGroup = GetGroupDoc(sGroup); std::unique_ptr<SwTextBlocks> pGroup = GetGroupDoc(sGroup);
if(pGroup) if(pGroup)
{ {
sRet = pGroup->GetName(); sRet = pGroup->GetName();
delete pGroup;
} }
return sRet; return sRet;
} }
// supplies the group rName's text block document // supplies the group rName's text block document
SwTextBlocks* SwGlossaries::GetGroupDoc(const OUString &rName, std::unique_ptr<SwTextBlocks> SwGlossaries::GetGroupDoc(const OUString &rName,
bool bCreate) bool bCreate)
{ {
// insert to the list of text blocks if applicable // insert to the list of text blocks if applicable
...@@ -186,12 +185,11 @@ bool SwGlossaries::NewGroupDoc(OUString& rGroupName, const OUString& rTitle) ...@@ -186,12 +185,11 @@ bool SwGlossaries::NewGroupDoc(OUString& rGroupName, const OUString& rTitle)
const OUString sNewFilePath(m_PathArr[nNewPath]); const OUString sNewFilePath(m_PathArr[nNewPath]);
const OUString sNewGroup = lcl_CheckFileName(sNewFilePath, rGroupName.getToken(0, GLOS_DELIM)) const OUString sNewGroup = lcl_CheckFileName(sNewFilePath, rGroupName.getToken(0, GLOS_DELIM))
+ OUStringLiteral1(GLOS_DELIM) + sNewPath; + OUStringLiteral1(GLOS_DELIM) + sNewPath;
SwTextBlocks *pBlock = GetGlosDoc( sNewGroup ); std::unique_ptr<SwTextBlocks> pBlock = GetGlosDoc( sNewGroup );
if(pBlock) if(pBlock)
{ {
GetNameList().push_back(sNewGroup); GetNameList().push_back(sNewGroup);
pBlock->SetName(rTitle); pBlock->SetName(rTitle);
delete pBlock;
rGroupName = sNewGroup; rGroupName = sNewGroup;
return true; return true;
} }
...@@ -273,10 +271,10 @@ SwGlossaries::~SwGlossaries() ...@@ -273,10 +271,10 @@ SwGlossaries::~SwGlossaries()
} }
// read a block document // read a block document
SwTextBlocks* SwGlossaries::GetGlosDoc( const OUString &rName, bool bCreate ) const std::unique_ptr<SwTextBlocks> SwGlossaries::GetGlosDoc( const OUString &rName, bool bCreate ) const
{ {
sal_uInt16 nPath = static_cast<sal_uInt16>(rName.getToken(1, GLOS_DELIM).toInt32()); sal_uInt16 nPath = static_cast<sal_uInt16>(rName.getToken(1, GLOS_DELIM).toInt32());
SwTextBlocks *pTmp = nullptr; std::unique_ptr<SwTextBlocks> pTmp;
if (static_cast<size_t>(nPath) < m_PathArr.size()) if (static_cast<size_t>(nPath) < m_PathArr.size())
{ {
const OUString sFileURL = const OUString sFileURL =
...@@ -288,7 +286,7 @@ SwTextBlocks* SwGlossaries::GetGlosDoc( const OUString &rName, bool bCreate ) co ...@@ -288,7 +286,7 @@ SwTextBlocks* SwGlossaries::GetGlosDoc( const OUString &rName, bool bCreate ) co
if (bCreate || bExist) if (bCreate || bExist)
{ {
pTmp = new SwTextBlocks( sFileURL ); pTmp.reset(new SwTextBlocks( sFileURL ));
bool bOk = true; bool bOk = true;
if( pTmp->GetError() ) if( pTmp->GetError() )
{ {
......
...@@ -201,7 +201,7 @@ SwDocShellRef SwGlossaries::EditGroupDoc( const OUString& rGroup, const OUString ...@@ -201,7 +201,7 @@ SwDocShellRef SwGlossaries::EditGroupDoc( const OUString& rGroup, const OUString
{ {
SwDocShellRef xDocSh; SwDocShellRef xDocSh;
SwTextBlocks* pGroup = GetGroupDoc( rGroup ); std::unique_ptr<SwTextBlocks> pGroup = GetGroupDoc( rGroup );
if (pGroup && pGroup->GetCount()) if (pGroup && pGroup->GetCount())
{ {
// query which view is registered. In WebWriter there is no normal view // query which view is registered. In WebWriter there is no normal view
...@@ -268,7 +268,6 @@ SwDocShellRef SwGlossaries::EditGroupDoc( const OUString& rGroup, const OUString ...@@ -268,7 +268,6 @@ SwDocShellRef SwGlossaries::EditGroupDoc( const OUString& rGroup, const OUString
if ( bShow ) if ( bShow )
pFrame->GetFrame().Appear(); pFrame->GetFrame().Appear();
} }
delete pGroup;
return xDocSh; return xDocSh;
} }
......
...@@ -340,7 +340,9 @@ uno::Reference< text::XAutoTextEntry > SwXAutoTextGroup::insertNewByName(const ...@@ -340,7 +340,9 @@ uno::Reference< text::XAutoTextEntry > SwXAutoTextGroup::insertNewByName(const
if(!xTextRange.is()) if(!xTextRange.is())
throw uno::RuntimeException(); throw uno::RuntimeException();
SwTextBlocks* pGlosGroup = pGlossaries ? pGlossaries->GetGroupDoc(m_sGroupName) : nullptr; std::unique_ptr<SwTextBlocks> pGlosGroup;
if (pGlossaries)
pGlosGroup = pGlossaries->GetGroupDoc(m_sGroupName);
const OUString& sShortName(aName); const OUString& sShortName(aName);
const OUString& sLongName(aTitle); const OUString& sLongName(aTitle);
if (pGlosGroup && !pGlosGroup->GetError()) if (pGlosGroup && !pGlosGroup->GetError())
...@@ -398,7 +400,7 @@ uno::Reference< text::XAutoTextEntry > SwXAutoTextGroup::insertNewByName(const ...@@ -398,7 +400,7 @@ uno::Reference< text::XAutoTextEntry > SwXAutoTextGroup::insertNewByName(const
throw uno::RuntimeException(); throw uno::RuntimeException();
} }
} }
delete pGlosGroup; pGlosGroup.reset();
uno::Reference< text::XAutoTextEntry > xEntry; uno::Reference< text::XAutoTextEntry > xEntry;
......
...@@ -353,7 +353,7 @@ AutoTextGroup* SwGlossaryList::FindGroup(const OUString& rGroupName) ...@@ -353,7 +353,7 @@ AutoTextGroup* SwGlossaryList::FindGroup(const OUString& rGroupName)
void SwGlossaryList::FillGroup(AutoTextGroup* pGroup, SwGlossaries* pGlossaries) void SwGlossaryList::FillGroup(AutoTextGroup* pGroup, SwGlossaries* pGlossaries)
{ {
SwTextBlocks* pBlock = pGlossaries->GetGroupDoc(pGroup->sName); std::unique_ptr<SwTextBlocks> pBlock = pGlossaries->GetGroupDoc(pGroup->sName);
pGroup->nCount = pBlock ? pBlock->GetCount() : 0; pGroup->nCount = pBlock ? pBlock->GetCount() : 0;
pGroup->sLongNames.clear(); pGroup->sLongNames.clear();
pGroup->sShortNames.clear(); pGroup->sShortNames.clear();
...@@ -367,7 +367,6 @@ void SwGlossaryList::FillGroup(AutoTextGroup* pGroup, SwGlossaries* pGlossaries) ...@@ -367,7 +367,6 @@ void SwGlossaryList::FillGroup(AutoTextGroup* pGroup, SwGlossaries* pGlossaries)
pGroup->sShortNames += pBlock->GetShortName(j) pGroup->sShortNames += pBlock->GetShortName(j)
+ OUStringLiteral1(STRING_DELIM); + OUStringLiteral1(STRING_DELIM);
} }
delete pBlock;
} }
// Give back all (not exceeding FIND_MAX_GLOS) found modules // Give back all (not exceeding FIND_MAX_GLOS) found modules
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment