fix memory leak

Submitted by: Nils Larsch
diff --git a/crypto/engine/enginetest.c b/crypto/engine/enginetest.c
index 598cb31..87fa8c5 100644
--- a/crypto/engine/enginetest.c
+++ b/crypto/engine/enginetest.c
@@ -79,6 +79,9 @@
 		h = ENGINE_get_next(h);
 		}
 	printf("end of list\n");
+	/* ENGINE_get_first() increases the struct_ref counter, so we 
+           must call ENGINE_free() to decrease it again */
+	ENGINE_free(h);
 	}
 
 int main(int argc, char *argv[])
@@ -94,6 +97,18 @@
 	ENGINE *new_h3 = NULL;
 	ENGINE *new_h4 = NULL;
 
+	/* enable memory leak checking unless explicitly disabled */
+	if (!((getenv("OPENSSL_DEBUG_MEMORY") != NULL) && (0 == strcmp(getenv("OPENSSL_DEBUG_MEMORY"), "off"))))
+		{
+		CRYPTO_malloc_debug_init();
+		CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
+		}
+	else
+		{
+		/* OPENSSL_DEBUG_MEMORY=off */
+		CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0);
+		}
+	CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
 	ERR_load_crypto_strings();
 
 	memset(block, 0, 512 * sizeof(ENGINE *));
@@ -127,6 +142,8 @@
 		printf("Remove failed!\n");
 		goto end;
 		}
+	if (ptr)
+		ENGINE_free(ptr);
 	display_engine_list();
 	if(!ENGINE_add(new_h3) || !ENGINE_add(new_h2))
 		{
@@ -181,6 +198,8 @@
 		if(!ENGINE_remove(ptr))
 			printf("Remove failed!i - probably no hardware "
 				"support present.\n");
+	if (ptr)
+		ENGINE_free(ptr);
 	display_engine_list();
 	if(!ENGINE_add(new_h1) || !ENGINE_remove(new_h1))
 		{
@@ -226,6 +245,7 @@
 			printf("\nRemove failed!\n");
 			goto end;
 			}
+		ENGINE_free(ptr);
 		printf("."); fflush(stdout);
 		}
 	for(loop = 0; loop < 512; loop++)
@@ -245,5 +265,10 @@
 	for(loop = 0; loop < 512; loop++)
 		if(block[loop])
 			ENGINE_free(block[loop]);
+	ENGINE_cleanup();
+	CRYPTO_cleanup_all_ex_data();
+	ERR_free_strings();
+	ERR_remove_state(0);
+	CRYPTO_mem_leaks_fp(stderr);
 	return to_return;
 	}