diff --git a/python/port/mod/turtle/modturtle.cpp b/python/port/mod/turtle/modturtle.cpp index c2c8e78f8..44c78c0e4 100644 --- a/python/port/mod/turtle/modturtle.cpp +++ b/python/port/mod/turtle/modturtle.cpp @@ -8,7 +8,7 @@ static Turtle sTurtle; void modturtle_gc_collect() { // Mark the shared sTurtle object as a GC root - gc_collect_root((void **)&sTurtle, sizeof(Turtle)); + gc_collect_root((void **)&sTurtle, sizeof(Turtle)/sizeof(void *)); } void modturtle_view_did_disappear() { diff --git a/python/port/port.cpp b/python/port/port.cpp index 2eb068e18..410bc4cca 100644 --- a/python/port/port.cpp +++ b/python/port/port.cpp @@ -138,9 +138,9 @@ void gc_collect(void) { /* On the device, the stack is stored in reverse order, but it might not be * the case on a computer. We thus have to take the absolute value of the * addresses difference. */ - size_t stackLength; + size_t stackLengthInByte; void ** scanStart; - if ((uintptr_t)python_stack_top > (uintptr_t)®s) { + if ((uintptr_t)python_stack_top > (uintptr_t)regs_ptr) { /* To compute the stack length: * regs @@ -149,7 +149,7 @@ void gc_collect(void) { * ^®s ^python_stack_top * */ - stackLength = ceil((float)((uintptr_t)python_stack_top - (uintptr_t)®s) / (float)sizeof(uintptr_t)); + stackLengthInByte = (uintptr_t)python_stack_top - (uintptr_t)regs_ptr; scanStart = regs_ptr; } else { @@ -161,12 +161,13 @@ void gc_collect(void) { * ^python_stack_top ^®s * */ - size_t sizeOfRegs = ceil(((float)sizeof(regs))/(float)sizeof(uintptr_t)); - stackLength = (size_t)ceil(((float)((uintptr_t)(®s) - (uintptr_t)python_stack_top)) / (float)sizeof(uintptr_t)) + sizeOfRegs; + stackLengthInByte = (uintptr_t)regs_ptr - (uintptr_t)python_stack_top + sizeof(regs); scanStart = (void **)python_stack_top; } - gc_collect_root(scanStart, stackLength); + /* Memory error detectors might find an error here as they might split regs + * and stack memory zones. */ + gc_collect_root(scanStart, stackLengthInByte/sizeof(void *)); gc_collect_end(); }