Memory leak in ComObjQuery's documentation page Topic is solved

Share your ideas as to how the documentation can be improved.
iPhilip
Posts: 815
Joined: 02 Oct 2013, 12:21

Memory leak in ComObjQuery's documentation page

Post by iPhilip » 26 Jan 2021, 18:51

I would like to suggest a change to Example #1 on ComObjQuery's documentation page to address what I believe is a memory leak. To test the leak I used the following script and monitored the process' Private Bytes according to the "Using Performance Monitor to Find a User-Mode Memory Leak" note on Microsoft's Windows Debugging Tools page.

Code: Select all

#NoEnv

F3::
obj := ComObjCreate("Scripting.Dictionary")

IID_IProvideClassInfo := "{B196B283-BAB4-101A-B69C-00AA00341D07}"

; Request a pointer to the object's IProvideClassInfo interface.
if !(pci := ComObjQuery(obj, IID_IProvideClassInfo))
{
	MsgBox IProvideClassInfo interface not supported.
	return
}

Loop
{
	; Call GetClassInfo to retrieve a pointer to the ITypeInfo interface.
	DllCall(vtable(pci, 3), "ptr", pci, "ptr*", ti)

	; Call GetDocumentation to get the object's full type name.
	DllCall(vtable(ti, 12), "ptr", ti, "int", -1, "ptr*", name, "ptr", 0, "ptr", 0, "ptr", 0)
	
	; Convert the BSTR pointer to a usable string.
	name := StrGet(name, "UTF-16")

	; Release raw interface pointers.
	ObjRelease(ti)
}
ObjRelease(pci)

; Display the type name!
MsgBox % "Class name: " name

Esc::ExitApp

vtable(ptr, n) {
    ; NumGet(ptr+0) returns the address of the object's virtual function
    ; table (vtable for short). The remainder of the expression retrieves
    ; the address of the nth function's address from the vtable.
    return NumGet(NumGet(ptr+0), n*A_PtrSize)
}
The resulting graph is as follows:
Without SysFreeString.gif
Without SysFreeString.gif (17.98 KiB) Viewed 998 times
The documentation on the ITypeInfo::GetDocumentation method recommends that SysFreeString be used to free the BSTR referenced by name. With that, the resulting graph shows no memory leak:
With SysFreeString.gif
With SysFreeString.gif (17.6 KiB) Viewed 998 times
Thus, I would recommend the following version of the example:

Code: Select all

obj := ComObjCreate("Scripting.Dictionary")

MsgBox % "Interface name: " ComObjType(obj, "name")

IID_IProvideClassInfo := "{B196B283-BAB4-101A-B69C-00AA00341D07}"

; Request a pointer to the object's IProvideClassInfo interface.
if !(pci := ComObjQuery(obj, IID_IProvideClassInfo))
{
    MsgBox IProvideClassInfo interface not supported.
    return
}

; Call GetClassInfo to retrieve a pointer to the ITypeInfo interface.
DllCall(vtable(pci, 3), "ptr", pci, "ptr*", ti)

; Call GetDocumentation to get the object's full type name.
DllCall(vtable(ti, 12), "ptr", ti, "int", -1, "ptr*", pname, "ptr", 0, "ptr", 0, "ptr", 0)

; Convert the BSTR pointer to a usable string.
name := StrGet(pname, "UTF-16")

; Free the BSTR referenced by pname
DllCall("OleAut32\SysFreeString", "ptr", pname)

; Release raw interface pointers.
ObjRelease(ti)
ObjRelease(pci)

; Display the type name!
MsgBox % "Class name: " name

vtable(ptr, n) {
    ; NumGet(ptr+0) returns the address of the object's virtual function
    ; table (vtable for short). The remainder of the expression retrieves
    ; the address of the nth function's address from the vtable.
    return NumGet(NumGet(ptr+0), n*A_PtrSize)
}
Cheers!

- iPhilip
P.S.: This is a reposting of this post.
Windows 10 Pro (64 bit) - AutoHotkey v2.0+ (Unicode 64-bit)

User avatar
Ragnar
Posts: 613
Joined: 30 Sep 2013, 15:25

Re: Memory leak in ComObjQuery's documentation page  Topic is solved

Post by Ragnar » 07 Feb 2021, 10:27

Thanks for the detailed analysis. The change is included in PR #464.

iPhilip
Posts: 815
Joined: 02 Oct 2013, 12:21

Re: Memory leak in ComObjQuery's documentation page

Post by iPhilip » 07 Feb 2021, 15:05

Hi @Ragnar, You are welcome. Happy to contribute. :)
Windows 10 Pro (64 bit) - AutoHotkey v2.0+ (Unicode 64-bit)

iPhilip
Posts: 815
Joined: 02 Oct 2013, 12:21

Re: Memory leak in ComObjQuery's documentation page

Post by iPhilip » 08 Feb 2021, 14:51

Hi @Ragnar, fyi, I noticed that the same example under the v2 documentation page was implemented correctly.
Windows 10 Pro (64 bit) - AutoHotkey v2.0+ (Unicode 64-bit)

User avatar
Ragnar
Posts: 613
Joined: 30 Sep 2013, 15:25

Re: Memory leak in ComObjQuery's documentation page

Post by Ragnar » 08 Feb 2021, 15:42

Yes, I know, I've noticed that too.

Post Reply

Return to “Suggestions on Documentation Improvements”