Bonsai Posted August 6, 2014 Share Posted August 6, 2014 Hey Peeps, just some quick question. Does an EventHandler gets removed if the element it is attached to gets destroyed? Bonsai Link to comment
50p Posted August 6, 2014 Share Posted August 6, 2014 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. Link to comment
Bonsai Posted August 6, 2014 Author Share Posted August 6, 2014 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
Castillo Posted August 6, 2014 Share Posted August 6, 2014 I'm not sure if they use memory or something, but if you think they do, then you could just remove the event before destroying the element. Link to comment
50p Posted August 7, 2014 Share Posted August 7, 2014 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
DiSaMe Posted August 7, 2014 Share Posted August 7, 2014 If they still use memory, isn't that a problem of MTA itself? It doesn't make much sense for MTA to destroy elements without cleaning up the information which will never be used again. Link to comment
50p Posted August 7, 2014 Share Posted August 7, 2014 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
DiSaMe Posted August 7, 2014 Share Posted August 7, 2014 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
MTA Team ccw Posted August 7, 2014 MTA Team Share Posted August 7, 2014 The event data is automatically removed when the element is destroyed. Link to comment
50p Posted August 8, 2014 Share Posted August 8, 2014 I forgot to reply but I've checked the source code and yes, the events are removed as well. Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now