Skip to content

Commit

Permalink
fix: proper file stream closing
Browse files Browse the repository at this point in the history
  • Loading branch information
Bizzonium committed Jul 13, 2024
1 parent 2f812f8 commit 423d832
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion prof.c
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,13 @@ static int utracy_write(void const *const buf, size_t size) {
}
#else
static int utracy_write(void const *const buf, size_t size) {
fwrite(buf, 1, size, utracy.fstream);
if(size != fwrite(buf, 1, size, utracy.fstream)) {
perror("fwrite");
fflush(utracy.fstream);
fsync(fileno(utracy.fstream));
fclose(utracy.fstream);
abort();
}
return 0;
}
#endif
Expand Down Expand Up @@ -1486,6 +1492,8 @@ char *UTRACY_WINDOWS_CDECL UTRACY_LINUX_CDECL init(int argc, char **argv) {
#if defined(UTRACY_WINDOWS)
if(NULL == (utracy.thread = CreateThread(NULL, 0, utracy_server_thread_start, NULL, 0, NULL))) {
LOG_DEBUG_ERROR;
fflush(utracy.fstream);
fsync(fileno(utracy.fstream));
fclose(utracy.fstream);
return "CreateThread failed";
}
Expand All @@ -1494,6 +1502,9 @@ char *UTRACY_WINDOWS_CDECL UTRACY_LINUX_CDECL init(int argc, char **argv) {
utracy.quit = 0;
if (0 != pthread_create(&utracy.thread, NULL, utracy_server_thread_start, NULL)) {
LOG_DEBUG_ERROR;
fflush(utracy.fstream);
fsync(fileno(utracy.fstream));
fclose(utracy.fstream);
return "pthread_create failed";
}

Expand Down Expand Up @@ -1528,7 +1539,10 @@ char *UTRACY_WINDOWS_CDECL UTRACY_LINUX_CDECL destroy(int argc, char **argv) {
pthread_join(utracy.thread, &thread_return);
#endif

fflush(utracy.fstream);
fsync(fileno(utracy.fstream));
fclose(utracy.fstream);

initialized = 0;

return "0";
Expand Down

0 comments on commit 423d832

Please sign in to comment.