Objects lifetime and multi threading



I’ve read that objects created in the chaiscript will be destroyed when the Chaiscript object is destroyed in C++ code. What I find is when I use the Chaiscript in std::async task, the objects created in the script are destroyed as soon as thread exits.

Is this an expected behavior ? I was expecting that the objects should stay valid until the Chaiscript object is destroyed. If this is the case then I would have to managed the object in the application code instead.



Objects survive until no one has a handle to them any longer.

For the case of a local variable, this means they are destroyed as soon as the local scope exits, which is probably what you are seeing.

There is no global namespace for variables, unless you intentionally put a variable there (ie with GLOBAL) or intentionally keep a handle to the Boxed_Value inside of your C++ code.

The intention was to as closely as possible model the lifetime of objects in C++.

Hopefully this discussion is helpful as well: https://github.com/ChaiScript/ChaiScript/issues/214#issuecomment-146198716

Let me know if you have any other questions or if the behavior you are seeing seems contrary to this information.



Does this mean … an object created in the script would be destroyed after the script returns?

It actually makes sense though. But if that the case, I see my code running well if I remove the async task.


It depends at what scope the variable is created. The top level scope sticks around, for various reasons, but anything below the top level scope or at function level is destroyed when the scope exits.