[v2.0-beta.1] Array.Capacity := 0 Topic is solved

Report problems with documented functionality
just me
Posts: 9453
Joined: 02 Oct 2013, 08:51
Location: Germany

[v2.0-beta.1] Array.Capacity := 0

Post by just me » 16 Aug 2021, 07:51

The maximum number of elements the array should be able to contain before it must be automatically expanded. If setting a value less than Length, elements are removed.

Code: Select all

Arr := Array()
Arr.Capacity := 10
MsgBox(Arr.Length . " - " Arr.Capacity, "1")
Arr.Length := 10
MsgBox(Arr.Length . " - " Arr.Capacity, "2")
Arr.Capacity := 0
MsgBox(Arr.Length . " - " Arr.Capacity, "3")

Code: Select all

---------------------------
M37427717.ahk
---------------------------
Error:  Out of memory.

	Line#
	001: Arr := Array()
	002: Arr.Capacity := 10
	003: MsgBox(Arr.Length . " - " Arr.Capacity, "1")
	004: Arr.Length := 10
	005: MsgBox(Arr.Length . " - " Arr.Capacity, "2")
--->	006: Arr.Capacity := 0
	007: MsgBox(Arr.Length . " - " Arr.Capacity, "3")
	008: Exit

Try to continue anyway?
---------------------------
Ja   Nein   
---------------------------

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: [v2.0-beta.1] Array.Capacity := 0

Post by swagfag » 16 Aug 2021, 08:22

https://github.com/Lexikos/AutoHotkey_L/blob/3808407a7c8871c0e87288a739f7ff317caaab7b/source/script_object.cpp#L1780
realloc msdn wrote:If size is zero, then the block pointed to by memblock is freed; the return value is NULL, and memblock is left pointing at a freed block.
size is zero, which is something Array(and probably others)::SetCapacity() should check for
there's also a double-free because of this(realloc() frees if size was 0, and so does Array's destructor https://github.com/Lexikos/AutoHotkey_L/blob/3808407a7c8871c0e87288a739f7ff317caaab7b/source/script_object.cpp#L1865)

lexikos
Posts: 9583
Joined: 30 Sep 2013, 04:07
Contact:

Re: [v2.0-beta.1] Array.Capacity := 0  Topic is solved

Post by lexikos » 09 Dec 2022, 17:24

Fixed by v2.0-rc.2.

(I forgot it for some time.)

Post Reply

Return to “Bug Reports”