Jump to content
Sign in to follow this  
Polyphem

Memory Leak / Going wild after some time Color Picker

Recommended Posts

Polyphem

HiHo Forum,

I put the below code together from scraps I found on the forum. Problems are that

- it seems to leak memory, usage is going up constantly

- if you execute the script for some minutes suddenly the display is going wild (stops if script canceled)

I'm not sure, but I guess not all resources in user32.dll or gdi32.dll freed correctly. Can anyone see where the problem lies?

#include <GUIConstants.au3>
#include <WindowsConstants.au3>
#include <misc.au3>

$dll_user32 = DllOpen("user32.dll")
$dll_gdi32 = DllOpen("gdi32.dll")

Global $GUI_Pixel[400]
$MagWidth = 200
$MagHeight = 200
$MagZoom = 10

Global $GUI = GUICreate("Preview",$MagWidth,$MagWidth,@DesktopWidth-300,120,$WS_POPUP,$WS_EX_TOOLWINDOW+$WS_EX_TOPMOST)
Global $LastPos[2] = [0,0]
Global $DeskHDC, $GUIHDC

GUICtrlCreateLabel("",0,0,200,200,Default,$GUI_WS_EX_PARENTDRAG)
GUICtrlSetColor(-1,0x7F7F7F)
$label = GUICtrlCreateLabel("",2,2,196,196,Default,$GUI_WS_EX_PARENTDRAG)
GUICtrlSetTip(-1, "Press ""F2"" To Get RGB Code" & @LF &"Press ""F3"" To Get Hexcode"& @lf & @lf & "Click label to drag window" & @lf & "Press ""ESC"" to exit","ColorPicker",1,1)
$label2 = GUICtrlCreateLabel("",81,90,45,15)

GUISetState(@SW_SHOW)

    #region <Making the Label>
    $MousePos = MouseGetPos()
    GUICtrlSetBkColor($label,PixelGetColor($MousePos[0],$MousePos[1]))
        If Dec(PixelGetColor($mousePos[0],$MousePos[1])) < Dec(0x7F7F7F) Then
    GUICtrlSetData($label2,StringTrimLeft(Hex(PixelGetColor($MousePos[0],$MousePos[1])),2))
    GUICtrlSetColor(-1,0xffFFFF)
    GuiCtrlSetBkColor($label2,$GUI_BKCOLOR_TRANSPARENT)
        Else
    GUICtrlSetData($label2,StringTrimLeft(Hex(PixelGetColor($MousePos[0],$MousePos[1])),2))
    GUICtrlSetColor(-1,0x000000)
    GuiCtrlSetBkColor($label2,$GUI_BKCOLOR_TRANSPARENT)
        EndIf
    #endregion

$guipos = WinGetPos($gui)
Global $GUIzoom = GUICreate ("Zoom x2 Au3",$MagWidth,$MagHeight,0,0,$WS_POPUP+$WS_BORDER,BitOR($WS_EX_TOPMOST,$WS_EX_MDICHILD),$gui)
WinMove($GUIzoom,"",$guipos[0],$guipos[1]+210)
GUISetState(@SW_SHOW)

$begin = TimerInit()
$fadetooltip = 0

While 1
    
   $msg = GUIGetMsg()

    if TimerDiff($begin) > 2000 AND $fadetooltip = 1 Then
        ToolTip("")
        $fadetooltip = 0
    EndIf

    If $msg = $GUI_EVENT_CLOSE Then
        ExitLoop
    EndIf

    MAG()

    $MousePos = MouseGetPos()

    If ($LastPos[0] <> $MousePos[0] Or $LastPos[1] <> $MousePos[1]) Then
        $LastPos[0] = $MousePos[0]
        $LastPos[1] = $MousePos[1]
    #region <Making the Label>
    $MousePos = MouseGetPos()
    GUICtrlSetBkColor($label,PixelGetColor($MousePos[0],$MousePos[1]))
        If Dec(PixelGetColor($mousePos[0],$MousePos[1])) < Dec(0x7F7F7F) Then
    GUICtrlSetData($label2,StringTrimLeft(Hex(PixelGetColor($MousePos[0],$MousePos[1])),2))
    GUICtrlSetColor(-1,0xffFFFF)
    GuiCtrlSetBkColor($label2,$GUI_BKCOLOR_TRANSPARENT)
        Else
    GUICtrlSetData($label2,StringTrimLeft(Hex(PixelGetColor($MousePos[0],$MousePos[1])),2))
    GUICtrlSetColor(-1,0x000000)
    GuiCtrlSetBkColor($label2,$GUI_BKCOLOR_TRANSPARENT)
        EndIf
    #endregion
    EndIf
    
    Sleep(10)
    
WEnd

Func MAG()
    $DeskHDC = DLLCall("user32.dll","int","GetDC","hwnd",0)
    $GUIHDC = DLLCall("user32.dll","int","GetDC","hwnd",$GUIzoom)
    If Not @error Then
        DLLCall("gdi32.dll", "int", "StretchBlt", "int", $GUIHDC[0], "int", _
            0, "int", 0, "int", $MagWidth * $MagZoom, "int", $MagHeight * $MagZoom, "int", $DeskHDC[0], "int", _
            MouseGetPos (0) - $MagWidth / $MagZoom / 2, "int", MouseGetPos (1) - $MagHeight / $MagZoom  / 2, "int", $MagWidth,"int", $MagHeight, _
            "long", $SRCCOPY)
        DLLCall("user32.dll","int","ReleaseDC","int",$DeskHDC[0],"hwnd",0)
        DLLCall("user32.dll","int","ReleaseDC","int",$GUIHDC[0],"hwnd",$GUIzoom)
    EndIf
EndFunc

Func OnAutoItExit()
    DllClose($dll_user32)
    DllClose($dll_gdi32)
    Exit
EndFunc

Best Regards

Poly


This post will be edited again by Polyphem: Tomorrow, 11:55 AM

Share this post


Link to post
Share on other sites
smashly

Hi, you have the handles the wrong way around in your ReleaseDC, should be:

DLLCall("user32.dll", "int", "ReleaseDC", "hwnd", 0, "hwnd", $DeskHDC[0])
DLLCall("user32.dll", "int", "ReleaseDC", "hwnd", $GUIzoom, "hwnd", $GUIHDC[0])

Cheers

Share this post


Link to post
Share on other sites
Polyphem

Perfect :D, seems to have fixed both issues... thanks a lot Smashly (again ;) )


This post will be edited again by Polyphem: Tomorrow, 11:55 AM

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.