mp_compile(): Properly free module_scope and all nested scopes.
diff --git a/py/compile.c b/py/compile.c
index 0704cc4..7e77832 100644
--- a/py/compile.c
+++ b/py/compile.c
@@ -3156,6 +3156,12 @@
 
     bool had_error = comp->had_error;
     m_del_obj(compiler_t, comp);
+    uint unique_code_id = module_scope->unique_code_id;
+    for (scope_t *s = module_scope; s;) {
+        scope_t *next = s->next;
+        scope_free(s);
+        s = next;
+    }
 
     if (had_error) {
         // TODO return a proper error message
@@ -3163,11 +3169,11 @@
     } else {
 #if MICROPY_EMIT_CPYTHON
         // can't create code, so just return true
-        (void)module_scope; // to suppress warning that module_scope is unused
+        (void)unique_code_id; // to suppress warning that module_scope is unused
         return mp_const_true;
 #else
         // return function that executes the outer module
-        return rt_make_function_from_id(module_scope->unique_code_id);
+        return rt_make_function_from_id(unique_code_id);
 #endif
     }
 }