Deadlock fixed and missed memory leaks when dll loading. Closes #10555

Reverted commit 8d566b611b from version 2.2.2