From: Mart Lubbers Date: Thu, 25 Feb 2021 12:51:44 +0000 (+0100) Subject: fix memory leaks X-Git-Url: https://git.martlubbers.net/?a=commitdiff_plain;h=796043477c3e381255dc42f98324077e21b351e8;p=ccc.git fix memory leaks --- diff --git a/sem.c b/sem.c index ffb7019..48ea8a0 100644 --- a/sem.c +++ b/sem.c @@ -93,6 +93,10 @@ void type_comp(struct gamma *gamma, int ndecls, struct fundecl **decl) struct type *t = subst_apply_t(s0, fs[i]); gamma_insert(gamma, decl[i]->ident, scheme_generalise(gamma, t)); + for (int j = 0; inatypes; j++) { + free(decl[i]->atypes[i]); + } + free(decl[i]->atypes); decl[i]->atypes = safe_malloc(decl[i]->nargs*sizeof(struct type *)); decl[i]->natypes = decl[i]->nargs; for (int j = 0; jnargs; j++) { diff --git a/sem/hm.c b/sem/hm.c index 45104e5..e19c9e6 100644 --- a/sem/hm.c +++ b/sem/hm.c @@ -144,8 +144,8 @@ struct subst *infer_expr(struct gamma *gamma, struct expr *expr, struct type *ty if ((s = gamma_lookup(gamma, expr->data.efuncall.ident)) == NULL) type_error(expr->loc, "Unbound function: %s\n" , expr->data.efuncall.ident); - struct type *t = scheme_instantiate(gamma, s); - + struct type *ft = scheme_instantiate(gamma, s); + struct type *t = ft; struct subst *s0 = subst_id(); //Infer args for (int i = 0; idata.efuncall.nargs; i++) { @@ -156,8 +156,7 @@ struct subst *infer_expr(struct gamma *gamma, struct expr *expr, struct type *ty s1 = infer_expr(gamma, expr->data.efuncall.args[i], t->data.tarrow.l); s0 = subst_union(s1, s0); - subst_apply_g(s0, gamma); - t = subst_apply_t(s0, t->data.tarrow.r); + t = t->data.tarrow.r; } if (t->type == tarrow) type_error(expr->loc, true, @@ -167,7 +166,7 @@ struct subst *infer_expr(struct gamma *gamma, struct expr *expr, struct type *ty //Infer return type s1 = unify(expr->loc, t, type); s0 = subst_union(s1, s0); - type_free(t); + type_free(ft); //TODO fields return s0; case eint: @@ -209,7 +208,7 @@ struct subst *infer_expr(struct gamma *gamma, struct expr *expr, struct type *ty struct subst *infer_stmt(struct gamma *gamma, struct stmt *stmt, struct type *type) { - struct subst *s0, *s1, *s2; + struct subst *s0, *s1; struct type *f1; switch (stmt->type) { case sassign: diff --git a/sem/hm/scheme.c b/sem/hm/scheme.c index a374f6b..166bba2 100644 --- a/sem/hm/scheme.c +++ b/sem/hm/scheme.c @@ -10,9 +10,7 @@ struct type *scheme_instantiate(struct gamma *gamma, struct scheme *sch) subst_insert(s, safe_strdup(sch->var[i]), gamma_fresh(gamma)); struct type *t = subst_apply_t(s, type_dup(sch->type)); - for (int i = 0; invar; i++) - free(s->vars[i]); - free(s); + subst_free(s); return t; } diff --git a/sem/hm/subst.c b/sem/hm/subst.c index f232c25..032f4b1 100644 --- a/sem/hm/subst.c +++ b/sem/hm/subst.c @@ -106,6 +106,7 @@ struct scheme *subst_apply_s(struct subst *subst, struct scheme *scheme) subst_insert(s, subst->vars[j], subst->types[j]); } scheme->type = subst_apply_t(s, scheme->type); + subst_free(s); return scheme; }