Repository navigation
fix: RichLog inside a Modal, and passing data into the model - #162
Merged
Merged
Conversation
) - RichLog.view takes *const, so it can be called from a const view() without @constcast; follow mode always shows the tail. - Modal.withBody renders per-frame content (e.g. a RichLog) for one render instead of storing a frame-allocated slice that dangles on the next tick. - Modal body lines that carry their own resets keep the body style and content_bg for the rest of the line. - Program.deinit only calls Model.deinit once Model.init has run. - Program.initWithModel / Harness.initWithModel let main hand the model resources it created, such as a database pool. - Modal example gains a log modal; REFERENCE documents withBody, allocator lifetimes with deinit, and initWithModel.
This was referenced Oct 13, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #158
Closes #161
#158: RichLog in a modal crashes
I reproduced the reporter's program in a pty on Zig 0.16. They describe two crashes, and they have different causes:
RichLogwithctx.persistent_allocatorbut has nodeinitthat frees it. It is not specific to the modal.modal.body = log.view(ctx.allocator)is set inupdate(). The frame arena is reset at the start of the next tick. The modal still points at that memory, socomputeHeightreads freed pages.The reporter's workaround of setting
bodyinview()needed@constCasttwice, becauseRichLog.viewwanted a mutable pointer.Changes:
RichLog.viewtakes*const RichLog. When the filter cache is stale it filters into scratch space in the given allocator. Follow mode now always shows the tail, including aftersetSize.Modal.withBody(body). It returns a copy of the modal with a per-frame body for a single render. Call it fromview():self.modal.withBody(try self.log.view(ctx.allocator)).viewWithBackdrop(...).\x1b[0m, which used to dropcontent_bgfor the rest of the line.Program.deinitonly callsModel.deinitafterModel.inithas run. Before, a failedstart()(e.g. no terminal) handeddeinitanundefinedmodel.Harnessalready guarded this.modalexample gets a "6: Log" modal that shows the right pattern, withdeinit.#161: passing extra data to the model
Program(Model).initWithModel(gpa, io, environ_map, model, options). It sets the model's initial value, e.g..{ .pool = pool }, beforestart().Model.initstill runs and sees those fields throughself.Harness(Model).initWithModel(allocator, io, model, options)for tests, which covers the commented-out test in the issue.Docs
REFERENCE.md now covers:
withBodyand borrowed modal stringsctx.allocatorslices; free persistent state indeinitTests
testing.allocatorleak check, and quit. Withoutdeinitit fails with 15 leaks.*Storecreated outside the program.initWithModelseeds the model and options, anddeinitskipsModel.deinitwhenstart()never ran.withBody, and the body background after embedded resets. The background test fails without the fix.zig build test,zig buildandzig fmt --check src tests examples build.zigpass on Zig 0.16.0. I also ran themodalexample in a pty (open, close, log modal, quit): it exits with status 0 and reports no leaks.