So, I believe I understand.
- You are creating the objects inside of ChaiScript, so there is 1 reference to them: inside of the script engine.
- You pass the objects to your node structure, which only takes a pointer, so as @ninnghazad pointed out, there’s no reference count increase.
- You destroy the ChaiScript engine, which pops the only reference count from the stack and the nodes are destroyed
Someone needs to keep ownership of those nodes. You could do it with a factory as you suggest, or you could share the nodes with ChaiScript/C++, something like:
var n = Node();
nodes object will extend the lifetime of any object pushed into it for the entire life of the
std::vector<Boxed_Value> that is shared between ChaiScript and C++.