Fix for OpenGL context leak

Supersedes #640 with a non-intrusive mechanism to automatically free thread-local contexts after thread termination.