Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Application crashes when state loss is caused by closing a Workbook #3

Open
guwidoe opened this issue May 11, 2024 · 3 comments
Open

Comments

@guwidoe
Copy link

guwidoe commented May 11, 2024

I'm writing this issue for the second time because for some reason the first time it didn't go through when I submitted it.

The application crashes when I set up a state loss call back and then close the workbook containing it by using the top right "x" button.

Sub SetUpStateLossCallBack()
    Static stateTracker As StateLossCallback
    Set stateTracker = New StateLossCallback
    stateTracker.InitByMacroName "TestStateLossCall"
    Debug.Print "Initialized state loss callback"
End Sub

Sub TestStateLossCall()
    Debug.Print "Called back"
    
    Dim testFile As String
    testFile = CreateObject("WScript.Shell").SpecialFolders("Desktop") & "\TestFile.txt"
    Dim fileNum As Long: fileNum = FreeFile()
    
    Open testFile For Output Access Write As #fileNum
        Print #fileNum, "Called back"
    Close #fileNum
End Sub

This is the code I used to verify the callback was never successfully executed.

It works fine if the state loss is caused by the stop button in the VBE.

The crash also happens with my implementation of the release trick implemented in the KeepRecalculating function.

I remember this working in the past, so I wonder if you can reproduce the crash or have any idea why it happens.

@cristianbuse
Copy link
Owner

Yes, it crashes for me also. It definitely worked when I first posted this. Even the IUnknown::Release was working when closing the book. I have no idea why - something must have changed since then.

Interestingly, when I put a breakpoint on DataObject_Clear the code actually stops in debug mode and only then the crash occurs. Not sure if this was happening before.

Anyway, InitByAddress still works without crashing. So, it seems Application.Run is the culprit here.

@thefasquelle
Copy link

My workaround for this issue is to cancel the callback in the Workbook_BeforeClose event as explained in #4

However

Anyway, InitByAddress still works without crashing. So, it seems Application.Run is the culprit here.

On my computer, InitByAddress is very inconsistent. Sometimes it works and sometimes it crashes (not only on workbook close). Whereas InitByMacroName crashes only when workbook is closed, which can be worked around.

@cristianbuse
Copy link
Owner

Thanks @thefasquelle for the feedback!

I would obviously want to find a workaround that does not have a dependency on a Workbook event. However, I've been extremely busy in the last few months and had no time to work on this. Hopefully I can spend some time in the next few weeks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants