diff --git a/libraries/AP_Scripting/lua_scripts.cpp b/libraries/AP_Scripting/lua_scripts.cpp index d1c8badff71d9..b1b60a29c8a71 100644 --- a/libraries/AP_Scripting/lua_scripts.cpp +++ b/libraries/AP_Scripting/lua_scripts.cpp @@ -402,19 +402,18 @@ void lua_scripts::remove_script(lua_State *L, script_info *script) { } } + { + // Remove from running checksum + WITH_SEMAPHORE(crc_sem); + running_checksum ^= script->crc; + } + if (L != nullptr) { // state could be null if we are force killing all scripts luaL_unref(L, LUA_REGISTRYINDEX, script->lua_ref); } _heap.deallocate(script->name); _heap.deallocate(script); - - { - // Remove from running checksum - WITH_SEMAPHORE(crc_sem); - running_checksum ^= script->crc; - } - } void lua_scripts::reschedule_script(script_info *script) {