automatically clean up plugin commands

This commit is contained in:
Tilka 2012-03-20 22:29:15 +01:00
parent 5b6ab7e1d2
commit e408ae3eba
3 changed files with 31 additions and 24 deletions

View File

@ -54,26 +54,6 @@ static int plugin_command_dispatch(struct command_base* cbase, struct hub_user*
return 0;
}
struct plugin_callback_data* plugin_callback_data_create()
{
struct plugin_callback_data* data = (struct plugin_callback_data*) hub_malloc_zero(sizeof(struct plugin_callback_data));
LOG_PLUGIN("plugin_callback_data_create()");
data->commands = list_create();
return data;
}
void plugin_callback_data_destroy(struct plugin_callback_data* data)
{
LOG_PLUGIN("plugin_callback_data_destroy()");
if (data->commands)
{
uhub_assert(list_size(data->commands) == 0);
list_destroy(data->commands);
}
hub_free(data);
}
static struct hub_user* convert_user_type(struct plugin_user* user)
{
struct hub_user* huser = (struct hub_user*) user;
@ -173,3 +153,30 @@ void plugin_register_callback_functions(struct plugin_handle* handle)
void plugin_unregister_callback_functions(struct plugin_handle* handle)
{
}
struct plugin_callback_data* plugin_callback_data_create()
{
struct plugin_callback_data* data = (struct plugin_callback_data*) hub_malloc_zero(sizeof(struct plugin_callback_data));
LOG_PLUGIN("plugin_callback_data_create()");
data->commands = list_create();
return data;
}
void plugin_callback_data_destroy(struct plugin_handle* plugin, struct plugin_callback_data* data)
{
LOG_PLUGIN("plugin_callback_data_destroy()");
if (data->commands)
{
// delete commands not deleted by the plugin itself:
struct plugin_command_handle* cmd = list_get_first(data->commands);
while (cmd)
{
cbfunc_command_del(plugin, cmd);
list_remove(data->commands, cmd);
cmd = list_get_next(data->commands);
}
list_destroy(data->commands);
}
hub_free(data);
}

View File

@ -24,7 +24,7 @@ struct plugin_handle;
struct uhub_plugin;
extern struct plugin_callback_data* plugin_callback_data_create();
extern void plugin_callback_data_destroy(struct plugin_callback_data* data);
extern void plugin_callback_data_destroy(struct plugin_handle* plugin, struct plugin_callback_data* data);
extern void plugin_register_callback_functions(struct plugin_handle* plugin);
extern void plugin_unregister_callback_functions(struct plugin_handle* plugin);

View File

@ -76,7 +76,7 @@ void plugin_close(struct uhub_plugin* plugin)
struct plugin_hub_internals* internals = (struct plugin_hub_internals*) plugin->internals;
LOG_PLUGIN("plugin_close: \"%s\"", plugin->filename);
plugin_callback_data_destroy(internals->callback_data);
plugin_callback_data_destroy(plugin->handle, internals->callback_data);
hub_free(internals);
plugin->internals = NULL;
@ -162,8 +162,8 @@ struct plugin_handle* plugin_load(const char* filename, const char* config, stru
void plugin_unload(struct plugin_handle* plugin)
{
struct plugin_hub_internals* internals = get_internals(plugin);
plugin_unregister_callback_functions(plugin);
internals->unregister(plugin);
plugin_unregister_callback_functions(plugin);
plugin_close(plugin->handle);
hub_free(plugin);
}