Fix a memory leak issue in NppParameters::writeSession

Solution: simplify the method NppParameters::getSessionFromXmlTree by removing unecessary variable member NppParameters::_pXmlSessionDoc.
This commit is contained in:
Don HO 2021-01-17 19:47:45 +01:00
parent 477dd1dbb0
commit a2ee3e6254
No known key found for this signature in database
GPG Key ID: 6C429F1D8D84F46E
2 changed files with 24 additions and 37 deletions

View File

@ -1433,20 +1433,19 @@ bool NppParameters::load()
const NppGUI & nppGUI = (NppParameters::getInstance()).getNppGUI(); const NppGUI & nppGUI = (NppParameters::getInstance()).getNppGUI();
if (nppGUI._rememberLastSession) if (nppGUI._rememberLastSession)
{ {
_pXmlSessionDoc = new TiXmlDocument(_sessionPath); TiXmlDocument* pXmlSessionDoc = new TiXmlDocument(_sessionPath);
loadOkay = _pXmlSessionDoc->LoadFile(); loadOkay = pXmlSessionDoc->LoadFile();
if (!loadOkay) if (!loadOkay)
isAllLaoded = false; isAllLaoded = false;
else else
getSessionFromXmlTree(); getSessionFromXmlTree(pXmlSessionDoc, _session);
delete pXmlSessionDoc;
delete _pXmlSessionDoc;
for (size_t i = 0, len = _pXmlExternalLexerDoc.size() ; i < len ; ++i) for (size_t i = 0, len = _pXmlExternalLexerDoc.size() ; i < len ; ++i)
if (_pXmlExternalLexerDoc[i]) if (_pXmlExternalLexerDoc[i])
delete _pXmlExternalLexerDoc[i]; delete _pXmlExternalLexerDoc[i];
_pXmlSessionDoc = nullptr;
} }
//------------------------------// //------------------------------//
@ -1482,7 +1481,6 @@ void NppParameters::destroyInstance()
delete _pXmlToolIconsDoc; delete _pXmlToolIconsDoc;
delete _pXmlShortcutDoc; delete _pXmlShortcutDoc;
delete _pXmlContextMenuDocA; delete _pXmlContextMenuDocA;
delete _pXmlSessionDoc;
delete _pXmlBlacklistDoc; delete _pXmlBlacklistDoc;
delete getInstancePointer(); delete getInstancePointer();
} }
@ -2048,31 +2046,19 @@ bool NppParameters::loadSession(Session & session, const TCHAR *sessionFileName)
TiXmlDocument *pXmlSessionDocument = new TiXmlDocument(sessionFileName); TiXmlDocument *pXmlSessionDocument = new TiXmlDocument(sessionFileName);
bool loadOkay = pXmlSessionDocument->LoadFile(); bool loadOkay = pXmlSessionDocument->LoadFile();
if (loadOkay) if (loadOkay)
loadOkay = getSessionFromXmlTree(pXmlSessionDocument, &session); loadOkay = getSessionFromXmlTree(pXmlSessionDocument, session);
delete pXmlSessionDocument; delete pXmlSessionDocument;
return loadOkay; return loadOkay;
} }
bool NppParameters::getSessionFromXmlTree(TiXmlDocument *pSessionDoc, Session *pSession) bool NppParameters::getSessionFromXmlTree(TiXmlDocument *pSessionDoc, Session& session)
{ {
if ((pSessionDoc) && (!pSession)) if (!pSessionDoc)
return false; return false;
TiXmlDocument **ppSessionDoc = &_pXmlSessionDoc; TiXmlNode *root = pSessionDoc->FirstChild(TEXT("NotepadPlus"));
Session *ptrSession = &_session;
if (pSessionDoc)
{
ppSessionDoc = &pSessionDoc;
ptrSession = pSession;
}
if (!*ppSessionDoc)
return false;
TiXmlNode *root = (*ppSessionDoc)->FirstChild(TEXT("NotepadPlus"));
if (!root) if (!root)
return false; return false;
@ -2085,7 +2071,7 @@ bool NppParameters::getSessionFromXmlTree(TiXmlDocument *pSessionDoc, Session *p
const TCHAR *str = actView->Attribute(TEXT("activeView"), &index); const TCHAR *str = actView->Attribute(TEXT("activeView"), &index);
if (str) if (str)
{ {
(*ptrSession)._activeView = index; session._activeView = index;
} }
const size_t nbView = 2; const size_t nbView = 2;
@ -2102,9 +2088,9 @@ bool NppParameters::getSessionFromXmlTree(TiXmlDocument *pSessionDoc, Session *p
if (str) if (str)
{ {
if (k == 0) if (k == 0)
(*ptrSession)._activeMainIndex = index2; session._activeMainIndex = index2;
else // k == 1 else // k == 1
(*ptrSession)._activeSubIndex = index2; session._activeSubIndex = index2;
} }
for (TiXmlNode *childNode = viewRoots[k]->FirstChildElement(TEXT("File")); for (TiXmlNode *childNode = viewRoots[k]->FirstChildElement(TEXT("File"));
childNode ; childNode ;
@ -2196,9 +2182,9 @@ bool NppParameters::getSessionFromXmlTree(TiXmlDocument *pSessionDoc, Session *p
} }
} }
if (k == 0) if (k == 0)
(*ptrSession)._mainViewFiles.push_back(sfi); session._mainViewFiles.push_back(sfi);
else // k == 1 else // k == 1
(*ptrSession)._subViewFiles.push_back(sfi); session._subViewFiles.push_back(sfi);
} }
} }
} }
@ -2211,7 +2197,7 @@ bool NppParameters::getSessionFromXmlTree(TiXmlDocument *pSessionDoc, Session *p
const TCHAR *selectedItemPath = (fileBrowserRoot->ToElement())->Attribute(TEXT("latestSelectedItem")); const TCHAR *selectedItemPath = (fileBrowserRoot->ToElement())->Attribute(TEXT("latestSelectedItem"));
if (selectedItemPath) if (selectedItemPath)
{ {
(*ptrSession)._fileBrowserSelectedItem = selectedItemPath; session._fileBrowserSelectedItem = selectedItemPath;
} }
for (TiXmlNode *childNode = fileBrowserRoot->FirstChildElement(TEXT("root")); for (TiXmlNode *childNode = fileBrowserRoot->FirstChildElement(TEXT("root"));
@ -2221,7 +2207,7 @@ bool NppParameters::getSessionFromXmlTree(TiXmlDocument *pSessionDoc, Session *p
const TCHAR *fileName = (childNode->ToElement())->Attribute(TEXT("foldername")); const TCHAR *fileName = (childNode->ToElement())->Attribute(TEXT("foldername"));
if (fileName) if (fileName)
{ {
(*ptrSession)._fileBrowserRoots.push_back({ fileName }); session._fileBrowserRoots.push_back({ fileName });
} }
} }
} }
@ -3176,12 +3162,12 @@ void NppParameters::writeSession(const Session & session, const TCHAR *fileName)
{ {
const TCHAR *pathName = fileName?fileName:_sessionPath.c_str(); const TCHAR *pathName = fileName?fileName:_sessionPath.c_str();
_pXmlSessionDoc = new TiXmlDocument(pathName); TiXmlDocument* pXmlSessionDoc = new TiXmlDocument(pathName);
TiXmlDeclaration* decl = new TiXmlDeclaration(TEXT("1.0"), TEXT("UTF-8"), TEXT("")); TiXmlDeclaration* decl = new TiXmlDeclaration(TEXT("1.0"), TEXT("UTF-8"), TEXT(""));
_pXmlSessionDoc->LinkEndChild(decl); pXmlSessionDoc->LinkEndChild(decl);
TiXmlNode *root = _pXmlSessionDoc->InsertEndChild(TiXmlElement(TEXT("NotepadPlus"))); TiXmlNode *root = pXmlSessionDoc->InsertEndChild(TiXmlElement(TEXT("NotepadPlus")));
if (root) if (root)
{ {
@ -3267,7 +3253,9 @@ void NppParameters::writeSession(const Session & session, const TCHAR *fileName)
} }
} }
} }
_pXmlSessionDoc->SaveFile(); pXmlSessionDoc->SaveFile();
delete pXmlSessionDoc;
} }

View File

@ -1715,7 +1715,6 @@ private:
std::vector<UdlXmlFileState> _pXmlUserLangsDoc; std::vector<UdlXmlFileState> _pXmlUserLangsDoc;
TiXmlDocument *_pXmlToolIconsDoc = nullptr; TiXmlDocument *_pXmlToolIconsDoc = nullptr;
TiXmlDocument *_pXmlShortcutDoc = nullptr; TiXmlDocument *_pXmlShortcutDoc = nullptr;
TiXmlDocument *_pXmlSessionDoc = nullptr;
TiXmlDocument *_pXmlBlacklistDoc = nullptr; TiXmlDocument *_pXmlBlacklistDoc = nullptr;
TiXmlDocumentA *_pXmlNativeLangDocA = nullptr; TiXmlDocumentA *_pXmlNativeLangDocA = nullptr;
@ -1857,7 +1856,7 @@ private:
bool getUserCmdsFromXmlTree(); bool getUserCmdsFromXmlTree();
bool getPluginCmdsFromXmlTree(); bool getPluginCmdsFromXmlTree();
bool getScintKeysFromXmlTree(); bool getScintKeysFromXmlTree();
bool getSessionFromXmlTree(TiXmlDocument *pSessionDoc = NULL, Session *session = NULL); bool getSessionFromXmlTree(TiXmlDocument *pSessionDoc, Session& session);
bool getBlackListFromXmlTree(); bool getBlackListFromXmlTree();
void feedGUIParameters(TiXmlNode *node); void feedGUIParameters(TiXmlNode *node);