From 6e78ac630298f82d2ffcf28041839b7450da9a79 Mon Sep 17 00:00:00 2001 From: Borja Ferrer Date: Sat, 17 Mar 2007 00:55:46 +0000 Subject: [PATCH] fixed several memory leaks fixed plugin listeners being removed before a OnPluginUnloaded Call_AskPluginLoad removed some virtuality all cached ptrs using the Stack system are freed now --HG-- extra : convert_revision : svn%3A39bc706e-5318-0410-9160-8a85361fbb7c/trunk%40647 --- core/ConCmdManager.cpp | 1 - core/GameConfigs.cpp | 1 + core/TimerSys.cpp | 10 ++++++ core/TimerSys.h | 2 ++ core/UserMessages.cpp | 7 ++++ core/smn_timers.cpp | 12 +++++++ core/smn_usermsgs.cpp | 13 ++++++- core/systems/ExtensionSys.cpp | 1 - core/systems/ForwardSys.cpp | 15 +++++--- core/systems/ForwardSys.h | 3 +- core/systems/LibrarySys.h | 32 ++++++++--------- core/systems/PluginSys.cpp | 9 ++++- core/systems/PluginSys.h | 26 +++++++------- core/vm/sp_vm_basecontext.h | 66 +++++++++++++++++----------------- core/vm/sp_vm_engine.h | 15 ++++---- sourcepawn/jit/x86/jit_x86.cpp | 3 +- 16 files changed, 135 insertions(+), 81 deletions(-) diff --git a/core/ConCmdManager.cpp b/core/ConCmdManager.cpp index 11970155..140bc54b 100644 --- a/core/ConCmdManager.cpp +++ b/core/ConCmdManager.cpp @@ -56,7 +56,6 @@ void ConCmdManager::OnSourceModShutdown() /* All commands should already be removed by the time we're done */ SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, SetCommandClient, serverClients, this, &ConCmdManager::SetCommandClient, false); g_RootMenu.RemoveRootConsoleCommand("cmds", this); - g_PluginSys.RemovePluginsListener(this); } void ConCmdManager::RemoveConCmds(List &cmdlist, IPluginContext *pContext) diff --git a/core/GameConfigs.cpp b/core/GameConfigs.cpp index 249b32f3..e23e051f 100644 --- a/core/GameConfigs.cpp +++ b/core/GameConfigs.cpp @@ -46,6 +46,7 @@ CGameConfig::CGameConfig(const char *file) m_pProps = sm_trie_create(); m_pKeys = sm_trie_create(); m_pStrings = new BaseStringTable(512); + m_RefCount = 0; } CGameConfig::~CGameConfig() diff --git a/core/TimerSys.cpp b/core/TimerSys.cpp index 8c90a3ea..79acf385 100644 --- a/core/TimerSys.cpp +++ b/core/TimerSys.cpp @@ -27,6 +27,16 @@ void ITimer::Initialize(ITimedEvent *pCallbacks, float fInterval, float fToExec, m_KillMe = false; } +TimerSystem::~TimerSystem() +{ + CStack::iterator iter; + for (iter=m_FreeTimers.begin(); iter!=m_FreeTimers.end(); iter++) + { + delete (*iter); + } + m_FreeTimers.popall(); +} + void TimerSystem::OnSourceModAllInitialized() { g_ShareSys.AddInterface(NULL, this); diff --git a/core/TimerSys.h b/core/TimerSys.h index 96576004..0da497ce 100644 --- a/core/TimerSys.h +++ b/core/TimerSys.h @@ -44,6 +44,8 @@ class TimerSystem : public ITimerSystem, public SMGlobalClass { +public: + ~TimerSystem(); public: //SMGlobalClass void OnSourceModAllInitialized(); public: //ITimerSystem diff --git a/core/UserMessages.cpp b/core/UserMessages.cpp index 5687d504..08852c40 100644 --- a/core/UserMessages.cpp +++ b/core/UserMessages.cpp @@ -34,6 +34,13 @@ UserMessages::UserMessages() : m_InterceptBuffer(m_pBase, 2500) UserMessages::~UserMessages() { sm_trie_destroy(m_Names); + + CStack::iterator iter; + for (iter=m_FreeListeners.begin(); iter!=m_FreeListeners.end(); iter++) + { + delete (*iter); + } + m_FreeListeners.popall(); } void UserMessages::OnSourceModAllInitialized() diff --git a/core/smn_timers.cpp b/core/smn_timers.cpp index 140787b1..67e4b0b7 100644 --- a/core/smn_timers.cpp +++ b/core/smn_timers.cpp @@ -34,6 +34,8 @@ class TimerNatives : public IHandleTypeDispatch, public ITimedEvent { +public: + ~TimerNatives(); public: //ITimedEvent ResultType OnTimer(ITimer *pTimer, void *pData); void OnTimerEnd(ITimer *pTimer, void *pData); @@ -49,6 +51,16 @@ private: CStack m_FreeTimers; }; +TimerNatives::~TimerNatives() +{ + CStack::iterator iter; + for (iter=m_FreeTimers.begin(); iter!=m_FreeTimers.end(); iter++) + { + delete (*iter); + } + m_FreeTimers.popall(); +} + void TimerNatives::OnSourceModAllInitialized() { HandleAccess sec; diff --git a/core/smn_usermsgs.cpp b/core/smn_usermsgs.cpp index 9bb9305d..1d40e90a 100644 --- a/core/smn_usermsgs.cpp +++ b/core/smn_usermsgs.cpp @@ -34,6 +34,8 @@ class UsrMessageNatives : public IHandleTypeDispatch, public IPluginsListener { +public: + ~UsrMessageNatives(); public: //SMGlobalClass, IHandleTypeDispatch, IPluginListener void OnSourceModAllInitialized(); void OnSourceModShutdown(); @@ -47,6 +49,16 @@ private: CStack m_FreeListeners; }; +UsrMessageNatives::~UsrMessageNatives() +{ + CStack::iterator iter; + for (iter=m_FreeListeners.begin(); iter!=m_FreeListeners.end(); iter++) + { + delete (*iter); + } + m_FreeListeners.popall(); +} + void UsrMessageNatives::OnSourceModAllInitialized() { HandleAccess sec; @@ -70,7 +82,6 @@ void UsrMessageNatives::OnSourceModShutdown() g_HandleSys.RemoveType(g_WrBitBufType, g_pCoreIdent); g_HandleSys.RemoveType(g_RdBitBufType, g_pCoreIdent); - g_PluginSys.RemovePluginsListener(this); g_WrBitBufType = 0; g_RdBitBufType = 0; } diff --git a/core/systems/ExtensionSys.cpp b/core/systems/ExtensionSys.cpp index 7bc8abec..5d77c5b7 100644 --- a/core/systems/ExtensionSys.cpp +++ b/core/systems/ExtensionSys.cpp @@ -264,7 +264,6 @@ void CExtensionManager::OnSourceModAllInitialized() void CExtensionManager::OnSourceModShutdown() { g_RootMenu.RemoveRootConsoleCommand("exts", this); - g_PluginSys.RemovePluginsListener(this); g_ShareSys.DestroyIdentType(g_ExtType); } diff --git a/core/systems/ForwardSys.cpp b/core/systems/ForwardSys.cpp index f0ef70c6..c72d33b1 100644 --- a/core/systems/ForwardSys.cpp +++ b/core/systems/ForwardSys.cpp @@ -32,17 +32,22 @@ CForwardManager g_Forwards; // :TODO: IMPORTANT!!! The result pointer arg in the execute function maybe invalid if the forward fails // so later evaluation of this result may cause problems on higher levels of abstraction. DOCUMENT OR FIX ALL FORWARDS! +CForwardManager::~CForwardManager() +{ + CStack::iterator iter; + for (iter=m_FreeForwards.begin(); iter!=m_FreeForwards.end(); iter++) + { + delete (*iter); + } + m_FreeForwards.popall(); +} + void CForwardManager::OnSourceModAllInitialized() { g_PluginSys.AddPluginsListener(this); g_ShareSys.AddInterface(NULL, this); } -void CForwardManager::OnSourceModShutdown() -{ - g_PluginSys.RemovePluginsListener(this); -} - IForward *CForwardManager::CreateForward(const char *name, ExecType et, unsigned int num_params, const ParamType *types, ...) { CForward *fwd; diff --git a/core/systems/ForwardSys.h b/core/systems/ForwardSys.h index 77d798c6..2f0aeb00 100644 --- a/core/systems/ForwardSys.h +++ b/core/systems/ForwardSys.h @@ -105,6 +105,8 @@ class CForwardManager : public SMGlobalClass { friend class CForward; +public: + ~CForwardManager(); public: //IForwardManager IForward *CreateForward(const char *name, ExecType et, @@ -124,7 +126,6 @@ public: //IPluginsListener void OnPluginPauseChange(IPlugin *plugin, bool paused); public: //SMGlobalClass void OnSourceModAllInitialized(); - void OnSourceModShutdown(); protected: CForward *ForwardMake(); void ForwardFree(CForward *fwd); diff --git a/core/systems/LibrarySys.h b/core/systems/LibrarySys.h index 83009bdf..a7b5610f 100644 --- a/core/systems/LibrarySys.h +++ b/core/systems/LibrarySys.h @@ -31,12 +31,12 @@ public: CDirectory(const char *path); ~CDirectory(); public: - virtual bool MoreFiles(); - virtual void NextEntry(); - virtual const char *GetEntryName(); - virtual bool IsEntryDirectory(); - virtual bool IsEntryFile(); - virtual bool IsEntryValid(); + bool MoreFiles(); + void NextEntry(); + const char *GetEntryName(); + bool IsEntryDirectory(); + bool IsEntryFile(); + bool IsEntryValid(); public: bool IsValid(); private: @@ -56,8 +56,8 @@ public: CLibrary(LibraryHandle me); ~CLibrary(); public: - virtual void CloseLibrary(); - virtual void *GetSymbolAddress(const char *symname); + void CloseLibrary(); + void *GetSymbolAddress(const char *symname); private: LibraryHandle m_lib; }; @@ -65,14 +65,14 @@ private: class LibrarySystem : public ILibrarySys { public: - virtual ILibrary *OpenLibrary(const char *path, char *error, size_t err_max); - virtual IDirectory *OpenDirectory(const char *path); - virtual void CloseDirectory(IDirectory *dir); - virtual bool PathExists(const char *path); - virtual bool IsPathFile(const char *path); - virtual bool IsPathDirectory(const char *path); - virtual void GetPlatformError(char *error, size_t err_max); - virtual size_t PathFormat(char *buffer, size_t len, const char *fmt, ...); + ILibrary *OpenLibrary(const char *path, char *error, size_t err_max); + IDirectory *OpenDirectory(const char *path); + void CloseDirectory(IDirectory *dir); + bool PathExists(const char *path); + bool IsPathFile(const char *path); + bool IsPathDirectory(const char *path); + void GetPlatformError(char *error, size_t err_max); + size_t PathFormat(char *buffer, size_t len, const char *fmt, ...); }; extern LibrarySystem g_LibSys; diff --git a/core/systems/PluginSys.cpp b/core/systems/PluginSys.cpp index 95b4f4e7..84214b76 100644 --- a/core/systems/PluginSys.cpp +++ b/core/systems/PluginSys.cpp @@ -78,7 +78,7 @@ CPlugin::~CPlugin() g_pSourcePawn->FreeFromMemory(m_plugin); m_plugin = NULL; } - if (!m_pProps) + if (m_pProps) { sm_trie_destroy(m_pProps); } @@ -649,6 +649,13 @@ CPluginManager::~CPluginManager() */ sm_trie_destroy(m_LoadLookup); sm_trie_destroy(m_pNativeLookup); + + CStack::iterator iter; + for (iter=m_iters.begin(); iter!=m_iters.end(); iter++) + { + delete (*iter); + } + m_iters.popall(); } void CPluginManager::LoadAll_FirstPass(const char *config, const char *basedir) diff --git a/core/systems/PluginSys.h b/core/systems/PluginSys.h index d68340c2..3f010148 100644 --- a/core/systems/PluginSys.h +++ b/core/systems/PluginSys.h @@ -116,19 +116,19 @@ public: CPlugin(const char *file); ~CPlugin(); public: - virtual PluginType GetType(); - virtual SourcePawn::IPluginContext *GetBaseContext(); - virtual sp_context_t *GetContext(); - virtual const sm_plugininfo_t *GetPublicInfo(); - virtual const char *GetFilename(); - virtual bool IsDebugging(); - virtual PluginStatus GetStatus(); - virtual bool SetPauseState(bool paused); - virtual unsigned int GetSerial(); - virtual const sp_plugin_t *GetPluginStructure(); - virtual IdentityToken_t *GetIdentity(); - virtual bool SetProperty(const char *prop, void *ptr); - virtual bool GetProperty(const char *prop, void **ptr, bool remove=false); + PluginType GetType(); + SourcePawn::IPluginContext *GetBaseContext(); + sp_context_t *GetContext(); + const sm_plugininfo_t *GetPublicInfo(); + const char *GetFilename(); + bool IsDebugging(); + PluginStatus GetStatus(); + bool SetPauseState(bool paused); + unsigned int GetSerial(); + const sp_plugin_t *GetPluginStructure(); + IdentityToken_t *GetIdentity(); + bool SetProperty(const char *prop, void *ptr); + bool GetProperty(const char *prop, void **ptr, bool remove=false); public: /** * Creates a plugin object with default values. diff --git a/core/vm/sp_vm_basecontext.h b/core/vm/sp_vm_basecontext.h index 43d6d511..5f71bcea 100644 --- a/core/vm/sp_vm_basecontext.h +++ b/core/vm/sp_vm_basecontext.h @@ -36,43 +36,43 @@ namespace SourcePawn bool IsDebugging(); int SetDebugBreak(SPVM_DEBUGBREAK newpfn, SPVM_DEBUGBREAK *oldpfn); IPluginDebugInfo *GetDebugInfo(); - virtual int HeapAlloc(unsigned int cells, cell_t *local_addr, cell_t **phys_addr); - virtual int HeapPop(cell_t local_addr); - virtual int HeapRelease(cell_t local_addr); - virtual int FindNativeByName(const char *name, uint32_t *index); - virtual int GetNativeByIndex(uint32_t index, sp_native_t **native); - virtual uint32_t GetNativesNum(); - virtual int FindPublicByName(const char *name, uint32_t *index); - virtual int GetPublicByIndex(uint32_t index, sp_public_t **publicptr); - virtual uint32_t GetPublicsNum(); - virtual int GetPubvarByIndex(uint32_t index, sp_pubvar_t **pubvar); - virtual int FindPubvarByName(const char *name, uint32_t *index); - virtual int GetPubvarAddrs(uint32_t index, cell_t *local_addr, cell_t **phys_addr); - virtual uint32_t GetPubVarsNum(); - virtual int LocalToPhysAddr(cell_t local_addr, cell_t **phys_addr); - virtual int LocalToString(cell_t local_addr, char **addr); - virtual int StringToLocal(cell_t local_addr, size_t chars, const char *source); - virtual int StringToLocalUTF8(cell_t local_addr, size_t maxbytes, const char *source, size_t *wrtnbytes); - virtual int PushCell(cell_t value); - virtual int PushCellArray(cell_t *local_addr, cell_t **phys_addr, cell_t array[], unsigned int numcells); - virtual int PushString(cell_t *local_addr, char **phys_addr, const char *string); - virtual int PushCellsFromArray(cell_t array[], unsigned int numcells); - virtual int BindNatives(const sp_nativeinfo_t *natives, unsigned int num, int overwrite); - virtual int BindNative(const sp_nativeinfo_t *native); - virtual int BindNativeToAny(SPVM_NATIVE_FUNC native); - virtual int Execute(uint32_t code_addr, cell_t *result); - virtual cell_t ThrowNativeErrorEx(int error, const char *msg, ...); - virtual cell_t ThrowNativeError(const char *msg, ...); - virtual IPluginFunction *GetFunctionByName(const char *public_name); - virtual IPluginFunction *GetFunctionById(funcid_t func_id); + int HeapAlloc(unsigned int cells, cell_t *local_addr, cell_t **phys_addr); + int HeapPop(cell_t local_addr); + int HeapRelease(cell_t local_addr); + int FindNativeByName(const char *name, uint32_t *index); + int GetNativeByIndex(uint32_t index, sp_native_t **native); + uint32_t GetNativesNum(); + int FindPublicByName(const char *name, uint32_t *index); + int GetPublicByIndex(uint32_t index, sp_public_t **publicptr); + uint32_t GetPublicsNum(); + int GetPubvarByIndex(uint32_t index, sp_pubvar_t **pubvar); + int FindPubvarByName(const char *name, uint32_t *index); + int GetPubvarAddrs(uint32_t index, cell_t *local_addr, cell_t **phys_addr); + uint32_t GetPubVarsNum(); + int LocalToPhysAddr(cell_t local_addr, cell_t **phys_addr); + int LocalToString(cell_t local_addr, char **addr); + int StringToLocal(cell_t local_addr, size_t chars, const char *source); + int StringToLocalUTF8(cell_t local_addr, size_t maxbytes, const char *source, size_t *wrtnbytes); + int PushCell(cell_t value); + int PushCellArray(cell_t *local_addr, cell_t **phys_addr, cell_t array[], unsigned int numcells); + int PushString(cell_t *local_addr, char **phys_addr, const char *string); + int PushCellsFromArray(cell_t array[], unsigned int numcells); + int BindNatives(const sp_nativeinfo_t *natives, unsigned int num, int overwrite); + int BindNative(const sp_nativeinfo_t *native); + int BindNativeToAny(SPVM_NATIVE_FUNC native); + int Execute(uint32_t code_addr, cell_t *result); + cell_t ThrowNativeErrorEx(int error, const char *msg, ...); + cell_t ThrowNativeError(const char *msg, ...); + IPluginFunction *GetFunctionByName(const char *public_name); + IPluginFunction *GetFunctionById(funcid_t func_id); #if defined SOURCEMOD_BUILD - virtual SourceMod::IdentityToken_t *GetIdentity(); + SourceMod::IdentityToken_t *GetIdentity(); void SetIdentity(SourceMod::IdentityToken_t *token); #endif public: //IPluginDebugInfo - virtual int LookupFile(ucell_t addr, const char **filename); - virtual int LookupFunction(ucell_t addr, const char **name); - virtual int LookupLine(ucell_t addr, uint32_t *line); + int LookupFile(ucell_t addr, const char **filename); + int LookupFunction(ucell_t addr, const char **name); + int LookupLine(ucell_t addr, uint32_t *line); public: void SetContext(sp_context_t *_ctx); private: diff --git a/core/vm/sp_vm_engine.h b/core/vm/sp_vm_engine.h index f43c6b91..c4cf835c 100644 --- a/core/vm/sp_vm_engine.h +++ b/core/vm/sp_vm_engine.h @@ -32,13 +32,13 @@ class CContextTrace : public IContextTrace public: CContextTrace(TracedCall *pStart, int error, const char *msg, uint32_t native); public: - virtual int GetErrorCode(); - virtual const char *GetErrorString(); - virtual bool DebugInfoAvailable(); - virtual const char *GetCustomErrorString(); - virtual bool GetTraceInfo(CallStackInfo *trace); - virtual void ResetTrace(); - virtual const char *GetLastNative(uint32_t *index); + int GetErrorCode(); + const char *GetErrorString(); + bool DebugInfoAvailable(); + const char *GetCustomErrorString(); + bool GetTraceInfo(CallStackInfo *trace); + void ResetTrace(); + const char *GetLastNative(uint32_t *index); private: int m_Error; const char *m_pMsg; @@ -47,7 +47,6 @@ private: uint32_t m_Native; }; - class SourcePawnEngine : public ISourcePawnEngine { public: diff --git a/sourcepawn/jit/x86/jit_x86.cpp b/sourcepawn/jit/x86/jit_x86.cpp index df505752..36f305d3 100644 --- a/sourcepawn/jit/x86/jit_x86.cpp +++ b/sourcepawn/jit/x86/jit_x86.cpp @@ -2258,7 +2258,8 @@ void JITX86::FreeContext(sp_context_t *ctx) delete [] ctx->symbols; engine->BaseFree(ctx->vm[JITVARS_REBASE]); free(((tracker_t *)(ctx->vm[JITVARS_TRACKER]))->pBase); - delete (tracker_t *)ctx->vm[JITVARS_TRACKER]; + delete (tracker_t *)ctx->vm[JITVARS_TRACKER]; + delete (functracker_t *)ctx->vm[JITVARS_FUNCINFO]; delete ctx; }