cristianbuse / VBA-StateLossCallback

A class that allows a callback when state is lost
MIT License
12 stars 4 forks source link

Crash + some state losses not detected #1

Closed 6DiegoDiego9 closed 1 year ago

6DiegoDiego9 commented 1 year ago

Hi Cristian First of all thanks for this class! it seems a little gem!

However on my first try I had the following problems: 1) your Demo3 (slc.InitByAddress AddressOf TestX, "Testing") crashes my Excel application just after writing "State was lost. Clean proc X testing" on the console 2) Demo1 and Demo2 seems to work if I fully execute them and press Reset afterward. But if I make a state loss by other means, for example: by pressing Reset while in debugging mode or by pressing Reset while in a loop of doEvents or by adding a "Dim foobar as object" while in debugging mode (causing a state loss) ... it doesn't call the given macro.

cristianbuse commented 1 year ago

Hi @6DiegoDiego9 ,

Thank you for the kind words and for your feedback!

  1. I re-tested Demo3 on all of my machines and it does not crash. I tested on both Windows and Mac on both x32 and x64. I am definitely curious as to why it crashes on your machine. Have you done any changes to the code or did you just ran it as downloaded?
  2. Resetting while debugging produces some weird behaviour like completely disabling debugging. Same for the DoEvents loop. Also, it looks like there is still code running although Reset does not do anything anymore. I will need to investigate this when I have more time but I get the feeling that there is no solution to this issue.
6DiegoDiego9 commented 1 year ago
  1. I tested again with your fresh new demo file and this time it works! I'm not 100% sure I didn't touched it then. Thanks for pushing me to try it again! :)

  2. I remain very much interested in this point. I hope for some good news in future :)

Thanks again!

cristianbuse commented 1 year ago

Hi @6DiegoDiego9 ,

  1. Thanks for re-testing!
  2. Please ignore my previous comment about weird behaviour if resseting while debugging. I tested that without using the class (just a fake virtual table and a module method i.e. as in the Option 3, the "magic" way section of this CR answer). Using the class in this repo does not cause that issue.

However, I am sad to say that while debugging or while code is running (which includes a DoEvents loop), if state is lost then indeed the callbacks are not called, If a form is displayed, then the callbacks still work (unless a DoEvents loop is run from within the form or state is lost while some other code is running).

So, there are 2 cases where this fails:

Any advice or criticism is always welcome, so please do help if you have any ideas on how to improve this or any other of my repos.

Thank you again for your feedback!

With Kind Regards, Cristian

6DiegoDiego9 commented 1 year ago

Hi @cristianbuse

The first reason why I'm interested in a state loss callback is in VBA apps that open or modify external resources, to call a procedure that cleans them on crash. My most important use would be to improve SeleniumVBA by adding a cleaner procedure triggered on crash, that (maybe optionally with a Yes/No) closes the webdriver.exe and browser opened by the script session, as current version is unable to reuse a browser used in a previous script session and anyway we often want to run/test scripts on fresh new browser sessions.

Scenario 1: while I'm building a VBA script to scrape/interact with a website, adding commands locating html elements, occasionally an error is raised (eg. the given xpath doesn't find an element) and I need to VBE "Reset" and run the whole script again to see if everything works until to my last fixed line of code. I'd like VBA-StateLossCallback to close the exe+browser on "Reset".

Scenario 2: my script is scheduled to scape data from a website, daily, until a day the website changes the html of an element and my script raises the error. The user (or me) press "End" to terminate. I'd like VBA-StateLossCallback to close the exe+browser on "End".

As far as I understand, the current VBA-StateLossCallback isn't able to detect these two kind of state losses. Is it correct?

cristianbuse commented 1 year ago

Hi @6DiegoDiego9 ,

As far as I understand, the current VBA-StateLossCallback isn't able to detect these two kind of state losses. Is it correct?

Correct. The reason is that the IUnknown::Release is not called anymore if Reset/End is pressed while code is running. This is somenthing I was not aware until you raised this issue. It's simply how VB works behind the scenes and there's absolutely nothing I can do about it. In a few weeks time I will try to investigate other ways of achieving this but not Release. If I find anything interesting then I will surely write you here.