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
- Clamp distance to the container's valid offset range before creating the pointer.
- Use MoveByOffset-style APIs that check return values, or compare pointer positions with CompareTo before moving.
- Iterate with smaller moves until Move succeeds, or use the container's end/start pointers as bounds.
- 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
- Clamp offsets against the container's start/end pointers before moving.
- Compare pointer positions (CompareTo) to bound seeks.
- Never derive distances from foreign documents' offsets.
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
- SR.NotInAssociatedContainer
- SR.BadFixedTextPosition
- SR.NegativeValue
- SR.NoElementObject
- SR.TextPositionIsFrozen
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)