Skip to content

Commit 3f69dab

Browse files
committed
Fix memory leak
1 parent 71d3683 commit 3f69dab

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

mrbgems/picoruby-mruby/src/task.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,13 +224,27 @@ mrb_tcb_new(mrb_state *mrb, enum MrbTaskState task_state, int priority)
224224
return tcb;
225225
}
226226

227+
static void
228+
cleanup_context(mrb_state *mrb, struct mrb_context *c)
229+
{
230+
if (c->stbase) {
231+
mrb_free(mrb, c->stbase);
232+
c->stbase = NULL;
233+
}
234+
if (c->cibase) {
235+
mrb_free(mrb, c->cibase);
236+
c->cibase = NULL;
237+
}
238+
}
239+
227240

228241
#define TASK_STACK_INIT_SIZE 64
229242
#define TASK_CI_INIT_SIZE 8
230243

231244
void
232245
mrb_tcb_init_context(mrb_state *mrb, struct mrb_context *c, struct RProc *proc)
233246
{
247+
cleanup_context(mrb, c);
234248
size_t slen = TASK_STACK_INIT_SIZE;
235249
if (proc->body.irep->nregs > slen) {
236250
slen += proc->body.irep->nregs;

mrbgems/picoruby-sandbox/src/mruby/sandbox.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ mrb_sandbox_exec_vm_code(mrb_state *mrb, mrb_value self)
220220
mrb_value vm_code;
221221
mrb_get_args(mrb, "S", &vm_code);
222222
const uint8_t *code = (const uint8_t *)RSTRING_PTR(vm_code);
223+
if (ss->irep) mrc_irep_free(ss->cc, ss->irep);
223224
ss->irep = mrb_read_irep(mrb, code);
224225
if (sandbox_exec_vm_code_sub(mrb, ss)) {
225226
return mrb_true_value();
@@ -234,6 +235,7 @@ mrb_sandbox_exec_vm_code_from_memory(mrb_state *mrb, mrb_value self)
234235
SS();
235236
mrb_int address;
236237
mrb_get_args(mrb, "i", &address);
238+
if (ss->irep) mrc_irep_free(ss->cc, ss->irep);
237239
ss->irep = mrb_read_irep(mrb, (const uint8_t *)(uintptr_t)address);
238240
if (sandbox_exec_vm_code_sub(mrb, ss)) {
239241
return mrb_true_value();

0 commit comments

Comments
 (0)