From e99bf73af31a8b1f09b9ce033c16832ee5cac51d Mon Sep 17 00:00:00 2001
From: Haru <haru@dotalux.com>
Date: Tue, 15 Sep 2015 12:12:01 +0200
Subject: Corrected a plugin unload issue

- This doesn't affect the current unloading function (executed at
  shutdown), but it is only related to the (upcoming) manual plugin
  unloading.

Signed-off-by: Haru <haru@dotalux.com>
---
 src/common/HPM.c | 27 ++++++++++++++-------------
 1 file changed, 14 insertions(+), 13 deletions(-)

diff --git a/src/common/HPM.c b/src/common/HPM.c
index 2277b9c71..23265fa24 100644
--- a/src/common/HPM.c
+++ b/src/common/HPM.c
@@ -477,31 +477,32 @@ struct hplugin *hplugin_load(const char* filename) {
 	return plugin;
 }
 
-void hplugin_unload(struct hplugin* plugin) {
-	unsigned int i = plugin->idx;
-
-	if( plugin->filename )
+void hplugin_unload(struct hplugin* plugin)
+{
+	if (plugin->filename)
 		aFree(plugin->filename);
-	if( plugin->dll )
+	if (plugin->dll)
 		plugin_close(plugin->dll);
 	/* TODO: for manual packet unload */
 	/* - Go through known packets and unlink any belonging to the plugin being removed */
-	aFree(plugin);
 	if (!HPM->off) {
-		int cursor = 0;
-		HPM->plugins[i] = NULL;
-		for(i = 0; i < HPM->plugin_count; i++) {
-			if( HPM->plugins[i] == NULL )
+		int i, cursor;
+		for (cursor = 0; cursor < HPM->plugin_count; cursor++) {
+			if (HPM->plugins[cursor]->idx != plugin->idx)
 				continue;
-			if( cursor != i )
-				HPM->plugins[cursor] = HPM->plugins[i];
+			HPM->plugins[cursor] = NULL;
+			break;
+		}
+		for(i = cursor + 1; i < HPM->plugin_count; i++) {
+			HPM->plugins[cursor] = HPM->plugins[i];
 			cursor++;
 		}
-		if( !(HPM->plugin_count = cursor) ) {
+		if (!(HPM->plugin_count = cursor)) {
 			aFree(HPM->plugins);
 			HPM->plugins = NULL;
 		}
 	}
+	aFree(plugin);
 }
 
 /**
-- 
cgit v1.2.3-70-g09d2