[12763] Fix a little memleak. Thank to @kid10. Also fix sql error introduced in 9b7bb67c0eeabfe3e8146e6762231d7869e47f33