Jump to content

EventHandler


Bonsai

Recommended Posts

If the event was attached to the element that gets destroyed there is no way the function will be called because the element doesn't exist any more.

Alright, but I was worried about if I have to take care about those handlers, even though the function won't be called anymore, are they still existing somehow, using memory etc. ?

Link to comment

They do use memory since you've attached it to event but the function won't be called because before the event calls a function it gets "validated". If you're building a gamemode that will be ran 24/7 then I'd advice to remove the handler before destroying element.

Link to comment

Ideally, it would make sense if MTA removed all the references, info and actions related to that element but from the scripting side of view, it is you who is responsible to deal with such problems. I have recently heard that setTimer has problems when passing player element as argument. When player leaves and another player joins the server (before the timer finishes) that new player will be used when timer calls the specified function. Why that happens? Because MTA reuses the same address in memory for players (userdata).

Link to comment

Taking care of things instead of leaving them to programmer is one of the purposes of high-level programming languages. So it doesn't feel right when the program that uses Lua for scripting doesn't do that.

Programmer is responsible to make sure that invalid arguments are not passed to the functions. But failure to do so doesn't crash MTA - instead, an error or warning message is produced.

Similarly, it should also take care of other programmer errors it is capable of detecting. I don't even see the destruction of element without removing the event handlers as a mistake. I see event handlers as a part of element they're attached to, similar to element data. When element is destroyed, everything that's part of it should be destroyed.

As for reuse of element identifiers, I wrote about it a few days ago in this forum. Failure to remove all references to element upon its destruction is definitely a programmer's mistake. However, it is possible for a host program that uses Lua scripting to avoid the reuse of element identifiers and reduce the negative consequences of that mistake. My own program that uses Lua for scripting uses full userdata wrappers (whose lifetime is managed by Lua) to point to data allocated in C. When C data is destroyed, pointer in the wrapper is set to NULL to indicate nonexsistence of that data. As a result, no data with a particular identifier will ever get created until the last reference to this identifier is destroyed. Therefore failure to clean up the variables might not be different from memory leak (data structures allow access of those values but algorithms might not do anything about it), while in MTA it affects newly created elements and causes incorrect results to be silently produced.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...