diff --git a/core/iwasm/aot/aot_runtime.c b/core/iwasm/aot/aot_runtime.c index e0d56a698..a7d6ef2d9 100644 --- a/core/iwasm/aot/aot_runtime.c +++ b/core/iwasm/aot/aot_runtime.c @@ -1264,16 +1264,6 @@ aot_deinstantiate(AOTModuleInstance *module_inst, bool is_sub_inst) wasm_exec_env_destroy((WASMExecEnv *)module_inst->exec_env_singleton); } -#if WASM_ENABLE_LIBC_WASI != 0 - /* Destroy wasi resource before freeing app heap, since some fields of - wasi contex are allocated from app heap, and if app heap is freed, - these fields will be set to NULL, we cannot free their internal data - which may allocated from global heap. */ - /* Only destroy wasi ctx in the main module instance */ - if (!is_sub_inst) - wasm_runtime_destroy_wasi((WASMModuleInstanceCommon *)module_inst); -#endif - #if WASM_ENABLE_PERF_PROFILING != 0 if (module_inst->func_perf_profilings) wasm_runtime_free(module_inst->func_perf_profilings); @@ -1306,10 +1296,14 @@ aot_deinstantiate(AOTModuleInstance *module_inst, bool is_sub_inst) wasm_runtime_free( ((AOTModuleInstanceExtra *)module_inst->e)->c_api_func_imports); + if (!is_sub_inst) { +#if WASM_ENABLE_LIBC_WASI != 0 + wasm_runtime_destroy_wasi((WASMModuleInstanceCommon *)module_inst); +#endif #if WASM_ENABLE_WASI_NN != 0 - if (!is_sub_inst) wasi_nn_destroy(module_inst); #endif + } wasm_runtime_free(module_inst); } diff --git a/core/iwasm/interpreter/wasm_runtime.c b/core/iwasm/interpreter/wasm_runtime.c index 84155a05a..497e6b19e 100644 --- a/core/iwasm/interpreter/wasm_runtime.c +++ b/core/iwasm/interpreter/wasm_runtime.c @@ -2216,16 +2216,6 @@ wasm_deinstantiate(WASMModuleInstance *module_inst, bool is_sub_inst) sub_module_deinstantiate(module_inst); #endif -#if WASM_ENABLE_LIBC_WASI != 0 - /* Destroy wasi resource before freeing app heap, since some fields of - wasi contex are allocated from app heap, and if app heap is freed, - these fields will be set to NULL, we cannot free their internal data - which may allocated from global heap. */ - /* Only destroy wasi ctx in the main module instance */ - if (!is_sub_inst) - wasm_runtime_destroy_wasi((WASMModuleInstanceCommon *)module_inst); -#endif - if (module_inst->memory_count > 0) memories_deinstantiate(module_inst, module_inst->memories, module_inst->memory_count); @@ -2258,10 +2248,14 @@ wasm_deinstantiate(WASMModuleInstance *module_inst, bool is_sub_inst) if (module_inst->e->c_api_func_imports) wasm_runtime_free(module_inst->e->c_api_func_imports); + if (!is_sub_inst) { +#if WASM_ENABLE_LIBC_WASI != 0 + wasm_runtime_destroy_wasi((WASMModuleInstanceCommon *)module_inst); +#endif #if WASM_ENABLE_WASI_NN != 0 - if (!is_sub_inst) wasi_nn_destroy(module_inst); #endif + } wasm_runtime_free(module_inst); }