AP_Scripting: always free the heap and remove scripts

This commit is contained in:
Iampete1 2022-04-20 00:34:23 +01:00 committed by Andrew Tridgell
parent 985e24275a
commit 66cdfb015b
2 changed files with 22 additions and 3 deletions

View File

@ -37,6 +37,10 @@ lua_scripts::lua_scripts(const AP_Int32 &vm_steps, const AP_Int32 &heap_size, co
_heap = hal.util->allocate_heap_memory(heap_size);
}
lua_scripts::~lua_scripts() {
free(_heap);
}
void lua_scripts::hook(lua_State *L, lua_Debug *ar) {
lua_scripts::overtime = true;
@ -535,4 +539,19 @@ void lua_scripts::run(void) {
}
}
}
// make sure all scripts have been removed
while (scripts != nullptr) {
remove_script(lua_state, scripts);
}
if (lua_state != nullptr) {
lua_close(lua_state); // shutdown the old state
lua_state = nullptr;
}
if (error_msg_buf != nullptr) {
hal.util->heap_realloc(_heap, error_msg_buf, 0);
error_msg_buf = nullptr;
}
}

View File

@ -53,9 +53,9 @@ class lua_scripts
public:
lua_scripts(const AP_Int32 &vm_steps, const AP_Int32 &heap_size, const AP_Int8 &debug_options, struct AP_Scripting::terminal_s &_terminal);
/* Do not allow copies */
lua_scripts(const lua_scripts &other) = delete;
lua_scripts &operator=(const lua_scripts&) = delete;
~lua_scripts();
CLASS_NO_COPY(lua_scripts);
// return true if initialisation failed
bool heap_allocated() const { return _heap != nullptr; }