diff --git a/include/sqrat/sqratUtil.h b/include/sqrat/sqratUtil.h index 35faeba4..66be8615 100644 --- a/include/sqrat/sqratUtil.h +++ b/include/sqrat/sqratUtil.h @@ -1506,7 +1506,7 @@ struct StackGuard ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ~StackGuard() { - sq_pop(m_VM, sq_gettop(m_VM) - m_Top); + Restore(); } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -1519,6 +1519,20 @@ struct StackGuard ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// StackGuard & operator = (StackGuard &&) = delete; + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + /// Restore the stack to what was known to be. + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + void Restore() const + { + // Retrieve the new stack top + const SQInteger top = sq_gettop(m_VM); + // Did the stack size change? + if (top > m_Top) + { + sq_pop(m_VM, top - m_Top); // Trim the stack + } + } + private: HSQUIRRELVM m_VM; ///< The VM where the stack should be restored. diff --git a/source/Routine.hpp b/source/Routine.hpp index 8718a9d8..4a1036f8 100644 --- a/source/Routine.hpp +++ b/source/Routine.hpp @@ -146,6 +146,8 @@ private: } // Make the function call and store the result const SQRESULT res = sq_call(vm, mArgc + 1, false, !mQuiet); + // Pop the callback object from the stack + sq_pop(vm, 1); // Validate the result if (SQ_FAILED(res)) { diff --git a/source/Tasks.cpp b/source/Tasks.cpp index f0aaffaf..101737f1 100644 --- a/source/Tasks.cpp +++ b/source/Tasks.cpp @@ -86,6 +86,8 @@ Tasks::Interval Tasks::Task::Execute() } // Make the function call and store the result const SQRESULT res = sq_call(vm, mArgc + 1, false, ErrorHandling::IsEnabled()); + // Pop the callback object from the stack + sq_pop(vm, 1); // Validate the result if (SQ_FAILED(res)) {