iseahound / ImagePut

A core library for images in AutoHotkey. Supports AutoHotkey v1 and v2.
https://www.autohotkey.com/boards/viewtopic.php?f=83&t=76633
MIT License
116 stars 24 forks source link

Error after running script for some time #19

Closed jpuerj closed 1 year ago

jpuerj commented 1 year ago

Hello!

First of all, thank you for this project, it's amazing!

I have a script that uses ImagePut to update information about a window every 200ms. The update function is something like this:

updateInfo() { img := ImagePutBuffer("window") this.getInfoX(img) this.getInfoY(img) this.getInfoZ(img) }

The getInfo functions mainly check pixel colors at different locations.

After about 1 hour of running the script I always get this error:

Parameter #1 of File.Prototype.RawRead is invalid. Specifically: 0 Line# 1360: file := FileOpen(image, "r") 1361: hData := DllCall("GlobalAlloc", "uint", 0x2, "uptr", file.length, "ptr") 1362: pData := DllCall("GlobalLock", "ptr", hData, "ptr") ---> 1363: file.RawRead(pData, file.length) 1364: DllCall("GlobalUnlock", "ptr", hData) 1365: file.Close()

iseahound commented 1 year ago

Is there a typo in your copy of ImagePut?

Line 1362 should read GlobalLock, not GlobalAlloc.

See: https://github.com/iseahound/ImagePut/blob/4479b845856638bdde7606697e5ccf78725e612e/ImagePut.ahk#L1362

jpuerj commented 1 year ago

Sorry, that's a typo in the topic, the file is correct! Going to edit it!

iseahound commented 1 year ago

Can you check for a memory leak? It looks like one to me. See how much memory AutoHotkey is using in task manager - it should be stable.

Also can you post the full code? Are you deleting 'img'?

iseahound commented 1 year ago

Also,

ImagePutBuffer("window")

is ambiguous syntax try:

ImagePutBuffer({window: "MyTitle"})

to avoid the bug where a file name has the same name as a window

jpuerj commented 1 year ago

Can you check for a memory leak?

It increased 10MB in the last 3 minutes, I'm going to keep an eye on it. I had discarded this possibility earlier because when I got a leak in the past it would crash very early. EDIT: look's like a leak, it increased 30MB since writting this post.

Are you deleting 'img'?

I'm not deleting image. I thought it was not needed because it's inside a class method.

Also can you post the full code?

Sure: script.zip

I'm going to try deleting 'img' and every related object. I just need img := "", right?

Thanks!

iseahound commented 1 year ago

Well this doesn't look like an ImagePut error.

Try to improve your memory management:

fn(img) {
    filepath := "something.png"
    static search := Imageputbuffer(filepath)
    img.imagesearch(search)
}

Make the search images static so you don't create a million of them. Then I see you're storing the screen capture in an array, you'll need to do arr[123] := "" to delete them. (Don't bother deleting static images they will be cleaned on script exit)

You probably didn't notice, but it will have to hit a few gigs of ram until it crashes like above.

iseahound commented 1 year ago

Add documentation to delete the buffers

Also, add an update() method to Imageputbuffer (for screen capture only)?

jpuerj commented 1 year ago

The problems seems to be with ImageSearch. Whenever the function is called the memory use increase a bit. I tested on this hotkey:

F1:: { static img := ImagePutBuffer("Code") static search := ImagePutBuffer("images/waypoint_1.png") loop { xy := img.ImageSearch(search) Sleep(100) } }

The increase is very small... About 1MB each minute... I think that's why it took more than an hour to crash on my script.

iseahound commented 1 year ago

Fixed by af2bca4d69010a86eb4eb2e20964996f5188071e 44a25c805a326135f2f2b30e4a7d7a420b13e8c9 and 2fbc4fe122ade19e3f6dfc2aad9b256be680e61a.