From 3643a1875b588e730d132a3e401f099a83b866ef Mon Sep 17 00:00:00 2001 From: Don HO Date: Sun, 11 Aug 2019 01:17:53 +0200 Subject: [PATCH] Make 2 singleton "new-less" Make WcharMbcsConvertor & FileManager "new-less" --- PowerEditor/src/MISC/Common/Common.cpp | 2 -- PowerEditor/src/MISC/Common/Common.h | 8 ++++---- PowerEditor/src/Notepad_plus.cpp | 2 -- PowerEditor/src/ScitillaComponent/Buffer.cpp | 2 -- PowerEditor/src/ScitillaComponent/Buffer.h | 8 ++++---- PowerEditor/src/winmain.cpp | 1 - 6 files changed, 8 insertions(+), 15 deletions(-) diff --git a/PowerEditor/src/MISC/Common/Common.cpp b/PowerEditor/src/MISC/Common/Common.cpp index 30b6b987..a22acaaf 100644 --- a/PowerEditor/src/MISC/Common/Common.cpp +++ b/PowerEditor/src/MISC/Common/Common.cpp @@ -38,8 +38,6 @@ #include "../Utf8.h" #include -WcharMbcsConvertor* WcharMbcsConvertor::_pSelf = new WcharMbcsConvertor; - void printInt(int int2print) { TCHAR str[32]; diff --git a/PowerEditor/src/MISC/Common/Common.h b/PowerEditor/src/MISC/Common/Common.h index 97de03bf..dccb0fa2 100644 --- a/PowerEditor/src/MISC/Common/Common.h +++ b/PowerEditor/src/MISC/Common/Common.h @@ -96,8 +96,10 @@ bool matchInList(const TCHAR *fileName, const std::vector & patt class WcharMbcsConvertor final { public: - static WcharMbcsConvertor * getInstance() {return _pSelf;} - static void destroyInstance() {delete _pSelf;} + static WcharMbcsConvertor* getInstance() { + static WcharMbcsConvertor instance; + return &instance; + } const wchar_t * char2wchar(const char *mbStr, UINT codepage, int lenIn=-1, int *pLenOut=NULL, int *pBytesNotProcessed=NULL); const wchar_t * char2wchar(const char *mbcs2Convert, UINT codepage, int *mstart, int *mend); @@ -120,8 +122,6 @@ protected: WcharMbcsConvertor(const WcharMbcsConvertor&) = delete; WcharMbcsConvertor& operator= (const WcharMbcsConvertor&) = delete; - static WcharMbcsConvertor* _pSelf; - template class StringBuffer final { diff --git a/PowerEditor/src/Notepad_plus.cpp b/PowerEditor/src/Notepad_plus.cpp index ef2a15ac..c9ac3e66 100644 --- a/PowerEditor/src/Notepad_plus.cpp +++ b/PowerEditor/src/Notepad_plus.cpp @@ -184,8 +184,6 @@ Notepad_plus::~Notepad_plus() // its children windows' handles will be destroyed automatically! (NppParameters::getInstance())->destroyInstance(); - MainFileManager->destroyInstance(); - (WcharMbcsConvertor::getInstance())->destroyInstance(); delete _pTrayIco; delete _pAnsiCharPanel; diff --git a/PowerEditor/src/ScitillaComponent/Buffer.cpp b/PowerEditor/src/ScitillaComponent/Buffer.cpp index 897fdb1a..13c34e17 100644 --- a/PowerEditor/src/ScitillaComponent/Buffer.cpp +++ b/PowerEditor/src/ScitillaComponent/Buffer.cpp @@ -38,8 +38,6 @@ #include "uchardet.h" #include "LongRunningOperation.h" -FileManager * FileManager::_pSelf = new FileManager(); - static const int blockSize = 128 * 1024 + 4; static const int CR = 0x0D; static const int LF = 0x0A; diff --git a/PowerEditor/src/ScitillaComponent/Buffer.h b/PowerEditor/src/ScitillaComponent/Buffer.h index 7eb68bc3..22c55207 100644 --- a/PowerEditor/src/ScitillaComponent/Buffer.h +++ b/PowerEditor/src/ScitillaComponent/Buffer.h @@ -106,8 +106,10 @@ public: bool deleteFile(BufferID id); bool moveFile(BufferID id, const TCHAR * newFilename); bool createEmptyFile(const TCHAR * path); - static FileManager * getInstance() {return _pSelf;}; - void destroyInstance() { delete _pSelf; }; + static FileManager * getInstance() { + static FileManager instance; + return &instance; + }; int getFileNameFromBuffer(BufferID id, TCHAR * fn2copy); int docLength(Buffer * buffer) const; size_t nextUntitledNewNumber() const; @@ -127,8 +129,6 @@ private: private: - static FileManager *_pSelf; - Notepad_plus* _pNotepadPlus = nullptr; ScintillaEditView* _pscratchTilla = nullptr; Document _scratchDocDefault; diff --git a/PowerEditor/src/winmain.cpp b/PowerEditor/src/winmain.cpp index dd6c91c3..f748a7fe 100644 --- a/PowerEditor/src/winmain.cpp +++ b/PowerEditor/src/winmain.cpp @@ -479,7 +479,6 @@ int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int) { // First of all, destroy static object NppParameters pNppParameters->destroyInstance(); - MainFileManager->destroyInstance(); int sw = 0;