From 11e5fe8879406398842ba4ddc5bc1958726b93e5 Mon Sep 17 00:00:00 2001 From: Gregory Hainaut Date: Mon, 18 Jul 2016 19:17:59 +0200 Subject: [PATCH] pcsx2 gui: fix a pseudo memory leak on SavestateEntryPack It is a pseudo leak as it is a global object. Nevertheless it pollutes the valgrind log. v2: * use an array of unique pointer * use ArraySize instead of a constant --- pcsx2/gui/SysState.cpp | 87 ++++++++++++++++++++++-------------------- 1 file changed, 45 insertions(+), 42 deletions(-) diff --git a/pcsx2/gui/SysState.cpp b/pcsx2/gui/SysState.cpp index bb862cd618..ac1717de3c 100644 --- a/pcsx2/gui/SysState.cpp +++ b/pcsx2/gui/SysState.cpp @@ -42,9 +42,10 @@ class BaseSavestateEntry { protected: BaseSavestateEntry() {} - virtual ~BaseSavestateEntry() throw() {} public: + virtual ~BaseSavestateEntry() throw() {} + virtual wxString GetFilename() const=0; virtual void FreezeIn( pxInputStream& reader ) const=0; virtual void FreezeOut( SaveStateBase& writer ) const=0; @@ -142,10 +143,12 @@ void PluginSavestateEntry::FreezeOut( SaveStateBase& writer ) const class SavestateEntry_EmotionMemory : public MemorySavestateEntry { public: + virtual ~SavestateEntry_EmotionMemory() throw() {} + wxString GetFilename() const { return L"eeMemory.bin"; } u8* GetDataPtr() const { return eeMem->Main; } uint GetDataSize() const { return sizeof(eeMem->Main); } - + virtual void FreezeIn( pxInputStream& reader ) const { SysClearExecutionCache(); @@ -156,6 +159,8 @@ public: class SavestateEntry_IopMemory : public MemorySavestateEntry { public: + virtual ~SavestateEntry_IopMemory() throw() {} + wxString GetFilename() const { return L"iopMemory.bin"; } u8* GetDataPtr() const { return iopMem->Main; } uint GetDataSize() const { return sizeof(iopMem->Main); } @@ -164,6 +169,8 @@ public: class SavestateEntry_HwRegs : public MemorySavestateEntry { public: + virtual ~SavestateEntry_HwRegs() throw() {} + wxString GetFilename() const { return L"eeHwRegs.bin"; } u8* GetDataPtr() const { return eeHw; } uint GetDataSize() const { return sizeof(eeHw); } @@ -172,6 +179,8 @@ public: class SavestateEntry_IopHwRegs : public MemorySavestateEntry { public: + virtual ~SavestateEntry_IopHwRegs() throw() {} + wxString GetFilename() const { return L"iopHwRegs.bin"; } u8* GetDataPtr() const { return iopHw; } uint GetDataSize() const { return sizeof(iopHw); } @@ -180,6 +189,8 @@ public: class SavestateEntry_Scratchpad : public MemorySavestateEntry { public: + virtual ~SavestateEntry_Scratchpad() throw() {} + wxString GetFilename() const { return L"Scratchpad.bin"; } u8* GetDataPtr() const { return eeMem->Scratch; } uint GetDataSize() const { return sizeof(eeMem->Scratch); } @@ -188,6 +199,8 @@ public: class SavestateEntry_VU0mem : public MemorySavestateEntry { public: + virtual ~SavestateEntry_VU0mem() throw() {} + wxString GetFilename() const { return L"vu0Memory.bin"; } u8* GetDataPtr() const { return vuRegs[0].Mem; } uint GetDataSize() const { return VU0_MEMSIZE; } @@ -196,6 +209,8 @@ public: class SavestateEntry_VU1mem : public MemorySavestateEntry { public: + virtual ~SavestateEntry_VU1mem() throw() {} + wxString GetFilename() const { return L"vu1Memory.bin"; } u8* GetDataPtr() const { return vuRegs[1].Mem; } uint GetDataSize() const { return VU1_MEMSIZE; } @@ -204,6 +219,8 @@ public: class SavestateEntry_VU0prog : public MemorySavestateEntry { public: + virtual ~SavestateEntry_VU0prog() throw() {} + wxString GetFilename() const { return L"vu0MicroMem.bin"; } u8* GetDataPtr() const { return vuRegs[0].Micro; } uint GetDataSize() const { return VU0_PROGSIZE; } @@ -212,6 +229,8 @@ public: class SavestateEntry_VU1prog : public MemorySavestateEntry { public: + virtual ~SavestateEntry_VU1prog() throw() {} + wxString GetFilename() const { return L"vu1MicroMem.bin"; } u8* GetDataPtr() const { return vuRegs[1].Micro; } uint GetDataSize() const { return VU1_PROGSIZE; } @@ -227,42 +246,26 @@ public: // would not be useful). // -static const uint NumSavestateEntries = 9 + PluginId_Count; +static const std::unique_ptr SavestateEntries[] = { + std::unique_ptr(new SavestateEntry_EmotionMemory), + std::unique_ptr(new SavestateEntry_IopMemory), + std::unique_ptr(new SavestateEntry_HwRegs), + std::unique_ptr(new SavestateEntry_IopHwRegs), + std::unique_ptr(new SavestateEntry_Scratchpad), + std::unique_ptr(new SavestateEntry_VU0mem), + std::unique_ptr(new SavestateEntry_VU1mem), + std::unique_ptr(new SavestateEntry_VU0prog), + std::unique_ptr(new SavestateEntry_VU1prog), -class SavestateEntryPack : public ScopedAlloc -{ - typedef ScopedAlloc _parent; - -public: - SavestateEntryPack() - : _parent( NumSavestateEntries ) - { - uint i = 0; // more convenient in case we re-arrange anything... - - this->operator[](i++) = new SavestateEntry_EmotionMemory; - this->operator[](i++) = new SavestateEntry_IopMemory; - this->operator[](i++) = new SavestateEntry_HwRegs; - this->operator[](i++) = new SavestateEntry_IopHwRegs; - this->operator[](i++) = new SavestateEntry_Scratchpad; - this->operator[](i++) = new SavestateEntry_VU0mem; - this->operator[](i++) = new SavestateEntry_VU1mem; - this->operator[](i++) = new SavestateEntry_VU0prog; - this->operator[](i++) = new SavestateEntry_VU1prog; - - this->operator[](i++) = new PluginSavestateEntry( PluginId_GS ); - this->operator[](i++) = new PluginSavestateEntry( PluginId_PAD ); - this->operator[](i++) = new PluginSavestateEntry( PluginId_SPU2 ); - this->operator[](i++) = new PluginSavestateEntry( PluginId_CDVD ); - this->operator[](i++) = new PluginSavestateEntry( PluginId_USB ); - this->operator[](i++) = new PluginSavestateEntry( PluginId_FW ); - this->operator[](i++) = new PluginSavestateEntry( PluginId_DEV9 ); - } - - using _parent::operator[]; + std::unique_ptr(new PluginSavestateEntry( PluginId_GS )), + std::unique_ptr(new PluginSavestateEntry( PluginId_PAD )), + std::unique_ptr(new PluginSavestateEntry( PluginId_SPU2 )), + std::unique_ptr(new PluginSavestateEntry( PluginId_CDVD )), + std::unique_ptr(new PluginSavestateEntry( PluginId_USB )), + std::unique_ptr(new PluginSavestateEntry( PluginId_FW )), + std::unique_ptr(new PluginSavestateEntry( PluginId_DEV9 )) }; -static const SavestateEntryPack SavestateEntries; - // It's bad mojo to have savestates trying to read and write from the same file at the // same time. To prevent that we use this mutex lock, which is used by both the // CompressThread and the UnzipFromDisk events. (note that CompressThread locks the @@ -275,7 +278,7 @@ static void CheckVersion( pxInputStream& thr ) { u32 savever; thr.Read( savever ); - + // Major version mismatch. Means we can't load this savestate at all. Support for it // was removed entirely. if( savever > g_SaveVersion ) @@ -336,7 +339,7 @@ protected: internals.SetDataSize( saveme.GetCurrentPos() - internals.GetDataIndex() ); m_dest_list->Add( internals ); - for (uint i=0; iFreezeOut( saveme ); @@ -527,10 +530,10 @@ protected: bool foundVersion = false; //bool foundScreenshot = false; - //bool foundEntry[numSavestateEntries] = false; + //bool foundEntry[ArraySize(SavestateEntries)] = false; std::unique_ptr foundInternal; - std::unique_ptr foundEntry[NumSavestateEntries]; + std::unique_ptr foundEntry[ArraySize(SavestateEntries)]; while(true) { @@ -561,7 +564,7 @@ protected: foundScreenshot = true; }*/ - for (uint i=0; iGetName().CmpNoCase(SavestateEntries[i]->GetFilename()) == 0) { @@ -582,7 +585,7 @@ protected: // Log any parts and pieces that are missing, and then generate an exception. bool throwIt = false; - for (uint i=0; i