Skip to content

Conversation

@llinfeng
Copy link

@llinfeng llinfeng commented Dec 3, 2025

Fix initialization crash by adding COM retry logic and pointer safety checks

When VD.ahk is loaded as part of my main AHK script at user logon, initialization occasionally runs before Windows has fully registered the virtual-desktop COM services. This caused early-boot failures and null COM pointers downstream.

What was happening

Two issues surfaced during early-boot initialization:

  1. this._initMaxAttempts was accessed before being defined, causing:

    Error: This value of type "Class" has no property named "_initMaxAttempts".

  2. Windows had not yet registered several virtual-desktop COM classes, leading to:

    Error: (0x80040154) Class not registered

    when constructing:

    • {aa509086-5ca9-4c25-8f95-589d3c07b48a}
    • {c2f03a33-21f5-47fa-b4bb-156362a2f239}

    As a result, _init() failed and key COM pointers (e.g., ptr_GetViewForHwnd) remained unset, causing crashes when calling getDesktopNumOfWindow via hotkeys.


What this PR changes

  • Added retry logic around COM initialization to handle the race condition where shell services may not be available immediately after system startup.
  • Added pointer guards to prevent downstream calls from running before initialization is complete.
  • Ensured _initMaxAttempts and related internal members are defined before their first use.

These changes stabilize initialization on boot and prevent hotkey-triggered crashes when COM services are briefly unavailable.

This reflects the added retry/backoff around virtual desktop COM setup, defensive\nchecks for missing interfaces, and the safe fallback in _dll_GetViewForHwnd to\navoid boot-time crashes.
@FuPeiJiang
Copy link
Owner

@llinfeng sorry for the delay,

I was in the process of doing a complete rewrite and wanted to get rid of the old code

I tried to incorporate the polling in order to make sure it's properly initialized

could you test if it's working in this branch ? https://github.com/FuPeiJiang/VD.ahk/blob/alpha/VD.ahk

#requires AutoHotkey v2.1-alpha
    OnMessage(DllCall("RegisterWindowMessageW","WStr","TaskbarCreated","Uint"), (*) => (VD.reinit(), ""))
    VD.reinit()
    if (VD.waiting) { ; block / poll because it may be immediately used, ex: VD.createUntil(3)
        loop 50 { ; 10 seconds
            Sleep 200
            if (!VD.waiting) {
                break
            }
        }
    }

I'll wait for TaskbarCreated
if explorer.exe already exists, then the event TaskbarCreated will never be fired
if explorer.exe doesn't already exist, and TaskbarCreated event is fired within 10 seconds, then nothing bad happens
if 10 seconds pass, then it's undefined behavior

There's probably a better way to get notified of explorer.exe starting than TaskbarCreated, but I'm not aware of it

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

Successfully merging this pull request may close these issues.

2 participants