dotnet/wpf · warning · ElementNotAvailableException

E_OUTOFMEMORY

E_OUTOFMEMORY

Error message

ElementNotAvailableException(e)

What it means

This is WPF UIAutomation's MSAA client-side provider translating a raw COMException from an IAccessible (MSAA) server into a UIA exception. When the OLEACC call returns E_OUTOFMEMORY, the provider throws ElementNotAvailableException because, in practice, some OLEACC proxies (notably the treeview proxy) return out-of-memory for non-critical reasons — e.g. the target HWND no longer exists and GetWindowThreadProcessID fails, so the proxy cannot allocate shared memory in the target process and wrongly assumes OOM. It means the UIA element wrapped around that IAccessible is no longer usable, not that the machine is actually out of memory.

Solutions

  1. Treat ElementNotAvailableException as expected during UI automation: catch it and re-query the AutomationElement tree (FindAll/FindFirst) instead of reusing the stale element.
  2. Check that the target application/window is still alive (IsOffscreen / element.Current via TryGetCurrentPattern) before reading properties; re-resolve elements from the root after window close.
  3. Wrap property/pattern reads in a helper that swallows ElementNotAvailableException and returns a sentinel so tree walks do not abort.
  4. If it happens against a specific legacy control without the window dying, verify the control's native proxy (oleacc) and consider switching the app to expose a real UIA provider (UIAutomationCore/UIA provider interfaces).

Example fix

// before
var name = targetElement.Current.Name; // throws ElementNotAvailableException if HWND died
// after
if (Automation.Compare(targetElement, AutomationElement.RootElement) || !targetElement.Current.IsOffscreen)
{
    var name = TryGetName(targetElement);
}
static string TryGetName(AutomationElement e)
{
    try { return e.Current.Name; }
    catch (ElementNotAvailableException) { return null; } // element vanished; re-find it
}
Defensive patterns

Strategy: try-catch

Validate before calling

// Before using a cached element:
bool IsUsable(AutomationElement el)
{
    try { var _ = el.Current.AutomationId; return true; }
    catch (ElementNotAvailableException) { return false; }
}

Type guard

static bool IsAlive(AutomationElement el) =>
    el != null && Automation.Compare(el, AutomationElement.RootElement) || SafeProbe(el);
static bool SafeProbe(AutomationElement el)
{ try { var _ = el.Current.Name; return true; } catch (ElementNotAvailableException) { return false; } }

Try / catch

try
{
    var value = element.Current.Name;
}
catch (ElementNotAvailableException)
{
    // HWND/proxy is gone; re-resolve the element tree
    element = root.FindFirst(TreeScope.Descendants, myCondition);
}

Prevention

When it happens

Trigger: Any Accessible-wrapped IAccessible call (e.g. get_accChild, get_accValue, accNavigate) whose underlying COM HRESULT is E_OUTOFMEMORY; most commonly when the source window/HWND has been destroyed and the OLEACC treeview proxy fails to allocate shared memory in the now-dead target process.

Common situations: Target application closed or a tree view node/window vanished while the UIA client was traversing the element tree; automated UI tests racing against app shutdown or page navigation; MSAA proxies for legacy controls whose HWND is stale.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/0d41ae9148cfa712. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/Accessible.cs:1350

                        throw new ElementNotAvailableException(e);

                    case NativeMethods.E_FAIL:
                        // An unknown or generic error occurred; treat as a not-impl. (Other methods on the object
                        // may still work, so don't treat as ElementNotAvailable.)
                    case NativeMethods.E_MEMBERNOTFOUND:
                        // The object does not support the requested property or action. For example,
                        // a push button returns this value if you request its Value property, since
                        // it does not have a Value property.
                    case NativeMethods.E_NOTIMPL:
                        // just return on E_NOTIMPL errors
                        return false;

                    case NativeMethods.E_OUTOFMEMORY:
                        // Some OLEACC proxies produce out-of-memory for non-critical reasons:
                        // notably, the treeview proxy will raise this if the target HWND no longer exists,
                        // GetWindowThreadProcessID fails and it therefore won't be able to allocate shared
                        // memory in the target process, so it incorrectly assumes OOM.
                        throw new ElementNotAvailableException(e);
                        
                    case NativeMethods.E_INVALIDARG:
                        // One or more arguments were invalid. This error occurs when the caller attempts to identify
                        // a child object using an identifier that the server does not recognize. This error also results
                        // when a client attempts to identify a child object within an object that has no children.
                        throw new ArgumentException(SR.InvalidParameter);

                    case NativeMethods.E_ACCESSDENIED:
                        // This is returned when you call get_accValue to get the value of a password control.
                        throw new UnauthorizedAccessException();

                    case NativeMethods.E_UNEXPECTED:
                        // An IAccessible server has been released unexpectedly but still has pending events.
                        // If the current execution context is inside one of these event handlers it must be 
                        // abandoned.
                        throw new ElementNotAvailableException(e);

                    default:

View on GitHub (pinned to 81131a70a4)