remove useless null checks before free
remove yet more useless null checks before free, and reorder a free to prevent undefined behavior
remove useless null checks before free
more const correctness