Issue 13414 - Static destructors not called correctly with dynamic loading

Patch provided by Niklas Wallén in private correspondence.
Merge pull request #947 from klickverbot/run-tls-dtors

Issue 13414 - Static destructors not called correctly with dynamic loading