Skip to content

Commit d552db4

Browse files
committed
Merge pull request #214 from pfalcon/compile-mem-leaks
Memory leaks in lexer/compiler
2 parents 3257d35 + fd31358 commit d552db4

File tree

4 files changed

+15
-2
lines changed

4 files changed

+15
-2
lines changed

py/compile.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3156,18 +3156,24 @@ mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, bool is_repl) {
31563156

31573157
bool had_error = comp->had_error;
31583158
m_del_obj(compiler_t, comp);
3159+
uint unique_code_id = module_scope->unique_code_id;
3160+
for (scope_t *s = module_scope; s;) {
3161+
scope_t *next = s->next;
3162+
scope_free(s);
3163+
s = next;
3164+
}
31593165

31603166
if (had_error) {
31613167
// TODO return a proper error message
31623168
return mp_const_none;
31633169
} else {
31643170
#if MICROPY_EMIT_CPYTHON
31653171
// can't create code, so just return true
3166-
(void)module_scope; // to suppress warning that module_scope is unused
3172+
(void)unique_code_id; // to suppress warning that module_scope is unused
31673173
return mp_const_true;
31683174
#else
31693175
// return function that executes the outer module
3170-
return rt_make_function_from_id(module_scope->unique_code_id);
3176+
return rt_make_function_from_id(unique_code_id);
31713177
#endif
31723178
}
31733179
}

py/lexer.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -691,6 +691,7 @@ void mp_lexer_free(mp_lexer_t *lex) {
691691
lex->stream_close(lex->stream_data);
692692
}
693693
vstr_clear(&lex->vstr);
694+
m_del(uint16_t, lex->indent_level, lex->alloc_indent_level);
694695
m_del_obj(mp_lexer_t, lex);
695696
}
696697
}

py/scope.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ scope_t *scope_new(scope_kind_t kind, mp_parse_node_t pn, qstr source_file, uint
6060
return scope;
6161
}
6262

63+
void scope_free(scope_t *scope) {
64+
m_del(id_info_t, scope->id_info, scope->id_info_alloc);
65+
m_del(scope_t, scope, 1);
66+
}
67+
6368
id_info_t *scope_find_or_add_id(scope_t *scope, qstr qstr, bool *added) {
6469
for (int i = 0; i < scope->id_info_len; i++) {
6570
if (scope->id_info[i].qstr == qstr) {

py/scope.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ typedef struct _scope_t {
5656
} scope_t;
5757

5858
scope_t *scope_new(scope_kind_t kind, mp_parse_node_t pn, qstr source_file, uint unique_code_id, uint emit_options);
59+
void scope_free(scope_t *scope);
5960
id_info_t *scope_find_or_add_id(scope_t *scope, qstr qstr, bool *added);
6061
id_info_t *scope_find(scope_t *scope, qstr qstr);
6162
id_info_t *scope_find_global(scope_t *scope, qstr qstr);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy