History

Make Application destructor work when terminating early

Dale Glass committed 3 years ago

Commit a4e79ff1 parent c7480641 file · +46 −27Browse files

Showing the changes to interface/src/Application.cpp (1 of 1 file). Show all

Unified · Split

interface/src/Application.cpp+46−27
@@ -2897,43 +2897,59 @@ void Application::cleanupBeforeQuit() { · 2896 unmodified lines
28972897
28982898 Application::~Application() {
28992899 // remove avatars from physics engine
2900− auto avatarManager = DependencyManager::get<AvatarManager>();
2901− avatarManager->clearOtherAvatars();
2902− auto myCharacterController = getMyAvatar()->getCharacterController();
2903− myCharacterController->clearDetailedMotionStates();
2900+ if (auto avatarManager = DependencyManager::get<AvatarManager>()) {
2901+ // AvatarManager may not yet exist in case of an early exit
2902+
2903+ avatarManager->clearOtherAvatars();
2904+ auto myCharacterController = getMyAvatar()->getCharacterController();
2905+ myCharacterController->clearDetailedMotionStates();
29042906
2905− PhysicsEngine::Transaction transaction;
2906− avatarManager->buildPhysicsTransaction(transaction);
2907− _physicsEngine->processTransaction(transaction);
2908− avatarManager->handleProcessedPhysicsTransaction(transaction);
2909− avatarManager->deleteAllAvatars();
2907+ PhysicsEngine::Transaction transaction;
2908+ avatarManager->buildPhysicsTransaction(transaction);
2909+ _physicsEngine->processTransaction(transaction);
2910+ avatarManager->handleProcessedPhysicsTransaction(transaction);
2911+ avatarManager->deleteAllAvatars();
2912+ }
29102913
2911− _physicsEngine->setCharacterController(nullptr);
2914+ if (_physicsEngine) {
2915+ _physicsEngine->setCharacterController(nullptr);
2916+ }
29122917
29132918 // the _shapeManager should have zero references
29142919 _shapeManager.collectGarbage();
29152920 assert(_shapeManager.getNumShapes() == 0);
29162921
2917− // shutdown graphics engine
2918− _graphicsEngine->shutdown();
2922+ if (_graphicsEngine) {
2923+ // shutdown graphics engine
2924+ _graphicsEngine->shutdown();
2925+ }
29192926
29202927 _gameWorkload.shutdown();
29212928
29222929 DependencyManager::destroy<Preferences>();
29232930 PlatformHelper::shutdown();
29242931
2925− _entityClipboard->eraseAllOctreeElements();
2926− _entityClipboard.reset();
2932+ if (_entityClipboard) {
2933+ _entityClipboard->eraseAllOctreeElements();
2934+ _entityClipboard.reset();
2935+ }
29272936
2928− _octreeProcessor->terminate();
2929− _entityEditSender->terminate();
2937+ if (_octreeProcessor) {
2938+ _octreeProcessor->terminate();
2939+ }
29302940
2931− if (auto steamClient = PluginManager::getInstance()->getSteamClientPlugin()) {
2932− steamClient->shutdown();
2941+ if (_entityEditSender) {
2942+ _entityEditSender->terminate();
29332943 }
29342944
2935− if (auto oculusPlatform = PluginManager::getInstance()->getOculusPlatformPlugin()) {
2936− oculusPlatform->shutdown();
2945+ if (auto pluginManager = PluginManager::getInstance()) {
2946+ if (auto steamClient = pluginManager->getSteamClientPlugin()) {
2947+ steamClient->shutdown();
2948+ }
2949+
2950+ if (auto oculusPlatform = pluginManager->getOculusPlatformPlugin()) {
2951+ oculusPlatform->shutdown();
2952+ }
29372953 }
29382954
29392955 DependencyManager::destroy<PluginManager>();
@@ -2961,7 +2977,9 @@ Application::~Application() { · 21 unmodified lines
29612977 DependencyManager::destroy<GeometryCache>();
29622978 DependencyManager::destroy<ScreenshareScriptingInterface>();
29632979
2964− DependencyManager::get<ResourceManager>()->cleanup();
2980+ if (auto resourceManager = DependencyManager::get<ResourceManager>()) {
2981+ resourceManager->cleanup();
2982+ }
29652983
29662984 // remove the NodeList from the DependencyManager
29672985 DependencyManager::destroy<NodeList>();
@@ -2975,13 +2993,14 @@ Application::~Application() { · 7 unmodified lines
29752993 _window->deleteLater();
29762994
29772995 // make sure that the quit event has finished sending before we take the application down
2978− auto closeEventSender = DependencyManager::get<CloseEventSender>();
2979− while (!closeEventSender->hasFinishedQuitEvent() && !closeEventSender->hasTimedOutQuitEvent()) {
2980− // sleep a little so we're not spinning at 100%
2981− std::this_thread::sleep_for(std::chrono::milliseconds(10));
2996+ if (auto closeEventSender = DependencyManager::get<CloseEventSender>()) {
2997+ while (!closeEventSender->hasFinishedQuitEvent() && !closeEventSender->hasTimedOutQuitEvent()) {
2998+ // sleep a little so we're not spinning at 100%
2999+ std::this_thread::sleep_for(std::chrono::milliseconds(10));
3000+ }
3001+ // quit the thread used by the closure event sender
3002+ closeEventSender->thread()->quit();
29823003 }
2983− // quit the thread used by the closure event sender
2984− closeEventSender->thread()->quit();
29853004
29863005 // Can't log to file past this point, FileLogger about to be deleted
29873006 qInstallMessageHandler(LogHandler::verboseMessageHandler);