Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Sources/MarkdownView/MarkdownTextBuilder/ListProcessor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import MarkdownParser
// MARK: - ListProcessor

final class ListProcessor {
// Synchronous rendering must avoid the isolated-deinit back-deployment path.
nonisolated deinit {}

private let theme: MarkdownTheme
private let context: MarkdownTextView.PreprocessedContent
private let viewProvider: ReusableViewProvider
Expand Down
3 changes: 3 additions & 0 deletions Sources/MarkdownView/Supplements/InlineNode+Render.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ import SwiftMath
/// Cached attribute dictionaries to avoid repeated allocations.
/// Keyed by theme identity (font + color) which changes infrequently.
final class InlineAttributeCache {
// Synchronous rendering must avoid the isolated-deinit back-deployment path.
nonisolated deinit {}

let bodyAttributes: [NSAttributedString.Key: Any]
let codeAttributes: [NSAttributedString.Key: Any]

Expand Down
49 changes: 49 additions & 0 deletions Tests/MarkdownViewTests/RenderingLifetimeTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import Foundation
import XCTest
@testable import MarkdownView

@MainActor
final class RenderingLifetimeTests: XCTestCase {
private enum LayoutContext {
@TaskLocal static var value = "outside"
}

func testInlineAttributeCacheReleaseInsideSynchronousTaskLocalScope() {
checkSynchronousRelease {
InlineAttributeCache(theme: .default)
}
}

func testListProcessorAndOwnedCacheReleaseInsideSynchronousTaskLocalScope() {
let theme = MarkdownTheme.default
let context = MarkdownTextView.PreprocessedContent(markdown: "- **First**\n- `Second`", theme: theme)
let viewProvider = ReusableViewProvider()
checkSynchronousRelease {
ListProcessor(
theme: theme,
viewProvider: viewProvider,
context: context,
bulletDrawing: nil,
numberedDrawing: nil,
checkboxDrawing: nil
)
}
}

// Keep this synchronous to match UIKit layout callbacks outside a Swift task.
private func checkSynchronousRelease<T: AnyObject>(_ makeObject: () -> T) {
for _ in 0 ..< 100 {
LayoutContext.$value.withValue("layout") {
autoreleasepool {
var objects = [0: makeObject()]
weak var object: T?
object = objects[0]
objects.removeAll()
XCTAssertNil(object)
}
XCTAssertEqual(LayoutContext.value, "layout")
}
XCTAssertEqual(LayoutContext.value, "outside")
}
}
}
Loading