{"record":{"id":"52af1559fc5c82f2","repo":"java-native-access/jna","slug":"error-occured-in-unadvise-when-trying-to-disconnect-the","errorCode":null,"errorMessage":"Error occured in unadvise when trying to disconnect the listener from <this>","messagePattern":"Error occured in unadvise when trying to disconnect the listener from <this>","errorType":"exception","errorClass":"com.sun.jna.platform.win32.COM.COMException","httpStatus":null,"severity":"error","filePath":"contrib/platform/src/com/sun/jna/platform/win32/COM/util/ProxyObject.java","lineNumber":350,"sourceCode":"            if (null == comInterfaceAnnotation) {\n                throw new COMException(\n                        \"unadvise: Interface must define a value for iid via the ComInterface annotation\");\n            }\n            IID iid = this.getIID(comInterfaceAnnotation);\n\n            final ConnectionPoint rawCp = this.fetchRawConnectionPoint(iid);\n\n            HRESULT hr = rawCp.Unadvise(((ComEventCallbackCookie) cookie).getValue());\n\n            rawCp.Release();\n            COMUtils.checkRC(hr);\n\n        } catch (RuntimeException e) {\n            // Do not rewrap COMException\n            if (e instanceof COMException) {\n                throw e;\n            } else {\n                throw new COMException(\"Error occured in unadvise when trying to disconnect the listener from \" + this, e);\n            }\n        }\n    }\n\n    // --------------------- IDispatch ------------------------------\n    @Override\n    public <T> void setProperty(String name, T value) {\n        DISPID dispID = resolveDispId(this.getRawDispatch(), name);\n        setProperty(dispID, value);\n    }\n\n    @Override\n    public <T> void setProperty(DISPID dispId, T value) {\n        assert COMUtils.comIsInitialized() : \"COM not initialized\";\n\n        VARIANT v = Convert.toVariant(value);\n        WinNT.HRESULT hr = this.oleMethod(OleAuto.DISPATCH_PROPERTYPUT, null, this.getRawDispatch(), dispId, v);\n        Convert.free(v, value); // Free value allocated by Convert#toVariant","sourceCodeStart":332,"sourceCodeEnd":368,"githubUrl":"https://github.com/java-native-access/jna/blob/d036ad9781adad4b66693e8fa7098e4ac665e0a3/contrib/platform/src/com/sun/jna/platform/win32/COM/util/ProxyObject.java#L332-L368","documentation":"Generic wrapper COMException thrown by ProxyObject.unadvise() when disconnecting the listener throws a non-COMException RuntimeException. The original exception is preserved as the cause; the message includes the proxy's toString. Failures here typically mean the connection point or cookie is no longer valid.","triggerScenarios":"unadvise() fails while fetching the connection point or calling IConnectionPoint.Unadvise(cookie) — e.g. an invalid/stale cookie, or the COM object already released its connection points.","commonSituations":"Calling unadvise with a cookie from a different advise call; double-unadvise; unadvising after the COM server terminated; wrong interface class for the cookie.","solutions":["Check getCause() for the underlying error","Ensure the cookie comes from the matching advise() on the same interface/proxy","Call unadvise exactly once per successful advise","Unadvise before releasing the proxy, on the same thread/apartment"],"exampleFix":"// before\nproxy.unadvise(MyEvents.class, cookieFromOtherProxy); // fails\n// after\nIComEventCallbackCookie cookie = proxy.advise(MyEvents.class, listener);\ntry { /* use */ } finally { proxy.unadvise(MyEvents.class, cookie); }","handlingStrategy":"try-catch","validationCode":"if (cookie == null || iface == null) throw new IllegalArgumentException(\"cookie and interface required\");","typeGuard":"boolean validPair = cookie != null && iface.isAnnotationPresent(ComInterface.class);","tryCatchPattern":"try { proxy.unadvise(iface, cookie); } catch (COMException e) { LOG.error(\"unadvise failed on \" + this, e.getCause()); }","preventionTips":["Unadvise once, with the cookie from the matching advise","Unadvise before releasing the proxy","Perform unadvise on the same thread as advise"],"tags":["com","events","callback","wrapper"],"backgroundTag":"api-request-failed","analyzedSha":"d036ad9781adad4b66693e8fa7098e4ac665e0a3","analyzedAt":"2026-09-12T06:50:59.239Z","contentChangedAt":"2026-09-12T06:50:59.239Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}