Add query leak tests
Fix memleak in json set