Clean up global datastructures at shutdown.

This should make detecting leaks much easier.