I am creating scripting language. When I allocate thing ,it\'s allocate the thing and returns the address and then I do whatever with it and then delete it. I can\'t contro
You can use shared_ptr<> to hold your pointer, and use weak_ptr<> to pass your pointer around to consumers of the object. You delete the object by destroying the shared_ptr<> object, and then all the weak_ptr<>s will become expired.
std::weak_ptr wptr;
assert(wptr.expired());
{
std::shared_ptr intptr(new int);
wptr = intptr;
assert(!wptr.expired());
}
assert(wptr.expired());
So, your exists check would be to check if the weak_ptr<> is expired or not.
To make the usage of the construct a little more concrete:
Script code Hypothetical C++ code that gets executed
---- ----
var MyVar=new MyStruct(); var_map["MyVar"]
= std::shared_ptr(new Obj("MyStruct"));
Func(MyVar); invoke("Func", std::weak_ptr(var_map["MyVar"]));
exists(arg0) !args[0].expired()
delete(MyVar); var_map.erase("MyVar");
If the script is to operate in a multi-threaded environment, then the weak_ptr<> state is a critical section.