Hi,
Is there a way to track a certain object's destruction and get a callback?
only way i can think of is implementing a custom free function.
thanks.
Callback when object destroyed
Any object in particular?
The destructor seems to be a good place to put this callback in.
The destructor seems to be a good place to put this callback in.
AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game
Any object in particular?
The destructor seems to be a good place to put this callback in.
i want to develop an event system without forcing scripter to put event unregisteration call in object destructor.
because they forget and cause all sorts of shenanigans.
this is my second time implementing an event system. i dont want to make the same mistake of forcing script syntax.
i thought of custom free function, which works but every time an object destroy i make an address lookup.
i do not like it, it is slow for long running scripts and error prone.
asIScriptEngine has userdata destruction callback when it is destroyed, but asIScriptObject lacks this sort of functionality.
is there an alternative way?
edit:
edit:
ok. i wasnt overthinking it. i must be getting tired.
see post below for details.
void TESTEVENT()
{
dbg("test event fired!");
}
class TEST
{
uint eid;
TEST()
{
eid = EM2.RegisterEvent("KeyPressed", Function(@this, "TESTEVENT")); // keeps reference to object, keeps this object from destroying it self
}
void TESTEVENT()
{
dbg("test event fired!");
}
~TEST()
{
dbg("TEST destroyed");
// EM2.UnregisterEvent(eid); // i dont want to force scripter to call this.
}
};
int main()
{
TEST t; // never destroyed
}
i want that event to be unregistered when object is destroyed. but object cant be destroyed since event holds reference to it.
circular reference.
but cant break it since never get callback about object's destruction or events destruction.
i can make event to not hold reference to object. but this time it will cause error unless scripter calls EM2.UnregisterEvent(eid); in ~TEST().
what do you think the best way to handle this?
In this case the object will never be destroyed as the EM2 object holds a reference to it, for that reason the destructor will never be called, nor would a potential callback on the object's destruction.
I understand you want the event to be unregistered when EM2 is the only one holding a reference to the object, right? The trick is how EM2 should know it is holding the last reference.
If you can force the event handler to always inherit from a specific base class, then that base class could implement the destructor to unregister the event. If you know the event will always be properly unregistered upon destruction, then the EM2 object can hold a weak reference to the event handler, i.e. a direct pointer without increasing the reference count.
It might look something like this:
I understand you want the event to be unregistered when EM2 is the only one holding a reference to the object, right? The trick is how EM2 should know it is holding the last reference.
If you can force the event handler to always inherit from a specific base class, then that base class could implement the destructor to unregister the event. If you know the event will always be properly unregistered upon destruction, then the EM2 object can hold a weak reference to the event handler, i.e. a direct pointer without increasing the reference count.
It might look something like this:
// This script class should be added as a standard implementation by the application, without the script being able to modify it
// The class is also shared to make sure all modules have the same implementation
shared class EventBase
{
~EventBase()
{
EM2.UnregisterEvent(this); // EM2 drops its reference to the object, without calling Release() as it never called AddRef()
}
}
class TEST : EventBase
{
TEST()
{
EM2.RegisterEvent("KeyPressed", Function(@this, "TESTEVENT")); // EM2 keeps a weak reference to the EventBase, i.e without calling AddRef()
}
void TESTEVENT()
{
dbg("test event fired!");
}
~TEST()
{
dbg("TEST destroyed");
}
};
int main()
{
TEST t; // object is destroyed when it goes out of scope
}
AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game
unfortunately most of the classes already inherit base classes.
and none of script interface force inheritance to obtain some functionality.
i am thinking:
never hold reference to the object.
then time comes to object to be freed, i get the pointer and check if any event holds pointer to it.
since they are scriptobject, they must be freed with my custom free function. i can catch them there.
tested this and it works.
but, is there a way to associate a user data with a scriptobject? and get a destructor callback back when it destroys?
that would be a much sweeter solution.
and none of script interface force inheritance to obtain some functionality.
i am thinking:
never hold reference to the object.
then time comes to object to be freed, i get the pointer and check if any event holds pointer to it.
since they are scriptobject, they must be freed with my custom free function. i can catch them there.
void MyFree(void *addr)
{
EventMgr->ObjectDeleted(addr); // EventMgr has a map of obj pointers with eventids
free(addr);
}
tested this and it works.
but, is there a way to associate a user data with a scriptobject? and get a destructor callback back when it destroys?
that would be a much sweeter solution.
I don't much like the idea of keeping user data in the script object. It would be an unnecessary overhead in most of the cases where user data is not even used.
I'll keep it in mind though. Perhaps I can come up with something to make the userdata pointer optional, so the overhead is only there when it is actually used.
I'll keep it in mind though. Perhaps I can come up with something to make the userdata pointer optional, so the overhead is only there when it is actually used.
AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement