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

register_forward


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
PartialCloning
Senior Member
Join Date: Dec 2015
Old 10-25-2016 , 07:50   register_forward
Reply With Quote #1

Does register_forward register the forward multiple times if it's already registered under the same function?

HTML Code:
gForwardHandle = register_forward(FM_EmitSound , "EmitSound");
If I call that line twice is the same old handle returned by the forward or would this cause memory leaks assuming I call that line multiple times before unregistering the forward?
PartialCloning is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 10-25-2016 , 08:35   Re: register_forward
Reply With Quote #2

It's all fine, you can register the same one multiple times in one plugin.
klippy is offline
PartialCloning
Senior Member
Join Date: Dec 2015
Old 10-25-2016 , 09:42   Re: register_forward
Reply With Quote #3

Are you sure it's all fine?

Code:
gForwardHandle = register_forward(FM_EmitSound , "EmitSound"); client_print(0, print_chat, "Handle: %d.", gForwardHandle); gForwardHandle = register_forward(FM_EmitSound , "EmitSound"); client_print(0, print_chat, "Handle: %d.", gForwardHandle); gForwardHandle = register_forward(FM_EmitSound , "EmitSound"); client_print(0, print_chat, "Handle: %d.", gForwardHandle);

Quote:
Handle: 595.
Handle: 597.
Handle: 599.
PartialCloning is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 10-25-2016 , 09:48   Re: register_forward
Reply With Quote #4

Yes, it will be a different handle every time, that's normal. What I meant by "it's all fine" is that you can register the same forward as many times as you want. If you register it let's say two times for the same callback, the callback will be called two times.
klippy is offline
PartialCloning
Senior Member
Join Date: Dec 2015
Old 10-25-2016 , 09:52   Re: register_forward
Reply With Quote #5

So to avoid that I should check if my handle is valid before registering the forward. Does unregister_forward set the handle to 0 or do I have to do that after unregistering the forward?
PartialCloning is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 10-25-2016 , 10:44   Re: register_forward
Reply With Quote #6

It can't set it to 0 because, as you can see just by looking at the native prototype, it doesn't pass it by reference. So you have to keep track of your handle manually.
Also, by looking at the code, register_forward() return value doesn't have to be unique - it seems that for every function you can hook there is a vector of single plugin forwards. That means that multiple of these handles can have the same value. Anything < 0 is surely an invalid handle.

Last edited by klippy; 10-25-2016 at 10:45.
klippy is offline
aron9forever
Veteran Member
Join Date: Feb 2013
Location: Rromania
Old 11-01-2017 , 07:55   Re: register_forward
Reply With Quote #7

Quote:
Originally Posted by KliPPy View Post
Yes, it will be a different handle every time, that's normal. What I meant by "it's all fine" is that you can register the same forward as many times as you want. If you register it let's say two times for the same callback, the callback will be called two times.
sorry for reviving the old thread, but the main question was not answered and I have the same problem

I'm talking about the scenario where g_handle = register_forward() is called twice, then unregister_forward() is called only once and obviously on the latest handle, since the first one was overwritten and is now lost

what if this happens often, like, every round?

I'd just like to know if there is a point in complicating the code to check for handler and invalidating cache when unregistering, or if I can just leave it like this. To be honest my intuition would say it's not fine, but I'd like to be sure
__________________
Meanwhile, in 2050:
Quote:
Originally Posted by aron9forever
useless small optimizations
Quote:
Originally Posted by Black Rose View Post
On a map that is 512x512x128 units you end up with 3,355,443,200,000 different "positions". To store each one of those positions individually in the variable "user_or" you need 12 terabytes of memory.

Last edited by aron9forever; 11-01-2017 at 08:20.
aron9forever is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 11-01-2017 , 09:40   Re: register_forward
Reply With Quote #8

Don't register the forward twice. It should require only trivial code to prevent this.
__________________
fysiks is offline
CrazY.
Veteran Member
Join Date: May 2015
Location: SP, Brazil
Old 11-01-2017 , 15:51   Re: register_forward
Reply With Quote #9

Why do you necessarily need to register multiple forwards in the same callback?
__________________








CrazY. is offline
KiLLeR.
Senior Member
Join Date: Jul 2014
Location: Bulgaria
Old 11-01-2017 , 20:20   Re: register_forward
Reply With Quote #10

Quote:
Originally Posted by aron9forever View Post
what if this happens often, like, every round?
The only thing, that can happen will be too many registered forwards somewhere in the memory...
Probably, if this was expensive operation, will cause memory leaks.
KiLLeR. 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 23:48.


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