{"record":{"id":"33641e86d99ef0a1","repo":"dotnet/wpf","slug":"throw-new-argumentnullexception-nameof-hwnd","errorCode":null,"errorMessage":"throw new ArgumentNullException(nameof(hwnd));","messagePattern":"throw new ArgumentNullException\\(nameof\\(hwnd\\)\\);","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/Shared/MS/Win32/HwndSubclass.cs","lineNumber":223,"sourceCode":"        /// </param>\n        /// <param name=\"subclass\">\n        ///     The identifier of the subclass to unsubclass.\n        /// </param>\n        /// <param name=\"force\">\n        ///     Whether or not the unsubclassing should be forced.  Due to the\n        ///     way that Win32 implements window subclassing, it is not always\n        ///     possible to safely remove a window proc from the WNDPROC chain.\n        ///     However, the delegate will not be called again after this\n        ///     method returns.\n        /// </param>\n        /// <returns>\n        ///     Nothing.\n        /// </returns>\n        internal static void RequestDetach(IntPtr hwnd, IntPtr subclass, bool force)\n        {\n            if(hwnd == IntPtr.Zero)\n            {\n                throw new ArgumentNullException(nameof(hwnd));\n            }\n            if(subclass == IntPtr.Zero)\n            {\n                throw new ArgumentNullException(nameof(subclass));\n            }\n\n            int iForce = force ? 1 : 0;\n            UnsafeNativeMethods.UnsafeSendMessage(hwnd, DetachMessage, subclass, (IntPtr) iForce);\n        }\n\n        /// <summary>\n        ///     This is the WNDPROC that gets inserted into the window's\n        ///     WNDPROC chain.  It responds to various conditions that\n        ///     would cause this HwndSubclass object to unsubclass the window,\n        ///     and then calls the delegate specified to the HwndSubclass\n        ///     constructor to process the message.  If the delegate does not\n        ///     handle the message, the message is then passed on down the\n        ///     WNDPROC chain for further processing.","sourceCodeStart":205,"sourceCodeEnd":241,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/Shared/MS/Win32/HwndSubclass.cs#L205-L241","documentation":"HwndSubclass.RequestDetach sends a special message to the window to locate and detach the given subclass. It validates inputs: an IntPtr.Zero hwnd cannot receive a message, so it throws ArgumentNullException for hwnd.","triggerScenarios":"Calling HwndSubclass.RequestDetach(IntPtr.Zero, subclass, force) — passing a zero handle as the target window.","commonSituations":"Caching an HWND after the window was destroyed (handle resets to Zero); using a default-initialized IntPtr; detaching during shutdown after handle teardown.","solutions":["Verify hwnd != IntPtr.Zero before calling RequestDetach","Skip detach if the window handle is already destroyed/zero (detach is then moot)","Store the HWND in a checked property rather than a raw field that can become Zero"],"exampleFix":"// before\nHwndSubclass.RequestDetach(_hwnd, _gcHandle, true);\n// after\nif (_hwnd != IntPtr.Zero) HwndSubclass.RequestDetach(_hwnd, _gcHandle, true);","handlingStrategy":"validation","validationCode":"if (hwnd == IntPtr.Zero) return; // window already gone; nothing to detach\nHwndSubclass.RequestDetach(hwnd, subclass, force);","typeGuard":"bool IsValidWindowHandle(IntPtr hwnd) => hwnd != IntPtr.Zero;","tryCatchPattern":"try { HwndSubclass.RequestDetach(hwnd, subclass, force); } catch (ArgumentNullException ex) when (ex.ParamName == \"hwnd\") { /* handle already destroyed; skip */ }","preventionTips":["Check handle validity before any Win32 call","Treat a destroyed window's Zero handle as a no-op for detach","Do not cache raw IntPtrs across window teardown"],"tags":["csharp","wpf","win32","null-argument"],"backgroundTag":"null-argument","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}