Raised This Month: $51 Target: $400
 12% 

Solved The right way to close CreateEvent handle (avoid memory leaks)


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Visual77
Veteran Member
Join Date: Jan 2009
Old 03-15-2018 , 21:07   The right way to close CreateEvent handle (avoid memory leaks)
Reply With Quote #1

Hi. There seems to be a lot of confusion regarding whether you should close handles created by CreateEvent or not. I would like to know the one and only way. The documentation says:

Quote:
The Handle should not be closed via CloseHandle(). It must be closed via
event.Fire() or event.Cancel().
Sourcemod api says:
Quote:
Event.FireToClient Method - Fires a game event to only the specified client. Unlike Fire, this function DOES NOT close the event Handle.
Event.Fire Method - This function closes the event Handle after completing.
So if I'm reading correctly, this would appear right?

Code:
stock void CreateDeathEvent(int victim, int attacker, const char[] sWeapon = "",  bool toAll)
{
	Event event = CreateEvent("player_death");

	if (event != null)
	{
		event.SetInt("userid", GetClientUserId(victim));
		event.SetInt("attacker", GetClientUserId(attacker));
		event.SetString("weapon", sWeapon);

		if (toAll)
		{
			event.Fire(false);

			/* Event.Fire closes the handle automaticlly. I don't need to clear the handle here. */
		}
		else
		{
			if (IsValidClient(victim))
			{
				event.FireToClient(victim); 
			}

			if (IsValidClient(attacker) && attacker != victim) 
			{
				event.FireToClient(attacker);
			}

                        /* The documentation says not to close the handle using CloseHandle which is equivalent to delete handle, so the handle is deleted using what the documentation says. */

                       event.Cancel();  // I've now deleted the handle?  

		}
	}
}

Last edited by Visual77; 03-16-2018 at 08:34.
Visual77 is offline
cravenge
Veteran Member
Join Date: Nov 2015
Location: Chocolate Factory
Old 03-15-2018 , 22:44   Re: The right way to close CreateEvent handle (avoid memory leaks)
Reply With Quote #2

That seems about right.
cravenge is offline
Neuro Toxin
Veteran Member
Join Date: Oct 2013
Location: { closing the void; }
Old 03-16-2018 , 02:53   Re: The right way to close CreateEvent handle (avoid memory leaks)
Reply With Quote #3

Does the event get received when fired to client after cancel is called?

If == true
What you're doing would be the right way.
__________________
Neuro Toxin is offline
asherkin
SourceMod Developer
Join Date: Aug 2009
Location: OnGameFrame()
Old 03-16-2018 , 03:23   Re: The right way to close CreateEvent handle (avoid memory leaks)
Reply With Quote #4

At least since FireToClient was added, CloseHandle/delete is equivalent to Event.Cancel, so either should be good.
__________________
asherkin is offline
Visual77
Veteran Member
Join Date: Jan 2009
Old 03-16-2018 , 04:24   Re: The right way to close CreateEvent handle (avoid memory leaks)
Reply With Quote #5

Quote:
Originally Posted by Neuro Toxin View Post
Does the event get received when fired to client after cancel is called?

If == true
What you're doing would be the right way.
Not had time to try if that is true yet. I looked at so many plugins and most of them left the handle to be stored in memory, so I did the same.

Quote:
Originally Posted by asherkin View Post
At least since FireToClient was added, CloseHandle/delete is equivalent to Event.Cancel, so either should be good.
Thanks, that's good to know.

I have two more question. The wiki says to use the Event.Cancel on events that aren't fired. What scenario makes an event not fire, creating another unexpected leak?

Looking at my code. If I send my DeathEvent to the atacker and the victim only, then the other players on the server won't see the public kill announcment, right?

Last edited by Visual77; 03-16-2018 at 04:28.
Visual77 is offline
cravenge
Veteran Member
Join Date: Nov 2015
Location: Chocolate Factory
Old 03-16-2018 , 07:27   Re: The right way to close CreateEvent handle (avoid memory leaks)
Reply With Quote #6

Quote:
Originally Posted by Visual77 View Post
What scenario makes an event not fire, creating another unexpected leak?
When either one of the players leave, thus invalidating their userid. It could also be when using timers to delay the firing of the event.

Quote:
Originally Posted by Visual77 View Post
then the other players on the server won't see the public kill announcment, right?
Yes. It will only show to the two players involved in the event.

Last edited by cravenge; 03-16-2018 at 07:28.
cravenge is offline
Visual77
Veteran Member
Join Date: Jan 2009
Old 03-16-2018 , 08:34   Re: The right way to close CreateEvent handle (avoid memory leaks)
Reply With Quote #7

Quote:
Originally Posted by cravenge View Post
When either one of the players leave, thus invalidating their userid. It could also be when using timers to delay the firing of the event.
Ok. I'm already validating the client before sending the event, so I shouldn't have to worry.

Quote:
Originally Posted by cravenge View Post
Yes. It will only show to the two players involved in the event.
Great! Thanks guys.
Visual77 is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 10:28.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode