don't leak memory

But the last one isn't needed since we free it in each loop iteration, and moreover, there's no NULL assignment after that, so double-free might occur. I've pushed one more commit to avoid it.