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().

This commit is contained in:
Jeremy Rand 2023-08-22 20:17:56 -04:00
parent 0071f3d4eb
commit 4de42bf26e
3 changed files with 24 additions and 30 deletions

View File

@ -7,17 +7,17 @@
<key>Archive.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>3</integer>
<integer>4</integer>
</dict>
<key>Binary.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>4</integer>
<integer>2</integer>
</dict>
<key>DiskImage.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>2</integer>
<integer>3</integer>
</dict>
<key>Listener.xcscheme_^#shared#^_</key>
<dict>

View File

@ -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();

View File

@ -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 */
},