Browse Source

AP_Scripting: always free the heap and remove scripts

apm_2208
Iampete1 3 years ago committed by Andrew Tridgell
parent
commit
66cdfb015b
  1. 19
      libraries/AP_Scripting/lua_scripts.cpp
  2. 6
      libraries/AP_Scripting/lua_scripts.h

19
libraries/AP_Scripting/lua_scripts.cpp

@ -37,6 +37,10 @@ lua_scripts::lua_scripts(const AP_Int32 &vm_steps, const AP_Int32 &heap_size, co @@ -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) { @@ -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;
}
}

6
libraries/AP_Scripting/lua_scripts.h

@ -53,9 +53,9 @@ class lua_scripts @@ -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; }

Loading…
Cancel
Save