From 10f6ff6c1fa3d0bcf594d03c572e5cb8444cf9a3 Mon Sep 17 00:00:00 2001 From: Don HO Date: Thu, 3 Oct 2019 14:05:50 +0200 Subject: [PATCH] Fix a crash (regression) due to "new-less" of NppParameters The commit 'Make NppParameters singleton "new-less"' make NppParameters instantiate in the stack instead of allocating in the heap: https://github.com/notepad-plus-plus/notepad-plus-plus/commit/3dbb2c4b8ef47fddfd4e78aed086b28108fd036d It makes Notepad++ crash: https://community.notepad-plus-plus.org/topic/18316/notepad-7-8-release-candidate-3/4 The crash is on following code: vector & vShortcuts = NppParameters::getInstance().getMacroList(); size_t nbItems = vShortcuts.size(); for (size_t itemIndex2 = 0; itemIndex2 < nbItems; ++itemIndex2) { ... vShortcuts.size() here return a random large number, which makes crash. The explanation about vector::size() unexpected behaviour: https://stackoverflow.com/questions/30548944/c-vector-size-is-wrong-and-higher-than-the-number-of-elements Obviously the stack size is not large enough to contain NppParameters singleton in 64 bits. --- PowerEditor/src/Parameters.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/PowerEditor/src/Parameters.h b/PowerEditor/src/Parameters.h index 03028df1..0325553f 100644 --- a/PowerEditor/src/Parameters.h +++ b/PowerEditor/src/Parameters.h @@ -1293,8 +1293,8 @@ class NppParameters final { public: static NppParameters& getInstance() { - static NppParameters instance; - return instance; + static NppParameters* instance = new NppParameters; + return *instance; }; static LangType getLangIDFromStr(const TCHAR *langName); static generic_string getLocPathFromStr(const generic_string & localizationCode); @@ -1658,6 +1658,7 @@ private: NppParameters(NppParameters&&) = delete; NppParameters& operator=(NppParameters&&) = delete; + TiXmlDocument *_pXmlDoc = nullptr; TiXmlDocument *_pXmlUserDoc = nullptr; TiXmlDocument *_pXmlUserStylerDoc = nullptr;