Raised This Month: $32 Target: $400
 8% 

Invalid handle error even with checking if it is not null


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
hh2
AlliedModders Donor
Join Date: Sep 2010
Old 08-20-2018 , 20:29   Invalid handle error even with checking if it is not null
Reply With Quote #1

I'm using BotMimic plugin, but every time when new map is started i always get invalid handle error.
Error:
Code:
[SM] Exception reported: Handle 9b80345 is invalid (error 1)
[SM] Blaming: botmimic.smx
[SM] Call stack trace:
[SM]   [0] CloseHandle
[SM]   [1] Line 285, botmimic.sp::OnMapStart
Line 285 code: (FH_frames is ArrayList)
Code:
if(iFileHeader[FH_frames] != null)
        delete iFileHeader[FH_frames];
Why this error appears, after all it is checked if it is not null.

Here is the full code with selected line (285) where error happens:
https://github.com/peace-maker/botmi...tmimic.sp#L285
hh2 is offline
ddhoward
Veteran Member
Join Date: May 2012
Location: California
Old 08-20-2018 , 20:33   Re: Invalid handle error even with checking if it is not null
Reply With Quote #2

A null check just checks to see if the value of the variable != 0. And indeed, the value of the variable is 9b80345, according to your log.

The issue is probably that somewhere else, something is closing the handle, but not setting the variable to null. So when you run a check on it later... it doesn't == null. There's still a Handle's information in there... but the Handle no longer exists. Either that, or the variable is being filled with garbage data somewhere.

Quote:
The data which your plugin stores in the variable is just a number, a number that happens to correspond to the location in memory of a Sourcemod handle. It's possible (especially accidentally) to hold onto the old variable data, referencing a handle which no longer exists. AFAIK, there is no good way to verify if a handle is still valid, given only a variable that points to said handle. IsValidHandle() exists, but I am not sure of how well it functions, and using it in your plugin will cause a warning to appear upon compilation.

And in fact, the opposite is also true. It is possible to overwrite the data in such a variable, so the variable no longer points to Handle A, but instead Handle B, even while Handle A still exists. If you do not have the data referencing Handle A saved elsewhere, then the data is lost, and you may no longer have a way to remove the Handle. If this happens over and over, this is what is called "handle leaking" and an excessive amount will cause SourceMod to automatically unload your plugin.
__________________

Last edited by ddhoward; 08-20-2018 at 20:39.
ddhoward is offline
hh2
AlliedModders Donor
Join Date: Sep 2010
Old 08-20-2018 , 20:37   Re: Invalid handle error even with checking if it is not null
Reply With Quote #3

So i should search for that variable somewhere else and then set like this?
Code:
delete handle;
handle = null;
hh2 is offline
ddhoward
Veteran Member
Join Date: May 2012
Location: California
Old 08-20-2018 , 20:43   Re: Invalid handle error even with checking if it is not null
Reply With Quote #4

delete already sets the variable to null. Perhaps the variable is being filled with garbage data somewhere?
__________________
ddhoward is offline
Fyren
FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren
Join Date: Feb 2106
Old 08-21-2018 , 00:53   Re: Invalid handle error even with checking if it is not null
Reply With Quote #5

CloseHandle does not set the variable to null, and you are apparently using that in some places.
Fyren is offline
ddhoward
Veteran Member
Join Date: May 2012
Location: California
Old 08-21-2018 , 01:37   Re: Invalid handle error even with checking if it is not null
Reply With Quote #6

Quote:
Originally Posted by Fyren View Post
CloseHandle does not set the variable to null, and you are apparently using that in some places.
CTRL+F CloseHandle shows nothing.

I'm pretty certain that he's using StringMap.GetArray incorrectly? I can't really look into it in detail currently.
__________________
ddhoward is offline
SHUFEN
Senior Member
Join Date: Jun 2014
Location: Japan, Tokyo
Old 08-21-2018 , 11:57   Re: Invalid handle error even with checking if it is not null
Reply With Quote #7

Did you CloseHandle (or delete) after passing the handles to other variable? For example like this,

Code:
ArrayList arr = new ArrayList();
ArrayList arr_ = view_as<ArrayList>(arr);
delete arr_;
if (arr != null)
	delete arr; // This may errors.
SHUFEN is offline
Send a message via Skype™ to SHUFEN
hh2
AlliedModders Donor
Join Date: Sep 2010
Old 08-21-2018 , 14:44   Re: Invalid handle error even with checking if it is not null
Reply With Quote #8

Fyren, this is what sourcemod log, in the code it is delete not CloseHandle.
SHUFEN.jp, look at the code below, i think there is problem with iHeader[FH_frames], but im not sure about this.

One more question: when i assign handle to temporary handle, should i delete both after all operations or just original handle is enough?

Take this code for example (From line 972 in BotMimic). A brief example of what I mean:
Code:
int iHeader[FileHeader];			// initiate new ArrayList here
iHeader[FH_frames] = g_hRecording[client];	// getting copy of arraylist i guess

g_hRecording[client] = null;		// later in the function this variable is set to null, but iHeader[FH_frames] is not, is it valid?

Last edited by hh2; 08-21-2018 at 15:07.
hh2 is offline
Scag
AlliedModders Donor
Join Date: May 2017
Location: Crashing Hale
Old 08-21-2018 , 21:42   Re: Invalid handle error even with checking if it is not null
Reply With Quote #9

Quote:
Originally Posted by asherkin
<asherkin> 1 is HandleError_Changed, which means the variable you called delete on contained a freed handle that had been reused
<asherkin> this could happen despite using delete if you did something like: Handle a = new Thing(); Handle b = a; delete a; delete b;
<asherkin> delete can only null the variable you call it on, not any erroneous by-value copies you made
__________________
Over-engineering is underrated.

GitHub
BTC
ETH

Retired
Scag is offline
ddhoward
Veteran Member
Join Date: May 2012
Location: California
Old 08-21-2018 , 21:45   Re: Invalid handle error even with checking if it is not null
Reply With Quote #10

That's your problem.
__________________
ddhoward is offline
Reply


Thread Tools
Display Modes

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 11:07.


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