{"record":{"id":"6bddc1777047d20a","repo":"dotnet/wpf","slug":"argumentnullexception-hwndsubclass","errorCode":null,"errorMessage":"ArgumentNullException","messagePattern":"ArgumentNullException","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/Shared/MS/Win32/HwndSubclass.cs","lineNumber":227,"sourceCode":"        /// <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.\n        /// </summary>\n        /// <param name=\"hwnd\">\n        ///     The window that this message was sent or posted to.\n        /// </param>","sourceCodeStart":209,"sourceCodeEnd":245,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/Shared/MS/Win32/HwndSubclass.cs#L209-L245","documentation":"The second validation in HwndSubclass.RequestDetach: the 'subclass' parameter must be a non-zero IntPtr identifying the subclass instance. Passing IntPtr.Zero throws ArgumentNullException for subclass.","triggerScenarios":"Calling HwndSubclass.RequestDetach(hwnd, IntPtr.Zero, force) — a zero handle where the subclass (gcHandle wrapper) reference is required.","commonSituations":"The subclass reference field was never initialized or was cleared before detach; passing default(IntPtr) as a placeholder; race where detach runs after the wrapper reset its fields.","solutions":["Pass the original non-zero subclass IntPtr (the gcHandle) used at attach time","Guard with if (subclass != IntPtr.Zero) before calling RequestDetach","Keep the subclass handle alive until detach completes; avoid clearing it during teardown"],"exampleFix":"// before\nHwndSubclass.RequestDetach(hwnd, IntPtr.Zero, true);\n// after\nif (_subclass != IntPtr.Zero) HwndSubclass.RequestDetach(hwnd, _subclass, true);","handlingStrategy":"validation","validationCode":"if (hwnd == IntPtr.Zero || subclass == IntPtr.Zero) return;\nHwndSubclass.RequestDetach(hwnd, subclass, force);","typeGuard":"bool CanRequestDetach(IntPtr hwnd, IntPtr subclass) => hwnd != IntPtr.Zero && subclass != IntPtr.Zero;","tryCatchPattern":"try { HwndSubclass.RequestDetach(hwnd, subclass, force); } catch (ArgumentNullException ex) when (ex.ParamName == \"subclass\") { /* subclass reference lost; nothing to detach */ }","preventionTips":["Keep the subclass gcHandle IntPtr alive until detach completes","Validate both handles before calling RequestDetach","Clear subclass fields only after detach returns"],"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-22T01:17:13.364Z"}