dotnet/wpf · error · ArgumentException

SR.BadDistance

Error message

SR.BadDistance

What it means

FixedTextPointer.CreatePointer (CreateDynamicPointer) clones the current FlowPosition and moves it by 'distance'. If the move cannot be performed (target outside the document's valid position range), ArgumentException(SR.BadDistance) is thrown.

Solutions

  1. Clamp distance to the container's valid offset range before creating the pointer.
  2. Use MoveByOffset-style APIs that check return values, or compare pointer positions with CompareTo before moving.
  3. Iterate with smaller moves until Move succeeds, or use the container's end/start pointers as bounds.
  4. Catch ArgumentException and clamp/retry with a smaller distance.

Example fix

// before
var p = pointer.CreatePointer(gravity, hugeOffset); // throws if out of range
// after
var p = pointer.CreatePointer(gravity, Math.Min(Math.Max(hugeOffset, minOffset), maxOffset));
Defensive patterns

Strategy: validation

Validate before calling

int clamped = Math.Max(container.MinOffset, Math.Min(container.MaxOffset, distance));
var p = pointer.CreatePointer(gravity, clamped);

Try / catch

try { p = pointer.CreatePointer(gravity, distance); }
catch (ArgumentException) { p = distance > 0 ? container.End : container.Start; }

Prevention

When it happens

Trigger: Calling CreatePointer(gravity, distance) with a distance larger than the remaining positions forward/backward from the current pointer — e.g. moving past the end or before the start of the text container.

Common situations: Seeking by a symbol count computed from another document; retrying moves in a loop without checking bounds; distances derived from user input.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/FixedTextPointer.cs:275

        /// <summary>
        /// <see cref="ITextPointer.CreatePointer(LogicalDirection)"/>
        /// </summary>
        ITextPointer ITextPointer.CreatePointer(LogicalDirection gravity)
        {
            return ((ITextPointer)this).CreatePointer(0, gravity);
        }

        /// <summary>
        /// <see cref="ITextPointer.CreatePointer(int,LogicalDirection)"/>
        /// </summary>
        ITextPointer ITextPointer.CreatePointer(int distance, LogicalDirection gravity)
        {
            ValidationHelper.VerifyDirection(gravity, "gravity");

            FlowPosition fp = (FlowPosition)_flowPosition.Clone();
            if (!fp.Move(distance))
            {
                throw new ArgumentException(SR.BadDistance, nameof(distance));
            }

            return new FixedTextPointer(true, gravity, fp);
        }

        /// <summary>
        /// <see cref="ITextPointer.Freeze"/>
        /// </summary>
        void ITextPointer.Freeze()
        {
            _isFrozen = true;
        }

        /// <summary>
        /// <see cref="ITextPointer.GetFrozenPointer"/>
        /// </summary>
        ITextPointer ITextPointer.GetFrozenPointer(LogicalDirection logicalDirection)
        {

View on GitHub (pinned to 81131a70a4)