{"record":{"id":"492ff266f3aaf6d5","repo":"dotnet/wpf","slug":"sr-invalidattachedannotation","errorCode":null,"errorMessage":"SR.InvalidAttachedAnnotation","messagePattern":"SR\\.InvalidAttachedAnnotation","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Annotations/Component/HighlightComponent.cs","lineNumber":267,"sourceCode":"            highlightLayer.AddRange(this);\n            HighlightBrush = new SolidColorBrush(_background);\n            IsHitTestVisible = false;\n\n            //fire trace event\n            EventTrace.EasyTraceEvent(EventTrace.Keyword.KeywordAnnotation, EventTrace.Event.AddAttachedHighlightEnd);\n        }\n\n        /// <summary>\n        /// Remove an attached annotation from the component\n        /// </summary>\n        /// <param name=\"attachedAnnotation\">The attached annotation to be removed from the component</param>\n        public void RemoveAttachedAnnotation(IAttachedAnnotation attachedAnnotation)\n        {\n            ArgumentNullException.ThrowIfNull(attachedAnnotation);\n\n            if (attachedAnnotation != _attachedAnnotation)\n            {\n                throw new ArgumentException(SR.InvalidAttachedAnnotation, nameof(attachedAnnotation));\n            }\n\n            Invariant.Assert(_range != null, \"null highlight range\");\n\n            //fire trace event\n            EventTrace.EasyTraceEvent(EventTrace.Keyword.KeywordAnnotation, EventTrace.Event.RemoveAttachedHighlightBegin);\n\n            //check input data and retrieve the TextContainer\n            ITextContainer textContainer = CheckInputData(attachedAnnotation);\n\n            Invariant.Assert(textContainer.Highlights != null, \"textContainer.Highlights is null\");\n\n            //get AnnotationHighlightLayer in the textContainer\n            AnnotationHighlightLayer highlightLayer = textContainer.Highlights.GetLayer(typeof(HighlightComponent)) as AnnotationHighlightLayer;\n            Invariant.Assert(highlightLayer != null, \"AnnotationHighlightLayer is not initialized\");\n\n            //unregister of cargo changes\n            _attachedAnnotation.Annotation.CargoChanged -= new AnnotationResourceChangedEventHandler(OnAnnotationUpdated);","sourceCodeStart":249,"sourceCodeEnd":285,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Annotations/Component/HighlightComponent.cs#L249-L285","documentation":"RemoveAttachedAnnotation only removes the exact IAttachedAnnotation instance this HighlightComponent currently holds. Passing any other instance throws ArgumentException with SR.InvalidAttachedAnnotation. The strict identity check keeps the component's range/highlight state consistent with what the manager believes is attached.","triggerScenarios":"Calling RemoveAttachedAnnotation(attachedAnnotation) with an attachedAnnotation that is not reference-equal to the one previously added — e.g. a re-created IAttachedAnnotation from a reloaded annotation store.","commonSituations":"Rebuilding attached annotation objects between add and remove (store reload, clone); removing annotations against a different AnnotationService instance than the one that added them; holding serialized/deserialized attached annotations instead of the live instances.","solutions":["Keep the original IAttachedAnnotation reference returned at add time and pass that exact instance to RemoveAttachedAnnotation.","Track the live attached annotation per component in your manager layer so removal uses identity, not an equal copy.","Catch ArgumentException and log instead of failing when a best-effort cleanup is intended.","Route removal through AnnotationService/AnnotationComponentManager rather than calling the component directly, so the right instance is resolved."],"exampleFix":"// before\ncomponent.RemoveAttachedAnnotation(recreatedAnnotation); // identity mismatch -> throws\n// after\nif (ReferenceEquals(recreatedAnnotation, liveAnnotation))\n    component.RemoveAttachedAnnotation(liveAnnotation);","handlingStrategy":"validation","validationCode":"if (ReferenceEquals(attachedAnnotation, liveAttachedAnnotation))\n    component.RemoveAttachedAnnotation(liveAttachedAnnotation);","typeGuard":"bool IsLive(IAttachedAnnotation a, IAttachedAnnotation live) => ReferenceEquals(a, live);","tryCatchPattern":"try { component.RemoveAttachedAnnotation(a); }\ncatch (ArgumentException) { /* not the live instance; resolve via the service */ }","preventionTips":["Keep the original instance reference from add time; never recreate for removal","Remove via AnnotationService/manager, not raw component calls","Avoid mixing annotation stores/services between add and remove"],"tags":["wpf","annotations","identity-mismatch","invalid-argument"],"backgroundTag":"invalid-argument-value","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"}