Skip to content

[2.7] properly free memory in pgen. (closes bpo-27780) #7869

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Include/grammar.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ typedef struct {
/* FUNCTIONS */

grammar *newgrammar(int start);
void freegrammar(grammar *g);
dfa *adddfa(grammar *g, int type, char *name);
int addstate(dfa *d);
void addarc(dfa *d, int from, int to, int lbl);
Expand Down
18 changes: 18 additions & 0 deletions Parser/grammar.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,24 @@ newgrammar(int start)
return g;
}

void
freegrammar(grammar *g)
{
int i;
for (i = 0; i < g->g_ndfas; i++) {
int j;
free(g->g_dfa[i].d_name);
for (j = 0; j < g->g_dfa[i].d_nstates; j++)
PyObject_FREE(g->g_dfa[i].d_state[j].s_arc);
PyObject_FREE(g->g_dfa[i].d_state);
}
PyObject_FREE(g->g_dfa);
for (i = 0; i < g->g_ll.ll_nlabels; i++)
free(g->g_ll.ll_label[i].lb_str);
PyObject_FREE(g->g_ll.ll_label);
PyObject_FREE(g);
}

dfa *
adddfa(grammar *g, int type, char *name)
{
Expand Down
18 changes: 16 additions & 2 deletions Parser/pgen.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,16 @@ newnfagrammar(void)
return gr;
}

static void
freenfagrammar(nfagrammar *gr)
{
for (int i = 0; i < gr->gr_nnfas; i++) {
PyObject_FREE(gr->gr_nfa[i]->nf_state);
}
PyObject_FREE(gr->gr_nfa);
PyObject_FREE(gr);
}

static nfa *
addnfa(nfagrammar *gr, char *name)
{
Expand Down Expand Up @@ -488,7 +498,11 @@ makedfa(nfagrammar *gr, nfa *nf, dfa *d)

convert(d, xx_nstates, xx_state);

/* XXX cleanup */
for (int i = 0; i < xx_nstates; i++) {
for (int j = 0; j < xx_state[i].ss_narcs; j++)
delbitset(xx_state[i].ss_arc[j].sa_bitset);
PyObject_FREE(xx_state[i].ss_arc);
}
PyObject_FREE(xx_state);
}

Expand Down Expand Up @@ -669,7 +683,7 @@ pgen(node *n)
g = maketables(gr);
translatelabels(g);
addfirstsets(g);
PyObject_FREE(gr);
freenfagrammar(gr);
return g;
}

Expand Down
1 change: 1 addition & 0 deletions Parser/pgenmain.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ main(int argc, char **argv)
printf("Writing %s ...\n", graminit_h);
printnonterminals(g, fp);
fclose(fp);
freegrammar(g);
Py_Exit(0);
return 0; /* Make gcc -Wall happy */
}
Expand Down