Skip to content

Commit 060ef36

Browse files
committed
Windows: Fix crash when double-clicking a .MOD while a PT2 clone instance is already running
Also do less unnecessary stuff on initialization before closing newly spawned instance.
1 parent 96b4a25 commit 060ef36

File tree

3 files changed

+41
-23
lines changed

3 files changed

+41
-23
lines changed

src/pt2_header.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#include "pt2_unicode.h"
1515
#include "pt2_palette.h"
1616

17-
#define PROG_VER_STR "1.44"
17+
#define PROG_VER_STR "1.45"
1818

1919
#ifdef _WIN32
2020
#define DIR_DELIMITER '\\'

src/pt2_main.c

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ static LPCTSTR sharedMemBuf;
6161
static TCHAR sharedHwndName[] = TEXT("Local\\PT2CloneHwnd");
6262
static TCHAR sharedFileName[] = TEXT("Local\\PT2CloneFilename");
6363
static bool handleSingleInstancing(int32_t argc, char **argv);
64+
static void closeSingleInstancing(void);
6465
static void handleSysMsg(SDL_Event inputEvent);
6566
#endif
6667

@@ -233,10 +234,7 @@ int main(int argc, char *argv[])
233234
makeSureDirIsProgramDir();
234235
#endif
235236

236-
hpc_Init();
237-
hpc_SetDurationInHz(&video.vblankHpc, VBLANK_HZ);
238-
239-
if (!initializeVars() || !initKaiserTable())
237+
if (!initializeVars())
240238
{
241239
cleanUp();
242240
SDL_Quit();
@@ -264,7 +262,10 @@ int main(int argc, char *argv[])
264262
SDL_EventState(SDL_SYSWMEVENT, SDL_ENABLE);
265263
#endif
266264

267-
if (!setupAudio() || !unpackBMPs())
265+
hpc_Init();
266+
hpc_SetDurationInHz(&video.vblankHpc, VBLANK_HZ);
267+
268+
if (!initKaiserTable() || !setupAudio() || !unpackBMPs())
268269
{
269270
cleanUp();
270271
SDL_Quit();
@@ -772,6 +773,7 @@ static bool handleSingleInstancing(int32_t argc, char **argv)
772773
if (sharedMemBuf != NULL)
773774
{
774775
memcpy(&hWnd_to, sharedMemBuf, sizeof (HWND));
776+
775777
UnmapViewOfFile(sharedMemBuf);
776778
sharedMemBuf = NULL;
777779
CloseHandle(hMapFile);
@@ -784,11 +786,12 @@ static bool handleSingleInstancing(int32_t argc, char **argv)
784786
if (sharedMemBuf != NULL)
785787
{
786788
strcpy((char *)sharedMemBuf, argv[1]);
789+
787790
UnmapViewOfFile(sharedMemBuf);
788791
sharedMemBuf = NULL;
789792

790793
SendMessage(hWnd_to, SYSMSG_FILE_ARG, 0, 0);
791-
SDL_Delay(80); // wait a bit to make sure first instance received msg
794+
Sleep(80); // wait a bit to make sure first instance received msg
792795

793796
CloseHandle(hMapFile);
794797
hMapFile = NULL;
@@ -809,31 +812,39 @@ static bool handleSingleInstancing(int32_t argc, char **argv)
809812

810813
static void handleSysMsg(SDL_Event inputEvent)
811814
{
812-
SDL_SysWMmsg *wmMsg;
815+
if (inputEvent.type != SDL_SYSWMEVENT)
816+
return;
813817

814-
if (inputEvent.type == SDL_SYSWMEVENT)
818+
SDL_SysWMmsg *wmMsg = inputEvent.syswm.msg;
819+
if (wmMsg->subsystem == SDL_SYSWM_WINDOWS && wmMsg->msg.win.msg == SYSMSG_FILE_ARG)
815820
{
816-
wmMsg = inputEvent.syswm.msg;
817-
if (wmMsg->subsystem == SDL_SYSWM_WINDOWS && wmMsg->msg.win.msg == SYSMSG_FILE_ARG)
821+
hMapFile = OpenFileMapping(FILE_MAP_READ, FALSE, sharedFileName);
822+
if (hMapFile != NULL)
818823
{
819-
hMapFile = OpenFileMapping(FILE_MAP_READ, FALSE, sharedFileName);
820-
if (hMapFile != NULL)
824+
sharedMemBuf = (LPTSTR)MapViewOfFile(hMapFile, FILE_MAP_READ, 0, 0, ARGV_SHARED_MEM_MAX_LEN);
825+
if (sharedMemBuf != NULL)
821826
{
822-
sharedMemBuf = (LPTSTR)MapViewOfFile(hMapFile, FILE_MAP_READ, 0, 0, ARGV_SHARED_MEM_MAX_LEN);
823-
if (sharedMemBuf != NULL)
824-
{
825-
loadDroppedFile((char *)sharedMemBuf, (uint32_t)strlen(sharedMemBuf), true, true);
826-
UnmapViewOfFile(sharedMemBuf);
827-
sharedMemBuf = NULL;
828-
}
827+
loadDroppedFile((char *)sharedMemBuf, (uint32_t)strlen(sharedMemBuf), true, true);
829828

830-
CloseHandle(hMapFile);
831-
hMapFile = NULL;
829+
UnmapViewOfFile(sharedMemBuf);
830+
sharedMemBuf = NULL;
832831
}
832+
833+
CloseHandle(hMapFile);
834+
hMapFile = NULL;
833835
}
834836
}
835837
}
836838

839+
void closeSingleInstancing(void)
840+
{
841+
if (oneInstHandle != NULL)
842+
{
843+
CloseHandle(oneInstHandle);
844+
oneInstHandle = NULL;
845+
}
846+
}
847+
837848
static LONG WINAPI exceptionHandler(EXCEPTION_POINTERS *ptr)
838849
#else
839850
static void exceptionHandler(int32_t signal)
@@ -898,6 +909,10 @@ static void cleanUp(void) // never call this inside the main loop!
898909
#ifndef _DEBUG
899910
UnhookWindowsHookEx(g_hKeyboardHook);
900911
#endif
901-
if (oneInstHandle != NULL) CloseHandle(oneInstHandle);
912+
if (oneInstHandle != NULL)
913+
{
914+
CloseHandle(oneInstHandle);
915+
oneInstHandle = NULL;
916+
}
902917
#endif
903918
}

src/pt2_replayer.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ static const uint8_t funkTable[16] = // EFx (FunkRepeat/InvertLoop)
3737

3838
void setReplayerPosToTrackerPos(void)
3939
{
40+
if (song == NULL)
41+
return;
42+
4043
modPattern = (int8_t)song->currPattern;
4144
modOrder = song->currOrder;
4245
song->row = song->currRow;

0 commit comments

Comments
 (0)