Fixed memory leak caused by nit freeing query outputs

Added free_query_output function