LykosAI/StabilityMatrix · error · InvalidOperationException
Cannot create a marker when not attached to a document
Error message
Cannot create a marker when not attached to a document
What it means
TextMarkerService.TryCreate requires the service to be attached to a text document (an AvalonEdit-style document/text area). When _markers is null — meaning the service never attached — it throws InvalidOperationException('Cannot create a marker when not attached to a document') instead of silently returning null.
Solutions
- Guard calls so TryCreate is only invoked after the TextArea.Document is set and the service is attached
- Check an IsAttached/_markers != null condition before calling TryCreate
- Re-attach (or recreate) the service when a new document is loaded
- Return null or queue pending markers until attachment instead of calling early
Example fix
// before
var marker = _markerService.TryCreate(offset, length);
// after
if (_markerService is { } svc && textArea.Document is not null)
var marker = svc.TryCreate(offset, length); Defensive patterns
Strategy: try-catch
Validate before calling
if (textArea?.Document is null) return null; // not attached yet
Type guard
bool IsAttached(TextMarkerService s) => s.GetType().GetField("_markers", BindingFlags.NonPublic|BindingFlags.Instance)?.GetValue(s) != null; Try / catch
try { marker = service.TryCreate(offset, length); }
catch (InvalidOperationException) { marker = null; /* queue for after attach */ } Prevention
- Only create markers after TextArea.Document is assigned
- Defer diagnostics until the editor Loaded event
- Recreate/reattach the marker service on document change
When it happens
Trigger: Calling TryCreate before the service is attached to a TextArea/Document (e.g. during editor construction or after the editor was closed and detached), or creating markers from a background thread before attachment completes.
Common situations: Diagnostics/ squiggles pipeline running before the editor finishes initializing; markers requested after document unload; view model calls into the service when the editor control hasn't loaded.
Understand the failure class
Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.
Related errors
- Comfy client is not connected
- ImageSource is not a local file or bitmap
- Prompt must be processed before calling…
- Client is not connected
- Installed package path is not available.
AI-assisted analysis of LykosAI/StabilityMatrix@af93d6ef57 (2026-09-12).
Data as JSON: /api/errors/eb5079b20a57e393.
Report an issue: GitHub.
Appendix: source
Thrown at StabilityMatrix.Avalonia/Controls/TextMarkers/TextMarkerService.cs:76
// {
// args.SetToolTip(GetTooltipTextForCollapsedSection(args, collapsedSection));
// }
//}
var markersAtOffset = GetMarkersAtOffset(offset);
var markerWithToolTip = markersAtOffset.FirstOrDefault(marker => marker.ToolTip != null);
if (markerWithToolTip != null && markerWithToolTip.ToolTip != null)
{
args.SetToolTip(markerWithToolTip.ToolTip);
}
}*/
#region TextMarkerService
public TextMarker? TryCreate(int startOffset, int length)
{
if (_markers == null)
throw new InvalidOperationException("Cannot create a marker when not attached to a document");
var textLength = _document.TextLength;
if (startOffset < 0 || startOffset > textLength) return null;
//throw new ArgumentOutOfRangeException(nameof(startOffset), startOffset, "Value must be between 0 and " + textLength);
if (length < 0 || startOffset + length > textLength) return null;
//throw new ArgumentOutOfRangeException(nameof(length), length, "length must not be negative and startOffset+length must not be after the end of the document");
var marker = new TextMarker(this, startOffset, length);
_markers.Add(marker);
return marker;
}
public IEnumerable<TextMarker> GetMarkersAtOffset(int offset)
{
return _markers.FindSegmentsContaining(offset);
}
public IEnumerable<TextMarker> TextMarkers => _markers ?? Enumerable.Empty<TextMarker>();View on GitHub (pinned to af93d6ef57)