Plug memory leaks.

Most of the changes are mechanic std::unique_ptr insertions. All leaks were
detected by LeakSanitizer.