diff --git a/bare-arm/main.c b/bare-arm/main.c index 286e41cd4d..983810c892 100644 --- a/bare-arm/main.c +++ b/bare-arm/main.c @@ -35,7 +35,6 @@ void do_str(const char *src) { qstr source_name = mp_lexer_source_name(lex); mp_lexer_free(lex); mp_obj_t module_fun = mp_compile(pn, source_name, MP_EMIT_OPT_NONE, true); - mp_parse_node_free(pn); if (module_fun == mp_const_none) { // compile error diff --git a/py/builtinevex.c b/py/builtinevex.c index 6faeb6a071..63c1547fbe 100644 --- a/py/builtinevex.c +++ b/py/builtinevex.c @@ -63,7 +63,6 @@ STATIC mp_obj_t parse_compile_execute(mp_obj_t o_in, mp_parse_input_kind_t parse // compile the string mp_obj_t module_fun = mp_compile(pn, source_name, MP_EMIT_OPT_NONE, false); - mp_parse_node_free(pn); if (module_fun == mp_const_none) { // TODO handle compile error correctly diff --git a/py/builtinimport.c b/py/builtinimport.c index 9bcc07487d..07b7c199ae 100644 --- a/py/builtinimport.c +++ b/py/builtinimport.c @@ -141,7 +141,6 @@ STATIC void do_load(mp_obj_t module_obj, vstr_t *file) { // compile the imported script mp_obj_t module_fun = mp_compile(pn, source_name, MP_EMIT_OPT_NONE, false); - mp_parse_node_free(pn); if (module_fun == mp_const_none) { // TODO handle compile error correctly diff --git a/py/compile.c b/py/compile.c index c584f0cf54..a9d19d2a94 100644 --- a/py/compile.c +++ b/py/compile.c @@ -3710,6 +3710,9 @@ mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, uint emit_opt, bool is #endif #endif // !MICROPY_EMIT_CPYTHON + // free the parse tree + mp_parse_node_free(pn); + // free the scopes mp_raw_code_t *outer_raw_code = module_scope->raw_code; for (scope_t *s = module_scope; s;) { diff --git a/py/compile.h b/py/compile.h index 52c42aff4f..983a443e95 100644 --- a/py/compile.h +++ b/py/compile.h @@ -33,4 +33,5 @@ enum { MP_EMIT_OPT_ASM_THUMB, }; +// the compiler will free the parse tree (pn) before it returns mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, uint emit_opt, bool is_repl); diff --git a/py/parse.c b/py/parse.c index 26713dfe71..9508b82a02 100644 --- a/py/parse.c +++ b/py/parse.c @@ -179,6 +179,7 @@ void mp_parse_node_free(mp_parse_node_t pn) { mp_uint_t n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns); mp_uint_t rule_id = MP_PARSE_NODE_STRUCT_KIND(pns); if (rule_id == RULE_string) { + m_del(char, (char*)pns->nodes[0], (mp_uint_t)pns->nodes[1]); return; } bool adjust = ADD_BLANK_NODE(rule_id); @@ -562,8 +563,8 @@ mp_parse_node_t mp_parse(mp_lexer_t *lex, mp_parse_input_kind_t input_kind, mp_p if (input_kind != MP_PARSE_SINGLE_INPUT && rule->rule_id == RULE_expr_stmt && peek_result(&parser, 0) == MP_PARSE_NODE_NULL) { mp_parse_node_t p = peek_result(&parser, 1); if ((MP_PARSE_NODE_IS_LEAF(p) && !MP_PARSE_NODE_IS_ID(p)) || MP_PARSE_NODE_IS_STRUCT_KIND(p, RULE_string)) { - pop_result(&parser); - pop_result(&parser); + pop_result(&parser); // MP_PARSE_NODE_NULL + mp_parse_node_free(pop_result(&parser)); // RULE_string push_result_rule(&parser, rule_src_line, rules[RULE_pass_stmt], 0); break; } diff --git a/qemu-arm/main.c b/qemu-arm/main.c index 7ba818f582..97591fe944 100644 --- a/qemu-arm/main.c +++ b/qemu-arm/main.c @@ -35,7 +35,6 @@ void do_str(const char *src) { qstr source_name = mp_lexer_source_name(lex); mp_lexer_free(lex); mp_obj_t module_fun = mp_compile(pn, source_name, MP_EMIT_OPT_NONE, true); - mp_parse_node_free(pn); if (module_fun == mp_const_none) { // compile error diff --git a/qemu-arm/test_main.c b/qemu-arm/test_main.c index 818c558b3d..563e1c91df 100644 --- a/qemu-arm/test_main.c +++ b/qemu-arm/test_main.c @@ -38,7 +38,6 @@ inline void do_str(const char *src) { qstr source_name = mp_lexer_source_name(lex); mp_lexer_free(lex); mp_obj_t module_fun = mp_compile(pn, source_name, MP_EMIT_OPT_NONE, true); - mp_parse_node_free(pn); if (module_fun == mp_const_none) { tt_abort_msg("Computer error"); diff --git a/stmhal/pyexec.c b/stmhal/pyexec.c index cac6f0cf02..01c0173ef4 100644 --- a/stmhal/pyexec.c +++ b/stmhal/pyexec.c @@ -71,7 +71,6 @@ bool parse_compile_execute(mp_lexer_t *lex, mp_parse_input_kind_t input_kind, bo mp_lexer_free(lex); mp_obj_t module_fun = mp_compile(pn, source_name, MP_EMIT_OPT_NONE, is_repl); - mp_parse_node_free(pn); if (module_fun == mp_const_none) { return false;