Sort() - with custom sort func - invalid read/write - AHK v2 beta.3 Topic is solved

Report problems with documented functionality
User avatar
TheArkive
Posts: 1027
Joined: 05 Aug 2016, 08:06
Location: The Construct
Contact:

Sort() - with custom sort func - invalid read/write - AHK v2 beta.3

08 Dec 2021, 12:04

Code: Select all

#Requires AutoHotkey v2.0-

test := "
(
aa
aaa
bbb
cccc
dddd
eeeee
fff
)"

loop 10
    my_func(test)

my_func(test) {
    test := Sort(test,,_sort)
    msgbox test

    _sort(first, second, offset) { ; sorts by string length (longest string on top)
        If StrLen(first) > StrLen(second)
            return -1
        Else if StrLen(first) < StrLen(second)
            return 1
        Else return 0
    }
}

For me, I get invalid memory read/write error every time on the 3rd iteration. This only happens when using a custom sorting function. If I move the sort function out so it is not a closure func obj, it still happens.

Error Message:

Code: Select all

---------------------------
test.ahk
---------------------------
Critical Error:  Invalid memory read/write.

	Line#
	004: test := "aa
aaa
bbb
cccc
dddd
eeeee
fff"
	017: Loop 10
	018: my_func(test)
	020: {
--->	021: test := Sort(test,,_sort)
	022: msgbox(test)
	024: {
	025: If StrLen(first) > StrLen(second)
	026: Return -1
	027: Else
	027: If StrLen(first) < StrLen(second)
	028: Return 1

The program is now unstable and will exit.
---------------------------
OK   
---------------------------
User avatar
thqby
Posts: 406
Joined: 16 Apr 2021, 11:18
Contact:

Re: Sort() - with custom sort func - invalid read/write - AHK v2 beta.3

06 Jan 2022, 08:17

It looks like the custom sorting function has been released in the source.
You can add ObjPtrAddRef(_sort) before calling sort until lexikos fixes the bug.
User avatar
TheArkive
Posts: 1027
Joined: 05 Aug 2016, 08:06
Location: The Construct
Contact:

Re: Sort() - with custom sort func - invalid read/write - AHK v2 beta.3

06 Jan 2022, 10:26

@thqby

Thanks, and good catch. I would not not have found that.

Return to “Bug Reports”

Who is online

Users browsing this forum: No registered users and 38 guests