Fixed unloading of plugins with shared pointers

Cherry-picked from dev