diff --git a/aql2/src/prompt/ReadlinePromptHistory.cpp b/aql2/src/prompt/ReadlinePromptHistory.cpp
index e5e95b3fafe7eeeef066b517357e199b0c8a4a98..1ce2f0a87a3c8b8f68e242fcea7f1864e73b873f 100644
--- a/aql2/src/prompt/ReadlinePromptHistory.cpp
+++ b/aql2/src/prompt/ReadlinePromptHistory.cpp
@@ -61,7 +61,8 @@ void ReadlinePromptHistory::history_transform ( Callable callable ) {
 		int i = 0;
 		while ( * history ) {
 			char * tmp = callable ( ( * history )->line );
-			replace_history_entry ( i, tmp, ( * history )->data );
+			HIST_ENTRY * old = replace_history_entry ( i, tmp, ( * history )->data );
+			free_history_entry ( old );
 			free ( tmp );
 			++ history;
 			++ i;
@@ -76,6 +77,7 @@ ReadlinePromptHistory::ReadlinePromptHistory ( std::string history_file ) : m_hi
 ReadlinePromptHistory::~ ReadlinePromptHistory ( ) {
 	ReadlinePromptHistory::writeHistory ( m_history_file );
 	clear_history ( );
+	free ( history_list ( ) ); // Note: this may not be entiery safe, it depends on the implementation of readline (but 5.0 through 8.2-alpha) are safe.
 }
 
 void ReadlinePromptHistory::readHistory ( const std::string & history_file ) {