mirror of
https://github.com/anope/anope.git
synced 2026-07-05 21:13:13 +02:00
Do not use new/delete to allocate modules, allows modules to always destruct properly and automatically
This commit is contained in:
+6
-19
@@ -140,39 +140,26 @@ extern "C" void __pfnBkCheck() {}
|
||||
|
||||
/** This definition is used as shorthand for the various classes
|
||||
* and functions needed to make a module loadable by the OS.
|
||||
* It defines the class factory and external AnopeInit and AnopeFini functions.
|
||||
* It defines the class factory and the external AnopeInit function.
|
||||
*/
|
||||
#ifdef _WIN32
|
||||
# define MODULE_INIT(x) \
|
||||
extern "C" DllExport Module *AnopeInit(const Anope::string &, const Anope::string &); \
|
||||
extern "C" Module *AnopeInit(const Anope::string &modname, const Anope::string &creator) \
|
||||
{ \
|
||||
return new x(modname, creator); \
|
||||
static x module(modname, creator); \
|
||||
return &module; \
|
||||
} \
|
||||
BOOLEAN WINAPI DllMain(HINSTANCE, DWORD nReason, LPVOID) \
|
||||
BOOLEAN WINAPI DllMain(HINSTANCE, DWORD, LPVOID) \
|
||||
{ \
|
||||
switch (nReason) \
|
||||
{ \
|
||||
case DLL_PROCESS_ATTACH: \
|
||||
case DLL_PROCESS_DETACH: \
|
||||
break; \
|
||||
} \
|
||||
return TRUE; \
|
||||
} \
|
||||
extern "C" DllExport void AnopeFini(x *); \
|
||||
extern "C" void AnopeFini(x *m) \
|
||||
{ \
|
||||
delete m; \
|
||||
}
|
||||
#else
|
||||
# define MODULE_INIT(x) \
|
||||
extern "C" DllExport Module *AnopeInit(const Anope::string &modname, const Anope::string &creator) \
|
||||
{ \
|
||||
return new x(modname, creator); \
|
||||
} \
|
||||
extern "C" DllExport void AnopeFini(x *m) \
|
||||
{ \
|
||||
delete m; \
|
||||
static x module(modname, creator); \
|
||||
return &module; \
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
+2
-16
@@ -256,22 +256,8 @@ void ModuleManager::DeleteModule(Module *m)
|
||||
ano_module_t handle = m->handle;
|
||||
Anope::string filename = m->filename;
|
||||
|
||||
ano_modclearerr();
|
||||
void (*destroy_func)(Module *m) = function_cast<void (*)(Module *)>(dlsym(m->handle, "AnopeFini"));
|
||||
const char *err = ano_moderr();
|
||||
if (!destroy_func && err && *err)
|
||||
{
|
||||
Log() << "No destroy function found, chancing delete...";
|
||||
delete m; /* we just have to chance they haven't overwrote the delete operator then... */
|
||||
}
|
||||
else
|
||||
destroy_func(m); /* Let the module delete it self, just in case */
|
||||
|
||||
if (handle)
|
||||
{
|
||||
if (dlclose(handle))
|
||||
Log() << ano_moderr();
|
||||
}
|
||||
if (handle && dlclose(handle))
|
||||
Log() << ano_moderr();
|
||||
|
||||
if (!filename.empty())
|
||||
DeleteFile(filename.c_str());
|
||||
|
||||
Reference in New Issue
Block a user