diff --git a/tools/thanos/thanos.cpp b/tools/thanos/thanos.cpp
index 7ca7befa09116c5d802963cccaa595ca3d99c51b..751ac5d1decd91af637379d0b4a015a2cac500fa 100644
--- a/tools/thanos/thanos.cpp
+++ b/tools/thanos/thanos.cpp
@@ -199,7 +199,9 @@ int main(int argc, char *argv[])
                                 close(saved_stderr);
 
 				// cleanup plugin
-				free(argv);
+				free(argv); 
+				argv=nullptr;
+				the_step.reset(); // explicitly get rid of the handle to the library so we can close it.
                                 dlclose(dlhdl);
 				
 				string step_retval_str(to_string(step_retval)+"\n");