Fixing crashing on quit due to circular references with shared_ptr
objects which drop us into an infinite loop or the shared_ptr tries to
delete an invalid object. Both will cause a crash on OS X.