Add sync_cache to set_state code to fix logic flaw in resetting of state and memory leak. #92
This commit is contained in:
@@ -342,13 +342,10 @@ namespace chaiscript
|
|||||||
|
|
||||||
void sync_cache()
|
void sync_cache()
|
||||||
{
|
{
|
||||||
m_stack_holder->stack->get<0>().clear();
|
|
||||||
|
|
||||||
#ifndef CHAISCRIPT_NO_THREADS
|
#ifndef CHAISCRIPT_NO_THREADS
|
||||||
boost::shared_lock<boost::shared_mutex> l(m_mutex);
|
boost::shared_lock<boost::shared_mutex> l(m_mutex);
|
||||||
#endif
|
#endif
|
||||||
|
sync_cache_no_lock();
|
||||||
get_function_cache() = m_state.m_functions;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -670,6 +667,7 @@ namespace chaiscript
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
m_state = t_state;
|
m_state = t_state;
|
||||||
|
sync_cache_no_lock();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -688,6 +686,13 @@ namespace chaiscript
|
|||||||
return m_stack_holder->function_cache;
|
return m_stack_holder->function_cache;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void sync_cache_no_lock()
|
||||||
|
{
|
||||||
|
m_stack_holder->stack->get<0>().clear();
|
||||||
|
|
||||||
|
get_function_cache() = m_state.m_functions;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Throw a reserved_word exception if the name is not allowed
|
* Throw a reserved_word exception if the name is not allowed
|
||||||
|
83
src/memory_leak_test.cpp
Normal file
83
src/memory_leak_test.cpp
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
#include "chaiscript/chaiscript.hpp"
|
||||||
|
|
||||||
|
using namespace chaiscript;
|
||||||
|
|
||||||
|
std::string get_next_command() {
|
||||||
|
#ifdef READLINE_AVAILABLE
|
||||||
|
char *input_raw;
|
||||||
|
input_raw = readline("eval> ");
|
||||||
|
add_history(input_raw);
|
||||||
|
return std::string(input_raw);
|
||||||
|
#else
|
||||||
|
std::string retval;
|
||||||
|
std::cout << "eval> ";
|
||||||
|
std::getline(std::cin, retval);
|
||||||
|
return retval;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
void fuction(void)
|
||||||
|
{
|
||||||
|
// do nothing
|
||||||
|
}
|
||||||
|
|
||||||
|
class test
|
||||||
|
{
|
||||||
|
ChaiScript chai;
|
||||||
|
ChaiScript::State backupState;
|
||||||
|
public:
|
||||||
|
test()
|
||||||
|
{
|
||||||
|
backupState = chai.get_state();
|
||||||
|
}
|
||||||
|
~test(){}
|
||||||
|
|
||||||
|
void ResetState()
|
||||||
|
{
|
||||||
|
chai.set_state(backupState);
|
||||||
|
chai.add(fun(&fuction),"Whatever()");
|
||||||
|
}
|
||||||
|
void RunFile(std::string sFile)
|
||||||
|
{
|
||||||
|
chaiscript::Boxed_Value val;
|
||||||
|
try {
|
||||||
|
chaiscript::Boxed_Value val = chai.eval_file(sFile);
|
||||||
|
}
|
||||||
|
catch (std::exception &e) {
|
||||||
|
std::cout << e.what() << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]) {
|
||||||
|
|
||||||
|
test myChai;
|
||||||
|
|
||||||
|
|
||||||
|
std::string command = "";
|
||||||
|
|
||||||
|
//
|
||||||
|
// this loop increases memoryusage, if RunFile is not called (just hittin enter)
|
||||||
|
// as soon RunFile gets called, memory will be freed.
|
||||||
|
//
|
||||||
|
// scenario1 - RunFile gets called every Loop: memoryusage does not change
|
||||||
|
// scenario2 - RunFile gets never called (just hitting enter): memoryusage increases every loop
|
||||||
|
// scenario3 - RunFile gets in changing intervals: memoryusage goes up and down, but never as
|
||||||
|
// low as in case 1 scenario3 :
|
||||||
|
|
||||||
|
while(command != "quit")
|
||||||
|
{
|
||||||
|
for(int i = 1; i < 200; i++)
|
||||||
|
myChai.ResetState();
|
||||||
|
|
||||||
|
if(command == "runfile")
|
||||||
|
myChai.RunFile("Test.chai");
|
||||||
|
|
||||||
|
command = get_next_command();
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user