Fix use-after-free in submodule reload code and other memory leaks

The always wonderful @ethomson found a use-after-free in the newly updates submodule reloading code. I'm not sure why `valgrind` doesn't notice this, but it seems like a serious problem.

In testing this update in valgrind, I found two other memory leaks that were mystifyingly overlooked by the previous update, too.