[v2.0.11] Invalid Memory Read/Write with JsRT.ahk on FreeLibrary call Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
RaptorX
Posts: 390
Joined: 06 Dec 2014, 14:27
Contact:

[v2.0.11] Invalid Memory Read/Write with JsRT.ahk on FreeLibrary call

28 Jan 2024, 09:37

I am wondering if anybody else is experiencing this issue reported by one of the people I work with:

Seems like after the JsRT.ahk library created by @lexikos exits and tries to free the loaded dll, Autohotkey tries to read an invalid location in memory and we end up getting an error on program exit

Code: Select all

; plugin.js file
function filter_data(selected_text)
{
	return [ { text : 'hey man'} ];
}

Code: Select all

; test.ahk file
#SingleInstance

#Include <JsRT>

js_plugin := (JsRT.Edge)()
plugin_code := FileRead(A_ScriptDir "\plugin.js")
try js_plugin.Exec(plugin_code)
catch Error as e
	msgbox e.What " " e.Message

com_matches := js_plugin.filter_data("test test")

matches := []
loop com_matches.length
{
	match := com_matches.%A_Index-1%
	matches.Push({text : "hi"})
}

MsgBox matches.length
Everything works great until after pressing the OK button on the message box and we end up with this error:
Error IMG
Any ideas whether this is an Autohotkey bug or am I doing something wrong here?
Projects:
AHK-ToolKit
User avatar
thqby
Posts: 431
Joined: 16 Apr 2021, 11:18
Contact:

Re: [v2.0.11] Invalid Memory Read/Write with JsRT.ahk on FreeLibrary call

29 Jan 2024, 03:56

com_matches := 0
Perhaps it is necessary to release objects from JSRT before exiting
User avatar
thqby
Posts: 431
Joined: 16 Apr 2021, 11:18
Contact:

Re: [v2.0.11] Invalid Memory Read/Write with JsRT.ahk on FreeLibrary call

29 Jan 2024, 04:15

These objects follow the default release order when the script exits.

Code: Select all

com_matches := unset
js_plugin := unset
matches := unset
User avatar
V2User
Posts: 195
Joined: 30 Apr 2021, 04:04

Re: [v2.0.11] Invalid Memory Read/Write with JsRT.ahk on FreeLibrary call

30 Jan 2024, 03:42

RaptorX wrote:
28 Jan 2024, 09:37
JsRT.ahk library created by @lexikos
May you give a link of this library for me? I would appreciate it.😀
User avatar
RaptorX
Posts: 390
Joined: 06 Dec 2014, 14:27
Contact:

Re: [v2.0.11] Invalid Memory Read/Write with JsRT.ahk on FreeLibrary call

30 Jan 2024, 09:04

thqby wrote:
29 Jan 2024, 04:15
These objects follow the default release order when the script exits.

Code: Select all

com_matches := unset
js_plugin := unset
matches := unset
sadly this doesnt help with the issue:
image.png
image.png (55.83 KiB) Viewed 603 times
Projects:
AHK-ToolKit
User avatar
thqby
Posts: 431
Joined: 16 Apr 2021, 11:18
Contact:

Re: [v2.0.11] Invalid Memory Read/Write with JsRT.ahk on FreeLibrary call

30 Jan 2024, 10:14

The above code simulates the order in which the objects are released when the script exits, you must release them in the correct order to avoid this error.

Code: Select all

com_matches := matches := unset
js_plugin := unset
User avatar
RaptorX
Posts: 390
Joined: 06 Dec 2014, 14:27
Contact:

Re: [v2.0.11] Invalid Memory Read/Write with JsRT.ahk on FreeLibrary call

31 Jan 2024, 07:01

thqby wrote:
30 Jan 2024, 10:14
The above code simulates the order in which the objects are released when the script exits, you must release them in the correct order to avoid this error.

Code: Select all

com_matches := matches := unset
js_plugin := unset
Sadly this makes no difference.
Projects:
AHK-ToolKit
User avatar
thqby
Posts: 431
Joined: 16 Apr 2021, 11:18
Contact:

Re: [v2.0.11] Invalid Memory Read/Write with JsRT.ahk on FreeLibrary call  Topic is solved

31 Jan 2024, 21:15

This is a typo. matches is not an object from JsRT, match is.
lexikos
Posts: 9678
Joined: 30 Sep 2013, 04:07
Contact:

Re: [v2.0.11] Invalid Memory Read/Write with JsRT.ahk on FreeLibrary call

15 Feb 2024, 21:15

It is by design that the main JsRT object's destructor calls FreeLibrary. The program can't know whether there are any objects implemented by the library still referenced by the script, and the library itself probably doesn't keep track of how many of its objects are "alive". Even if it did, I'm not sure that the library could or should prevent itself from being unloaded.

I recall seeing similar problems with ActiveX in old scripts, where it was necessary to destroy the GUI before the script exits (or maybe it was because the script explicitly unloaded the library on exit). Otherwise, the process would crash on exit, because the GUI was destroyed after the ActiveX library was unloaded.

It might have been better to omit the FreeLibrary call and instead load the library exactly once, locking it in for the lifetime of the process. However, since (iirc) it isn't possible to have JScript within a WebBrowser control and JsRT working in the same process, allowing JsRT to be unloaded might provide some flexibility.

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: Idge_24, kunkel321, niCode, wjt936826577 and 33 guests