Skip to content

Commit 8f96150

Browse files
committed
Fix random crashes when exiting app
1 parent e22cee3 commit 8f96150

File tree

5 files changed

+30
-17
lines changed

5 files changed

+30
-17
lines changed

Goldleaf/include/base.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ struct Version {
306306
};
307307

308308
Result Initialize();
309-
void Exit();
309+
void Finalize();
310310

311311
inline ExecutableMode GetExecutableMode() {
312312
return envIsNso() ? ExecutableMode::NSO : ExecutableMode::NRO;

Goldleaf/source/base.cpp

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -264,9 +264,11 @@ Result Initialize() {
264264
return 0;
265265
}
266266

267-
void Exit() {
267+
void Finalize() {
268268
if(g_MainApplication) {
269+
GLEAF_LOG_FMT("Closing application...");
269270
g_MainApplication->Close();
271+
g_MainApplication = nullptr;
270272
}
271273

272274
romfsExit();
@@ -278,30 +280,29 @@ void Exit() {
278280
sd_exp->RenameFile(GLEAF_PATH_TEMP_UPDATE_NRO, cur_nro_file);
279281
}
280282

281-
{
282-
ScopedLock lk(g_LogLock);
283-
for(auto &it : g_ThreadLogBufferList) {
284-
delete[] it.second;
285-
}
286-
}
287-
288283
cnt::FinalizeTickets();
289284
cnt::FinalizeApplications();
290-
fs::Finalize();
291285
drive::Finalize();
292286
amssuExit();
287+
pdmqryExit();
288+
nifmExit();
293289
usb::Finalize();
294290
setsysExit();
295291
setExit();
296292
psmExit();
297293
esExit();
298294
nsExit();
299-
accountExit();
300295
ncmExit();
301-
nifmExit();
302-
pdmqryExit();
296+
accountExit();
303297

304-
exit(0);
298+
fs::Finalize();
299+
300+
{
301+
ScopedLock lk(g_LogLock);
302+
for(auto &it : g_ThreadLogBufferList) {
303+
delete[] it.second;
304+
}
305+
}
305306
}
306307

307308
char *GetLogBuffer() {

Goldleaf/source/cnt/cnt_Content.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ namespace cnt {
135135
}
136136

137137
Thread g_LoadApplicationsThread;
138+
std::atomic_bool g_LoadApplicationsThreadShouldExit = false;
138139
std::atomic_bool g_LoadApplicationsThreadDone = true;
139140

140141
void ScanApplications() {
@@ -146,6 +147,11 @@ namespace cnt {
146147

147148
// Try to get application views and control data
148149
for(auto &app: g_Applications) {
150+
if(g_LoadApplicationsThreadShouldExit) {
151+
g_LoadApplicationsThreadShouldExit = false;
152+
return;
153+
}
154+
149155
if(R_SUCCEEDED(nsGetApplicationView(reinterpret_cast<NsApplicationView*>(&app.view), &app.record.id, 1))) {
150156
for(u32 i = 0; i < sizeof(app.view.flags) * CHAR_BIT; i++) {
151157
if(app.view.flags & BIT(i)) {
@@ -314,6 +320,8 @@ namespace cnt {
314320
}
315321

316322
void FinalizeApplications() {
323+
g_LoadApplicationsThreadShouldExit = true;
324+
threadWaitForExit(&g_LoadApplicationsThread);
317325
threadClose(&g_LoadApplicationsThread);
318326
}
319327

Goldleaf/source/cnt/cnt_Ticket.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ namespace cnt {
137137
}
138138

139139
void FinalizeTickets() {
140+
threadWaitForExit(&g_LoadTicketsThread);
140141
threadClose(&g_LoadTicketsThread);
141142
}
142143

Goldleaf/source/main.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121

2222
#include <ui/ui_MainApplication.hpp>
2323

24-
ui::MainApplication::Ref g_MainApplication;
25-
cfg::Settings g_Settings;
24+
ui::MainApplication::Ref g_MainApplication = nullptr;
25+
cfg::Settings g_Settings = {};
2626
bool g_UpdatedNeedsRename = false;
2727

2828
int main() {
@@ -61,5 +61,8 @@ int main() {
6161
// Start and loop the application
6262
g_MainApplication->ShowWithFadeIn();
6363

64-
Exit();
64+
GLEAF_LOG_FMT("Exiting Goldleaf...");
65+
66+
Finalize();
67+
return 0;
6568
}

0 commit comments

Comments
 (0)