Memory leak! Gdip library Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Gnstuur
Posts: 11
Joined: 08 Aug 2021, 11:00

Memory leak! Gdip library

Post by Gnstuur » 08 Aug 2022, 07:05

Hey,
This is leaking memory, but i dont know how to fix it. Any help? I am scanning pixel x539 y182 of a window every 150-300ms for its color, and RAM just goes up every second in my ahk script until 2gb and my pc crashes :D

Code: Select all

#NoEnv
#include Gdip_All.ahk ;https://www.autohotkey.com/board/topic/29449-gdi-standard-library-145-by-tic/
SendMode Input
SetWorkingDir %A_ScriptDir%

global AhkColorPos := [539, 182]
global hwnd

OnExit("CloseGdip")

if (!Token := Gdip_Startup())
{
    MsgBox, 48, Error!, Gdiplus failed to start. Please ensure you have Gdiplus on your system
    ExitApp
}

if (!hwnd := WinExist("RuneLite"))
{
	MsgBox, 48, Error!, RuneLite not running!
    ExitApp
}

Random r, 150, 300
SetTimer, MainLoop, %r%

MainLoop()
{
	CoordMode Mouse, Window

	switch GetAhkColorState()
	{
	
	}
}
return

F1::Pause
return

F12::Reload
return

GetAhkColorState()
{
	Bitmap := Gdip_BitmapFromHWND(hwnd)
	Color := Gdip_GetPixel(Bitmap, AhkColorPos[1], AhkColorPos[2]) & 0xFFFFFF
	SetFormat, integer, hex
	Color +=0
	return %Color%
}

CloseGdip()
{
	Gdip_DisposeImage(pBitmap)
	Gdip_Shutdown(pToken)
}

User avatar
boiler
Posts: 16902
Joined: 21 Dec 2014, 02:44

Re: Memory leak! Gdip library  Topic is solved

Post by boiler » 08 Aug 2022, 07:18

It's pretty straightforward. You keep making bitmap images and you're not disposing of them. Use Gdip_DisposeImage() right after you get the color from it.

By the way, your CloseGdip() function not only does not take variable scope into account, even if those variables were made global, they don't appear anywhere else in your script, so it is also not disposing/closing anything.

Post Reply

Return to “Ask for Help (v1)”