Skip to content

Commit 4f2dc81

Browse files
committed
Fixed memory leaks & added cleanup to error exits
Fixed memory leaks in the preprocessor, and Hashtable code. Added memory cleanup stuff to all the error exit paths.
1 parent b81b247 commit 4f2dc81

File tree

4 files changed

+55
-8
lines changed

4 files changed

+55
-8
lines changed

PropellerCompiler/Utilities.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ struct HashNode
8383

8484
~HashNode()
8585
{
86+
if (pNext)
87+
{
88+
delete pNext;
89+
}
8690
delete pValue;
8791
}
8892
};

SpinSource/openspin.cpp

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,18 @@ void ComposeRAM(unsigned char** ppBuffer, int& bufferSize, bool bDATonly, bool b
470470
}
471471
}
472472

473+
void CleanupMemory()
474+
{
475+
// cleanup
476+
delete [] s_pCompilerData->list;
477+
delete [] s_pCompilerData->doc;
478+
delete [] s_pCompilerData->obj;
479+
delete [] s_pCompilerData->source;
480+
CleanObjectHeap();
481+
CleanupPathEntries();
482+
Cleanup();
483+
}
484+
473485
int main(int argc, char* argv[])
474486
{
475487
char* infile = NULL;
@@ -511,6 +523,7 @@ int main(int argc, char* argv[])
511523
else
512524
{
513525
Usage();
526+
CleanupMemory();
514527
return 1;
515528
}
516529
AddPath(p);
@@ -528,12 +541,14 @@ int main(int argc, char* argv[])
528541
else
529542
{
530543
Usage();
544+
CleanupMemory();
531545
return 1;
532546
}
533547
sscanf(p, "%d", &eeprom_size);
534548
if (eeprom_size > 16777216)
535549
{
536550
Usage();
551+
CleanupMemory();
537552
return 1;
538553
}
539554
break;
@@ -550,6 +565,7 @@ int main(int argc, char* argv[])
550565
else
551566
{
552567
Usage();
568+
CleanupMemory();
553569
return 1;
554570
}
555571
break;
@@ -576,13 +592,15 @@ int main(int argc, char* argv[])
576592
else
577593
{
578594
Usage();
595+
CleanupMemory();
579596
return 1;
580597
}
581598
// just skipping these for now
582599
}
583600
else
584601
{
585602
Usage();
603+
CleanupMemory();
586604
return 1;
587605
}
588606
break;
@@ -626,6 +644,7 @@ int main(int argc, char* argv[])
626644
case 'h':
627645
default:
628646
Usage();
647+
CleanupMemory();
629648
return 1;
630649
break;
631650
}
@@ -635,6 +654,7 @@ int main(int argc, char* argv[])
635654
if (infile)
636655
{
637656
Usage();
657+
CleanupMemory();
638658
return 1;
639659
}
640660
infile = argv[i];
@@ -645,6 +665,7 @@ int main(int argc, char* argv[])
645665
if (!infile)
646666
{
647667
Usage();
668+
CleanupMemory();
648669
return 1;
649670
}
650671

@@ -678,6 +699,7 @@ int main(int argc, char* argv[])
678699
else
679700
{
680701
Usage();
702+
CleanupMemory();
681703
return 1;
682704
}
683705
// add any predefined symbols here - note that when using the
@@ -688,6 +710,7 @@ int main(int argc, char* argv[])
688710
else
689711
{
690712
Usage();
713+
CleanupMemory();
691714
return 1;
692715
}
693716
break;
@@ -715,6 +738,7 @@ int main(int argc, char* argv[])
715738
{
716739
printf("ERROR: spinfile must have .spin extension. You passed in: %s\n", infile);
717740
Usage();
741+
CleanupMemory();
718742
return 1;
719743
}
720744
else
@@ -786,6 +810,7 @@ int main(int argc, char* argv[])
786810

787811
if (!CompileRecursively(infile, bQuiet, bFileTreeOutputOnly))
788812
{
813+
CleanupMemory();
789814
return 1;
790815
}
791816

@@ -940,14 +965,7 @@ int main(int argc, char* argv[])
940965
}
941966
}
942967

943-
// cleanup
944-
delete [] s_pCompilerData->list;
945-
delete [] s_pCompilerData->doc;
946-
delete [] s_pCompilerData->obj;
947-
delete [] s_pCompilerData->source;
948-
CleanObjectHeap();
949-
CleanupPathEntries();
950-
Cleanup();
968+
CleanupMemory();
951969

952970
return 0;
953971
}

SpinSource/preprocess.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -853,6 +853,7 @@ pp_run(struct preprocess *pp)
853853
} else {
854854
char *line = flexbuf_get(&pp->line);
855855
flexbuf_addstr(&pp->whole, line);
856+
free(line);
856857
}
857858
}
858859
pp_pop_file(pp);
@@ -863,6 +864,8 @@ char *
863864
pp_finish(struct preprocess *pp)
864865
{
865866
flexbuf_addchar(&pp->whole, 0);
867+
pp_clear_define_state(pp);
868+
flexbuf_delete(&pp->line);
866869
return flexbuf_get(&pp->whole);
867870
}
868871

@@ -908,6 +911,25 @@ pp_restore_define_state(struct preprocess *pp, void *vp)
908911
pp->defs = x;
909912
}
910913

914+
void
915+
pp_clear_define_state(struct preprocess *pp)
916+
{
917+
struct predef *x, *old;
918+
919+
x = pp->defs;
920+
while (x) {
921+
old = x;
922+
x = old->next;
923+
if (old->flags & PREDEF_FLAG_FREEDEFS)
924+
{
925+
free((void *)old->name);
926+
if (old->def) free((void *)old->def);
927+
}
928+
free(old);
929+
}
930+
pp->defs = 0;
931+
}
932+
911933
#ifdef TEST
912934
char *
913935
preprocess(const char *filename, bool alternate)

SpinSource/preprocess.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ void *pp_get_define_state(struct preprocess *pp);
9494
/* restore the define state to the state given by a previous call to get_define_state */
9595
void pp_restore_define_state(struct preprocess *pp, void *ptr);
9696

97+
/* clear all the define state */
98+
void pp_clear_define_state(struct preprocess *pp);
99+
97100
/* actually perform the preprocessing on all files that have been pushed so far */
98101
void pp_run(struct preprocess *pp);
99102

0 commit comments

Comments
 (0)