dotnet/wpf · error · ArgumentException

SR.IncorrectAnchorLength

Error message

SR.IncorrectAnchorLength

What it means

TextSelectionProcessor.ResolveLocatorPart throws ArgumentException(SR.IncorrectAnchorLength) when the text anchor built from the supplied TextAnchorLocatorPart resolves to an empty (zero or negative length) selection. WPF Annotations anchoring requires a non-empty text segment range to attach an annotation to; an empty selection has no span to anchor against.

Solutions

  1. Verify the segment's start and end offsets differ and end>start before creating/serializing the locator part
  2. Re-resolve the annotation after re-anchoring against refreshed text so offsets are recomputed
  3. Validate persisted locator parts when loading and drop/repair ones with empty ranges

Example fix

// before
part.NameValuePairs["Segment0"] = "10,10"; // empty range
// after
part.NameValuePairs["Segment0"] = "10,25"; // start < end
Defensive patterns

Strategy: validation

Validate before calling

// parse segment offsets before resolving
var kv = part.NameValuePairs["Segment0"].Split(',');
int s = int.Parse(kv[0], System.Globalization.CultureInfo.InvariantCulture);
int e = int.Parse(kv[1], System.Globalization.CultureInfo.InvariantCulture);
if (e <= s) throw new InvalidOperationException("Anchor range must be non-empty (end > start).");

Try / catch

try { processor.ResolveLocatorPart(part, out level); }
catch (ArgumentException ex) { /* log ex.ParamName; treat annotation as unresolvable */ }

Prevention

When it happens

Trigger: Calling ResolveLocatorPart with a ContentLocatorPart (TextSelectionProcessor) whose parsed segment start/end offsets produce an empty anchor — e.g. start==end or end<start, or a segment string encoding a zero-length range.

Common situations: Persisted annotation locators from an older document whose text shrank or was deleted; programmatically generated locator parts with equal start/end offsets; off-by-one in offset computation when creating anchors.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Annotations/Anchoring/TextSelectionProcessor.cs:242

                    return null;

                ITextPointer start = elementStart.CreatePointer(startOffset);// new TextPointer((TextPointer)elementStart, startOffset);

                ITextPointer end = (textRangeLength <= endOffset) ?
                    elementEnd.CreatePointer() : //new TextPointer((TextPointer)elementEnd) :
                    elementStart.CreatePointer(endOffset);// new TextPointer((TextPointer)elementStart, endOffset);

                //we do not process 0 length selection
                if (start.CompareTo(end) >= 0)
                    return null;

                anchor.AddTextSegment(start, end);
            }

            //we do not support 0 or negative length selection
            if (anchor.IsEmpty)
            {
                throw new ArgumentException(SR.IncorrectAnchorLength, nameof(locatorPart));
            }

            attachmentLevel = AttachmentLevel.Full;

            if (_clamping)
            {
                ITextPointer currentStart = anchor.Start;
                ITextPointer currentEnd = anchor.End;
                IServiceProvider serviceProvider = null;
                ITextView textView = null;

                if (_targetPage != null)
                {
                    serviceProvider = _targetPage as IServiceProvider;
                }
                else
                {
                    FlowDocument content = currentStart.TextContainer.Parent as FlowDocument;

View on GitHub (pinned to 81131a70a4)