Clean up global datastructures at shutdown.

This should make detecting leaks much easier.
Merge pull request #2420 from sipa/globclean

Global cleanups