Anki crashes while using custom AutoHotKey script

I am using an AutoHotKey script to change the case of selected text. It works as follows:

  1. Select some text.
  2. Press a keyboard shortcut (Win + CapsLk)
  3. Select the desired option.
  4. The case is changed.

This script works fine in all apps I have tried, including Notepad, MS Word, Chrome, etc.

However, just after pressing the keyboard shortcut, Anki .56 crashes. In Anki .54, the following error message is shown:

Error
An error occurred. Please use Tools > Check Database to see if that fixes the problem.
If problems persist, please report the problem on our support site. Please copy and paste the information below into your report.
Anki 2.1.54 (b6a7760c) Python 3.9.7 Qt 6.3.1 PyQt 6.3.1
Platform: Windows 10
Flags: frz=True ao=False sv=3
Add-ons, last update check: 2023-01-11 19:07:16

Caught exception:
Traceback (most recent call last):
File “aqt.progress”, line 118, in handler
File “aqt.editor”, line 1349, in
RuntimeError: wrapped C/C++ object of type QMimeData has been deleted

Note, however, that the error doesn’t occur every time while using the script and I am unable to determine exactly what causes the error.

In case this issue can’t be resolved in the next release (.57), please change the behavior to showing the error message (as in .54) instead of crashing (as in .56). This is because a crash not only forces me to open the app again unnecessarily, but it also causes the loss of unsaved text I am editing in the Add note window.

Can you share the script?

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
#SingleInstance Force
SetTitleMatchMode 2
SetCapsLockState, AlwaysOff

Menu, Case, Add, &UPPERCASE, CCase
Menu, Case, Add, &lowercase, CCase
Menu, Case, Add, &Title Case, CCase
Menu, Case, Add, &Sentence case, CCase
Menu, Case, Add ; Add a separator line.
Menu, Case, Add, &Fix Linebreaks, CCase
Menu, Case, Add, &reverse, ccase

#CapsLock:: ; Win+capslock to show text case change menu 
GetText(TempText)
If NOT ERRORLEVEL
   Menu, Case, Show
Return

CCase:
If (A_ThisMenuItemPos = 1)
   StringUpper, TempText, TempText
Else If (A_ThisMenuItemPos = 2)
   StringLower, TempText, TempText
Else If (A_ThisMenuItemPos = 3)
   StringLower, TempText, TempText, T
Else If (A_ThisMenuItemPos = 4)
{
   StringLower, TempText, TempText
   TempText := RegExReplace(TempText, "((?:^|[.!?]\s+)[a-z])", "$u1")
} ;Seperator, no 5
Else If (A_ThisMenuItemPos = 6)
{
   TempText := RegExReplace(TempText, "\R", "`r`n")
}
Else If (A_ThisMenuItemPos = 7)
{
   Temp2 =
   StringReplace, TempText, TempText, `r`n, % Chr(29), All
   Loop Parse, TempText
      Temp2 := A_LoopField . Temp2
   StringReplace, TempText, Temp2, % Chr(29), `r`n, All
}
PutText(TempText)
Return

; Copies the selected text to a variable while preserving the clipboard.
GetText(ByRef MyText = "")
{
   SavedClip := ClipboardAll ; Store full version of Clipboard
   Clipboard = ; Empty the clipboard
   Send ^c
   ClipWait 0.5
   If ERRORLEVEL
   {
      Clipboard := SavedClip ; Restore Clipboard
      MyText =
      MsgBox, No text was sent to clipboard
      Return
   }
   MyText := Clipboard
   Clipboard := SavedClip
   Return MyText
}

; Pastes text from a variable while preserving the clipboard.
PutText(MyText)
{
   SavedClip := ClipboardAll 
   Clipboard =              ; For better compatability
   Sleep 20                 ; with Clipboard History
   Clipboard := MyText
   Send ^v
   Sleep 100
   Clipboard := SavedClip
   Return
}

I can reproduce it reliably. I have no idea what’s causing it.

I’m guessing the issue lies in _flagAnkiText()

I was able to fix it by adding Sleep 10 at line 64. So, the end of GetText() looks like the following.

   MyText := Clipboard
   Sleep 10
   Clipboard := SavedClip
   Return MyText
}

@zrk Can you explain how does this fix the issue? (Note: I have not yet checked if it fixes the issue for me.)

After all, the text has already been copied from Anki into the clipboard and the sleep function only affects the interaction between AHK and the clipboard.

And why does this issue only affect Anki while other apps like Notepad, MS Word, Chrome, etc. work fine?

I’m not sure why it works, nor why Anki has problems and not other programs.

I took your script and verified the crash. Then I commented out the parts that save and restore the clipboard and it was no longer crashing. I took an educated guess that extracting the data from the clipboard and then overwriting the clipboard was happening too fast. Maybe there’s a race condition that’s making AHK read from and write to the clipboard at the same time and that’s having some weird interaction with Qt or something. I suspected adding a statement between the statements might help, and it did.

I don’t have any explanation beyond that.

1 Like

Ok. Thanks for the suggestion @zrk .
I will try it out when I get to my computer and update here if it resolves the issue.

So, I tested out the suggestion and it seems to work well. Thank you again @zrk.

However, the developers may still want to investigate this issue further as to why this issue occurs only in Anki.

Greetings,
Yes I tend to sprinkle sleep 25 after cut/copy to clipboard. I’d like to go fast, fast, fast but I’ve learned that this is more stable systemwide.
Nice implementation of context menu. I have four case-change script as well but I just uses CTRL-Numpad1/Numpad3/Numpad7/Numpad9. Once in clipboard I can keep changing the case without having to reselect anything.

Also I abort if the cut/copy is empty so I don’t overwrite my text with blank:

; Wait for the clipboard to contain text.
if Clipboard=
{
  RETURN
}
else
{
  ;manipulate text here
  Send ^v
}

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.