How to avoid execute_from_str leak?

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Post Reply
hchanon
Posts: 1
Joined: Sun Jan 31, 2016 12:04 pm

How to avoid execute_from_str leak?

Post by hchanon » Mon Jul 24, 2017 3:42 pm

Hi,

I built from examples a simple function that executes a "python" line from 'C' code. This code is provided below.

mp_obj_t execute_from_str(const char *str) {
nlr_buf_t nlr;
if (nlr_push(&nlr) == 0) {

mp_lexer_t *lex = mp_lexer_new_from_str_len(0/*MP_QSTR_*/, str, strlen(str), false);
mp_parse_tree_t pt = mp_parse(lex, MP_PARSE_FILE_INPUT);
mp_obj_t module_fun = mp_compile(&pt, lex->source_name, MP_EMIT_OPT_NONE, false);
mp_call_function_0(module_fun);

// m_del_obj(mp_lexer_t, lex); <-------------- Produce unwanted result if activated

nlr_pop();
return 0;
}
else
{
....
}
}

On the GIT VisualStudio project, It seems that even if I call this function with a "\n" dummy script ( execute_from_str("\n") ),
some memory gets allocated and not freed. The lex object seems to be the main memory consumer.
After observing this, my first reflex was to free this object manually using m_del().
This does free the memory but leads to broken script behavior.
I also experimented with the GC and it did not collect that memory at any point.

I understand this behavior could be normal, but my problem still stands.
My application requires me to execute let's say 1000000 python commands by seconds and
and quickly exhaust all my available memory.
Is there any way I could go around this memory consumption issue?

Post Reply