Question regarding --> DllCall("DeleteObject", "Ptr", hBM)

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
GEOVAN
Posts: 183
Joined: 03 Mar 2022, 11:12

Question regarding --> DllCall("DeleteObject", "Ptr", hBM)

08 May 2024, 12:35

Hello,
Question regarding --> DllCall("DeleteObject", "Ptr", hBM) ; Clean-up

Please let me ask:
If i use the --> DllCall("DeleteObject", "Ptr", hBM) ; Clean-up --> add the very end of a function AFTER return , for example:

Example 1:

Code: Select all

GDI_CaptureScreen( X, Y, W, H, ByRef Checksum="" )  {
 tDC := DllCall( "CreateCompatibleDC", UInt,0 )
 hBM := DllCall( "CopyImage", UInt,DllCall( "CreateBitmap", Int,W, Int,H, UInt,1, UInt,24
                            , UInt,0 ), UInt,0, Int,0, Int,0, UInt,0x2008, UInt )
 oBM := DllCall( "SelectObject", UInt,tDC, UInt,hBM ), hDC := DllCall( "GetDC", UInt,0 )
 DllCall( "BitBlt"
     , UInt,tDC, UInt,0, UInt,0, Int,W, Int,H, UInt,hDC, UInt,X, UInt,Y, UInt,0x00CC0020 )
 DllCall( "ReleaseDC", UInt,0, UInt,hDC ),   DllCall( "SelectObject", UInt,tDC, UInt,oBM )
 If ( Checksum <> "" )
   VarSetCapacity( BM,24,0 ), DllCall( "GetObject", UInt,hBM, UInt,24, UInt,&BM )
 , DllCall( "shlwapi\HashData", UInt,NumGet(BM,20), UInt,NumGet( BM,12 )*NumGet( BM,8 )
                              , Int64P,Checksum, UInt,7 )
 Return hBM, DllCall( "DeleteDC", UInt,tDC )
DllCall("DeleteObject", "Ptr", hBM)  ; Clean-up
}
Example 2:

Code: Select all

GDI_SaveBitmap( hBM, File )  {
 DllCall( "GetObject", Int,hBM, Int,VarSetCapacity($,84), UInt,NumPut(0,$,40,"Short")-42 )
 Numput( VarSetCapacity(BFH,14,0)+40, Numput((NumGet($,44)+54),Numput(0x4D42,BFH)-2)+4 )
 If ( hF := DllCall( "CreateFile", Str,File,UInt,2**30,UInt,2,Int,0,UInt,2,Int64,0 ) ) > 0
   DllCall( "WriteFile", UInt,hF, UInt,&BFH,  UInt,14, IntP,0,Int,0 ) ; BITMAPFILEHEADER
 , DllCall( "WriteFile", UInt,hF, UInt,&$+24, UInt,40, IntP,0,Int,0 ) ; BITMAPINFOHEADER
 , DllCall( "WriteFile", UInt,hF, UInt,NumGet($,20), UInt,NumGet($,44), UIntP,BW, Int,0 )
 , DllCall( "CloseHandle", UInt,hF )
Return BW ? 54+BW : 0
DllCall("DeleteObject", "Ptr", hBM)  ; Clean-up
}
The question is:
If i use the --> DllCall("DeleteObject", "Ptr", hBM) ; Clean-up --> add the very end of a function AFTER return (as in the above 2 examples), is it OK programmatically, please?
RussF
Posts: 1296
Joined: 05 Aug 2021, 06:36

Re: Question regarding --> DllCall("DeleteObject", "Ptr", hBM)

08 May 2024, 12:40

Anything after a Return will not be executed.

Russ
GEOVAN
Posts: 183
Joined: 03 Mar 2022, 11:12

Re: Question regarding --> DllCall("DeleteObject", "Ptr", hBM)

08 May 2024, 12:49

Thank you, i did not know that regarding inside a function, thanks.
So, where i can locate the --> DllCall("DeleteObject", "Ptr", hBM) inside the 2 example functions ?
If i locate it JUST BEFORE the RETURN line, it may not work as expected due to the previous IF ?
Please i will appreciate your help.
RussF
Posts: 1296
Joined: 05 Aug 2021, 06:36

Re: Question regarding --> DllCall("DeleteObject", "Ptr", hBM)

08 May 2024, 13:35

In your first example, you are returning hBM from the function call, so you probably don't want to delete it before you return it. hBM is one of the arguments in the second example - I can't say what the overall effect of deleting it to the system would be.

I guess my question is why? Are you experiencing a memory leak?

Russ
GEOVAN
Posts: 183
Joined: 03 Mar 2022, 11:12

Re: Question regarding --> DllCall("DeleteObject", "Ptr", hBM)

08 May 2024, 13:51

Thank you, you are right, may be is not needed at all to add the line --> DllCall("DeleteObject", "Ptr", hBM)

But if i want to add it, i can do it NOT inside the functions, but after i call the function, for example:

Code: Select all

hBM := GDI_CaptureScreen( 0, 0, A_ScreenWidth, A_ScreenHeight )
GDI_SaveBitmap( hBM, A_ScriptDir . "\Temp_ScreenShot_Files\Temp_Screenshot.bmp" )
DllCall("DeleteObject", "Ptr", hBM)  ; Clean-up
Questions:
(a) Is this the correct method to do it, please?
(b) By the way, let me ask for educational knowladge, is DllCall("DeleteObject", "Ptr", hBM) EQUIVALENT with hBM :="" ?
RussF
Posts: 1296
Joined: 05 Aug 2021, 06:36

Re: Question regarding --> DllCall("DeleteObject", "Ptr", hBM)

08 May 2024, 14:09

1) I would say that once you are done with it, you can delete it.

2) See Freeing Objects. Either way should work.

Russ
ntepa
Posts: 434
Joined: 19 Oct 2022, 20:52

Re: Question regarding --> DllCall("DeleteObject", "Ptr", hBM)

08 May 2024, 15:03

DllCall("DeleteObject", "Ptr", hBM) is not the same as hBM := "". To make AutoHotkey free the object automatically, you can wrap the handle in an AutoHotkey object:

Code: Select all

hBM := GDI_CaptureScreen( 0, 0, A_ScreenWidth, A_ScreenHeight )
hBM := new HBitmap(hBM)
GDI_SaveBitmap( hBM, A_ScriptDir . "\Temp_ScreenShot_Files\Temp_Screenshot.bmp" )
hBM := ""

class HBitmap {
    __New(hbm) {
        this.hbm := hbm
    }
    ; Automatically called when the last reference to this object is freed.
    __Delete() {
        DllCall("DeleteObject", "Ptr", this.hbm)
    }
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot] and 118 guests