Fix a possible memory leak from _ctx_history_query()