Doesn't this class create a dangling pointer? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
SAbboushi
Posts: 252
Joined: 08 Dec 2014, 22:13

Doesn't this class create a dangling pointer?

Post by SAbboushi » 22 Feb 2021, 17:55

Code: Select all

/*
    I'm trying to update Elgin's TypeLib2AHK https://www.autohotkey.com/boards/viewtopic.php?f=6&t=36025

    Amazing piece of code for someone like me needing to learn about UIAutomation.

    Wondering if this class constructor can result in a dangling pointer?  I failed to figure out how I can test for that...
*/

#Persistent
point:=new tagPOINT()
ListVars

class tagPOINT
{
	__New(byref p="empty")
	{
		If (p="empty")
		{
			VarSetCapacity(p,this.__SizeOf(),0)
		}
		ObjInsert(this, "__Value", &p) ; <-- when a parameter is not passed (i.e. p="empty"), isn't this pointing to a string buffer for local variable "p" whose memory will be released after __NEW() returns?  i.e. doesn't this result in a dangling pointer?
	}
}
re: @Elgin's TypeLib2AHK
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Doesn't this class create a dangling pointer?  Topic is solved

Post by swagfag » 22 Feb 2021, 19:40

yeah, ud have to replace VarSetCapacity with any of the XXXAlloc memory management functions. accessing .__Value after the constructor has returned is undefined behavior
or if ure porting to v2, BufferAlloc doesnt suffer from that issue
SAbboushi
Posts: 252
Joined: 08 Dec 2014, 22:13

Re: Doesn't this class create a dangling pointer?

Post by SAbboushi » 22 Feb 2021, 20:25

Thanks for the confirmation
swagfag wrote:
22 Feb 2021, 19:40
... or if ure porting to v2, BufferAlloc doesnt suffer from that issue
Yes - trying to update for V2.
Post Reply

Return to “Ask for Help (v1)”