AHK doesn't report a Sharing Violation error Topic is solved

Report problems with documented functionality
Jose Hidalgo
Posts: 222
Joined: 07 Mar 2021, 07:44

AHK doesn't report a Sharing Violation error

Post by Jose Hidalgo » 11 May 2021, 18:20

Hi,

Here's the topic where this bug was discovered (mikeyww considers it as a bug, hence this topic) : https://www.autohotkey.com/boards/viewtopic.php?f=76&t=90319
Here's the precise post where this bug was identified : https://www.autohotkey.com/boards/viewtopic.php?p=399236#p399236

How to reproduce it :

Code: Select all

ImgPath := path\to\a\picture
FileVar := FileOpen(ImgPath, "r")
Gui Add, Picture, , % ImgPath
Gui, Show
If we forget a FileVar.close() before the Gui Add command, then the command will fail : the picture won't be displayed, but AHK won't report any error, when there is in fact one, that Visual Studio in debug mode would report :
ERROR_SHARING_VIOLATION
32 (0x20)
The process cannot access the file because it is being used by another process.
We had a bit of trouble finding this one, so I hope it can be corrected in a future AHK release. Thanks in advance ! :)

lexikos
Posts: 9494
Joined: 30 Sep 2013, 04:07
Contact:

Re: AHK doesn't report a Sharing Violation error

Post by lexikos » 12 May 2021, 03:16

The error handling in AutoHotkey v1 is somewhat inconsistent, but the general pattern is not to report errors other than outright invalid parameters. Runtime errors may set ErrorLevel, or just be ignored.

A picture control can be added without a picture, and that is exactly what happens when you specify a value that cannot be added as a picture, regardless of the reason. If you give the control a size, you will see that the value you passed is its text. (The text is always set and can be retrieved by ControlGetText or other methods, but you don't see it if a picture loads.)

The lack of error reporting in this specific case is definitely intentional, as per comments in the AutoHotkey v1.0.24 source code.

Regardless, it is long-standing behaviour that scripts may rely on. For instance, if a runtime error is thrown, any code that has previously been used to detect failure of Gui Add will not be able to execute.

However, it should probably be changed for v2.


CreateFile fails because the sharing mode was specified as 0; it should be FILE_SHARE_READ, which allows it to succeed even if some other process has the file open for reading (but not writing). There is no reason for it to be 0. Under different circumstances this could produce a more difficult to trace, intermittent failure, so it is perhaps lucky that you have revealed this bug.

lexikos
Posts: 9494
Joined: 30 Sep 2013, 04:07
Contact:

Re: AHK doesn't report a Sharing Violation error

Post by lexikos » 12 May 2021, 05:56

Actually, the behaviour is explicitly documented.
If the picture cannot be loaded or displayed (e.g. file not found), the control is left empty and its width and height are set to zero.

Jose Hidalgo
Posts: 222
Joined: 07 Mar 2021, 07:44

Re: AHK doesn't report a Sharing Violation error

Post by Jose Hidalgo » 12 May 2021, 07:33

Thank you lexicos. :) For the record I'm barely an AHK beginner. :oops:
As you can see from the other topic, it took us a long time, with a bunch of knowledgeable people from this forum, to find where that problem was coming from. It was driving me crazy in a 1000 line script ! :crazy:

I hope this gets changed for v2. I guess there are probably two ways to do it, like you just said :
  • Either display an error message which will force coders to properly close files after opening them
  • Or change the sharing mode so the error doesn't occur anymore
Thanks in advance !

joefiesta
Posts: 494
Joined: 24 Jan 2016, 13:54
Location: Pa., USA

Re: AHK doesn't report a Sharing Violation error

Post by joefiesta » 14 May 2021, 08:30

Could someone explain why the author's script includes the FILEOPEN() command?

I add pictures to guis all the time without first opening the file. And, if the picture is already open in IRFANVIEW or PHOTOSHOP, my GUI ADD commands work just fine.

just me
Posts: 9406
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: AHK doesn't report a Sharing Violation error

Post by just me » 14 May 2021, 10:13

It appears that the error only occurs for special image types if AHK is trying to load the image via OLE functions, like .JPG without AltSubmit. File loaded using GDI+ (like .PNG) don't produce the error.
lexikos wrote:CreateFile fails because the sharing mode was specified as 0; it should be FILE_SHARE_READ, ...

lexikos
Posts: 9494
Joined: 30 Sep 2013, 04:07
Contact:

Re: AHK doesn't report a Sharing Violation error

Post by lexikos » 14 May 2021, 23:16

CreateFile is only used for the OLE method. For the other methods, there is no need to open the file directly.

The OP used FileOpen in place of FileExist. See the original topic linked in the first post.

lexikos
Posts: 9494
Joined: 30 Sep 2013, 04:07
Contact:

Re: AHK doesn't report a Sharing Violation error  Topic is solved

Post by lexikos » 28 Aug 2021, 19:36

Fixed by v1.1.33.10.

Post Reply

Return to “Bug Reports”