Add a test for memory leaks when creating a lot of queries
Fix the memory leak when creating queries
Add an explanatory comment