From 4de42bf26e9486c885da30a37bfa3ce933a58732 Mon Sep 17 00:00:00 2001 From: Jeremy Rand Date: Tue, 22 Aug 2023 20:17:56 -0400 Subject: [PATCH] Attempting to fix the issue that Kelvin has raised with improper memory handling in NDAInit(). I am simplifying NDAInit() and deferring all memory allocation and cleanup to NDAOpen() and NDAClose(). --- .../xcschemes/xcschememanagement.plist | 6 +-- Listener/main.c | 46 ++++++++----------- Listener/main.rez | 2 +- 3 files changed, 24 insertions(+), 30 deletions(-) diff --git a/Listener.xcodeproj/xcuserdata/jrand.xcuserdatad/xcschemes/xcschememanagement.plist b/Listener.xcodeproj/xcuserdata/jrand.xcuserdatad/xcschemes/xcschememanagement.plist index 67ec8d8..798be21 100644 --- a/Listener.xcodeproj/xcuserdata/jrand.xcuserdatad/xcschemes/xcschememanagement.plist +++ b/Listener.xcodeproj/xcuserdata/jrand.xcuserdatad/xcschemes/xcschememanagement.plist @@ -7,17 +7,17 @@ Archive.xcscheme_^#shared#^_ orderHint - 3 + 4 Binary.xcscheme_^#shared#^_ orderHint - 4 + 2 DiskImage.xcscheme_^#shared#^_ orderHint - 2 + 3 Listener.xcscheme_^#shared#^_ diff --git a/Listener/main.c b/Listener/main.c index b412f9d..1f3aa39 100644 --- a/Listener/main.c +++ b/Listener/main.c @@ -143,7 +143,7 @@ static char * stateMessages[NUM_LISTEN_STATES] = { // Implementation -void delayToNextTick() +void delayToNextTick(void) { LongWord oldTickCounter = GetTick(); while (oldTickCounter == GetTick()) @@ -195,7 +195,7 @@ void teardownNetwork(void) } -void freeText(void) +void freeMemory(void) { tTextList * textList = globals->textListHead; @@ -207,30 +207,25 @@ void freeText(void) textList = textList->header.next; free(prev); } + + free(globals); + globals = NULL; } void NDAClose(void) { - if ((globals != NULL) && - (globals->ndaActive)) { + if (globals == NULL) + return; + + if (globals->ndaActive) { CloseWindow(globals->winPtr); globals->winPtr = NULL; globals->ndaActive = FALSE; + ResourceShutDown(); } teardownNetwork(); - ResourceShutDown(); - freeText(); - memset(globals, 0, sizeof(*globals)); -} - - -void freeGlobals(void) -{ - freeText(); - - free(globals); - globals = NULL; + freeMemory(); } @@ -241,15 +236,6 @@ void NDAInit(int code) */ if (code) { userId = MMStartUp(); - globals = malloc(sizeof(*globals)); - memset(globals, 0, sizeof(*globals)); - } else { - if ((globals != NULL) && - (globals->ndaActive)) { - NDAClose(); - teardownNetwork(); - freeGlobals(); - } } } @@ -297,6 +283,11 @@ GrafPortPtr NDAOpen(void) SysPrefsRecGS prefsDCB; unsigned int oldPrefs; + if (globals == NULL) { + globals = malloc(sizeof(*globals)); + memset(globals, 0, sizeof(*globals)); + } + if (globals->ndaActive) return NULL; @@ -314,7 +305,7 @@ GrafPortPtr NDAOpen(void) oldResourceApp = OpenResourceFileByID(readEnable, userId); - globals->winPtr = NewWindow2("\p Listener ", 0, DrawContents, NULL, 0x02, windowRes, rWindParam1); + globals->winPtr = NewWindow2((Pointer)"\p Listener ", 0, DrawContents, NULL, 0x02, windowRes, rWindParam1); SetSysWindow(globals->winPtr); ShowWindow(globals->winPtr); @@ -670,6 +661,9 @@ BOOLEAN NDAAction(EventRecord *sysEvent, int code) unsigned int eventCode; BOOLEAN result = FALSE; + if (globals == NULL) + return result; + switch (code) { case runAction: HandleRun(); diff --git a/Listener/main.rez b/Listener/main.rez index 9e2700f..cd57f7d 100644 --- a/Listener/main.rez +++ b/Listener/main.rez @@ -16,7 +16,7 @@ resource rVersion (1) { { 1, /* Major version number in BCD */ 0, /* Minor version number in BCD */ - 0, /* Bug version number in BCD */ + 1, /* Bug version number in BCD */ release, /* Development phase */ 0 /* Release number */ },