Permalink
Browse files

Use references instead of pointers for CModInfo

This works fine and reduces the chances of memory leaks.

Signed-off-by: Uli Schlachter <psychon@znc.in>
  • Loading branch information...
1 parent d683aea commit ca97dca6dc705f216778e71ef76fc0f669bc80ed @psychon psychon committed Jun 11, 2011
Showing with 19 additions and 30 deletions.
  1. +11 −20 Modules.cpp
  2. +8 −10 Modules.h
View
@@ -817,7 +817,7 @@ bool CModules::LoadModule(const CString& sModule, const CString& sArgs, CUser* p
CString sModPath, sDataPath;
bool bVersionMismatch;
- CModInfo* Info;
+ CModInfo Info;
if (!FindModPath(sModule, sModPath, sDataPath)) {
sRetMsg = "Unable to find module [" + sModule + "]";
@@ -835,31 +835,28 @@ bool CModules::LoadModule(const CString& sModule, const CString& sArgs, CUser* p
return false;
}
- if ((pUser == NULL) != Info->IsGlobal()) {
- delete Info;
+ if ((pUser == NULL) != Info.IsGlobal()) {
dlclose(p);
sRetMsg = "Module [" + sModule + "] is ";
- sRetMsg += Info->IsGlobal() ? "" : "not ";
+ sRetMsg += Info.IsGlobal() ? "" : "not ";
sRetMsg += "a global module.";
return false;
}
CModule* pModule = NULL;
if (pUser) {
- pModule = Info->GetLoader()(p, pUser, sModule, sDataPath);
+ pModule = Info.GetLoader()(p, pUser, sModule, sDataPath);
} else {
- pModule = Info->GetGlobalLoader()(p, sModule, sDataPath);
+ pModule = Info.GetGlobalLoader()(p, sModule, sDataPath);
}
- pModule->SetDescription(Info->GetDescription());
- pModule->SetGlobal(Info->IsGlobal());
+ pModule->SetDescription(Info.GetDescription());
+ pModule->SetGlobal(Info.IsGlobal());
pModule->SetArgs(sArgs);
pModule->SetModPath(CDir::ChangeDir(CZNC::Get().GetCurPath(), sModPath));
push_back(pModule);
- delete Info;
-
bool bLoaded;
try {
bLoaded = pModule->OnLoad(sArgs, sRetMsg);
@@ -955,24 +952,19 @@ bool CModules::GetModInfo(CModInfo& ModInfo, const CString& sModule, CString& sR
bool CModules::GetModPathInfo(CModInfo& ModInfo, const CString& sModule, const CString& sModPath, CString& sRetMsg) {
bool bVersionMismatch;
- CModInfo* Info;
- ModHandle p = OpenModule(sModule, sModPath, bVersionMismatch, Info, sRetMsg);
+ ModHandle p = OpenModule(sModule, sModPath, bVersionMismatch, ModInfo, sRetMsg);
if (!p)
return false;
- if (Info) {
- ModInfo = *Info;
- }
ModInfo.SetName(sModule);
ModInfo.SetPath(sModPath);
if (bVersionMismatch) {
ModInfo.SetDescription("--- Version mismatch, recompile this module. ---");
}
- delete Info;
dlclose(p);
return true;
@@ -1057,10 +1049,9 @@ CModules::ModDirList CModules::GetModDirs() {
}
ModHandle CModules::OpenModule(const CString& sModule, const CString& sModPath, bool &bVersionMismatch,
- CModInfo*& Info, CString& sRetMsg) {
+ CModInfo& Info, CString& sRetMsg) {
// Some sane defaults in case anything errors out below
bVersionMismatch = false;
- Info = NULL;
sRetMsg.clear();
for (unsigned int a = 0; a < sModule.length(); a++) {
@@ -1098,7 +1089,7 @@ ModHandle CModules::OpenModule(const CString& sModule, const CString& sModPath,
return NULL;
}
- typedef CModInfo* (*InfoFP)();
+ typedef void (*InfoFP)(CModInfo&);
InfoFP ZNCModInfo = (InfoFP) dlsym(p, "ZNCModInfo");
if (!ZNCModInfo) {
@@ -1111,7 +1102,7 @@ ModHandle CModules::OpenModule(const CString& sModule, const CString& sModPath,
bVersionMismatch = true;
sRetMsg = "Version mismatch, recompile this module.";
} else {
- Info = ZNCModInfo();
+ ZNCModInfo(Info);
sRetMsg = "";
bVersionMismatch = false;
}
View
@@ -57,14 +57,12 @@ template<class M> CGlobalModule* TModLoadGlobal(ModHandle p,
extern "C" { \
double ZNCModVersion(); \
double ZNCModVersion() { return VERSION; } \
- CModInfo* ZNCModInfo(); \
- CModInfo* ZNCModInfo() { \
- CModInfo* Info = new CModInfo(); \
- Info->SetDescription(DESCRIPTION); \
- Info->SetGlobal(GLOBAL); \
+ void ZNCModInfo(CModInfo& Info); \
+ void ZNCModInfo(CModInfo& Info) { \
+ Info.SetDescription(DESCRIPTION); \
+ Info.SetGlobal(GLOBAL); \
LOADER; \
- TModInfo<CLASS>(*Info); \
- return Info; \
+ TModInfo<CLASS>(Info); \
} \
}
@@ -96,7 +94,7 @@ template<class M> CGlobalModule* TModLoadGlobal(ModHandle p,
* @see For global modules you need GLOBALMODULEDEFS.
*/
#define MODULEDEFS(CLASS, DESCRIPTION) \
- MODCOMMONDEFS(CLASS, DESCRIPTION, false, Info->SetLoader(TModLoad<CLASS>))
+ MODCOMMONDEFS(CLASS, DESCRIPTION, false, Info.SetLoader(TModLoad<CLASS>))
// !User Module Macros
// Global Module Macros
@@ -107,7 +105,7 @@ template<class M> CGlobalModule* TModLoadGlobal(ModHandle p,
/** This works exactly like MODULEDEFS, but for global modules. */
#define GLOBALMODULEDEFS(CLASS, DESCRIPTION) \
- MODCOMMONDEFS(CLASS, DESCRIPTION, true, Info->SetGlobalLoader(TModLoadGlobal<CLASS>))
+ MODCOMMONDEFS(CLASS, DESCRIPTION, true, Info.SetGlobalLoader(TModLoadGlobal<CLASS>))
// !Global Module Macros
// Forward Declarations
@@ -982,7 +980,7 @@ class CModules : public vector<CModule*> {
private:
static ModHandle OpenModule(const CString& sModule, const CString& sModPath,
- bool &bVersionMismatch, CModInfo*& Info, CString& sRetMsg);
+ bool &bVersionMismatch, CModInfo& Info, CString& sRetMsg);
protected:
CUser* m_pUser;

0 comments on commit ca97dca

Please sign in to comment.