@@ -167,7 +167,7 @@ namespace winrt::CelestiaWinUI::implementation
167167 renderer = CelestiaRenderer (appSettings.EnableMSAA (), [weak_this{ get_weak () }, resourcePath, configPath, locale, layoutDirection, defaultSettings](int32_t )
168168 {
169169 auto strong_this{ weak_this.get () };
170- return strong_this == nullptr ? false : strong_this->StartEngine (resourcePath, configPath, locale, layoutDirection, defaultSettings);
170+ return ( strong_this == nullptr || strong_this-> isClosed ) ? false : strong_this->StartEngine (resourcePath, configPath, locale, layoutDirection, defaultSettings);
171171 });
172172 renderer.SetCorePointer (appCore.Pointer ());
173173 renderer.SetSurface (GLView (), scale);
@@ -186,13 +186,14 @@ namespace winrt::CelestiaWinUI::implementation
186186 hstring localeDirectory = PathHelper::Combine (resourcePath, L" locale" );
187187 CelestiaAppCore::SetLocaleDirectory (localeDirectory, locale);
188188
189+ auto ref = get_strong (); // Keep it alive
189190 bool loadSuccess = appCore.StartSimulation (configPath, extraPaths, [weak_this{ get_weak () }](hstring const status) {
190191 auto strong_this{ weak_this.get () };
191- if (strong_this == nullptr ) return ;
192+ if (strong_this == nullptr || strong_this-> isClosed ) return ;
192193 strong_this->DispatcherQueue ().TryEnqueue (Microsoft::UI::Dispatching::DispatcherQueuePriority::Normal, [weak_this{ strong_this->get_weak () }, status]()
193194 {
194195 auto strong_this{ weak_this.get () };
195- if (strong_this == nullptr ) return ;
196+ if (strong_this == nullptr || strong_this-> isClosed ) return ;
196197 auto textToDisplay = to_hstring (fmt::sprintf (to_string (LocalizationHelper::Localize (L" Loading: %s" , L" Celestia initialization, loading file" )), to_string (status)));
197198 strong_this->LoadingText ().Text (textToDisplay);
198199 });
@@ -201,6 +202,9 @@ namespace winrt::CelestiaWinUI::implementation
201202 {
202203 if (resourcePath != defaultResourcePath || configPath != defaultConfigFilePath)
203204 {
205+ if (isClosed)
206+ return false ;
207+
204208 // Try to restore originial settings
205209 DispatcherQueue ().TryEnqueue (Microsoft::UI::Dispatching::DispatcherQueuePriority::Normal, [weak_this{ get_weak () }]()
206210 {
@@ -212,11 +216,11 @@ namespace winrt::CelestiaWinUI::implementation
212216 CelestiaAppCore::SetLocaleDirectory (PathHelper::Combine (defaultResourcePath, L" locale" ), locale);
213217 if (!appCore.StartSimulation (defaultConfigFilePath, extraPaths, [weak_this{ get_weak () }](hstring const status) {
214218 auto strong_this{ weak_this.get () };
215- if (strong_this == nullptr ) return ;
219+ if (strong_this == nullptr || strong_this-> isClosed ) return ;
216220 strong_this->DispatcherQueue ().TryEnqueue (Microsoft::UI::Dispatching::DispatcherQueuePriority::Normal, [weak_this{ strong_this->get_weak () }, status]()
217221 {
218222 auto strong_this{ weak_this.get () };
219- if (strong_this == nullptr ) return ;
223+ if (strong_this == nullptr || strong_this-> isClosed ) return ;
220224 auto textToDisplay = to_hstring (fmt::sprintf (to_string (LocalizationHelper::Localize (L" Loading: %s" , L" Celestia initialization, loading file" )), to_string (status)));
221225 strong_this->LoadingText ().Text (textToDisplay);
222226 });
@@ -293,10 +297,13 @@ namespace winrt::CelestiaWinUI::implementation
293297 appCore.SetRenderFont (PathHelper::Combine (pathPrefix, regularFont), regularFontIndex, 9 , CelestiaFontStyle::Normal);
294298 appCore.SetRenderFont (PathHelper::Combine (pathPrefix, boldFont), boldFontIndex, 15 , CelestiaFontStyle::Large);
295299
300+ if (isClosed)
301+ return false ;
302+
296303 DispatcherQueue ().TryEnqueue (Microsoft::UI::Dispatching::DispatcherQueuePriority::Normal, [weak_this{ get_weak () }, resourcePath]()
297304 {
298305 auto strong_this{ weak_this.get () };
299- if (strong_this == nullptr ) return ;
306+ if (strong_this == nullptr || strong_this-> isClosed ) return ;
300307 strong_this->UpdateScale (true );
301308 strong_this->LoadingText ().Visibility (Visibility::Collapsed);
302309 strong_this->resourceManager = ResourceManager (strong_this->extraAddonFolder , strong_this->extraScriptFolder );
@@ -311,10 +318,13 @@ namespace winrt::CelestiaWinUI::implementation
311318
312319 readyForInput = true ;
313320
321+ if (isClosed)
322+ return false ;
323+
314324 DispatcherQueue ().TryEnqueue (Microsoft::UI::Dispatching::DispatcherQueuePriority::Normal, [weak_this{ get_weak () }]()
315325 {
316326 auto strong_this{ weak_this.get () };
317- if (strong_this == nullptr ) return ;
327+ if (strong_this == nullptr || strong_this-> isClosed ) return ;
318328 strong_this->OpenFileOrURL ();
319329 });
320330 return true ;
0 commit comments