Change some instances of raw "new" to std::unique_ptr

And fix a shutdown race condition discovered using atomics and store/load.

I didn't modify the "new"s in MemTrie.* as that is possibly performance/space sensitive.