Fix memory leaks in QuerySet