mirror of
https://github.com/rivo/tview.git
synced 2026-04-27 05:45:49 +03:00
[GH-ISSUE #399] Question: Panic when printing to two textviews #293
Labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
starred/tview#293
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @ethantrithon on GitHub (Feb 8, 2020).
Original GitHub issue: https://github.com/rivo/tview/issues/399
I'm currently working on a go game using tview, and to make testing of certain things faster, I've set up a goroutine in my main file which automatically places stones and does a few other test things. I used to have a delay of about 100ms between each stone placed, and everything was fine. I removed the delay and then I get a panic (but not all the time, just about 70-80%) which looks like:
where sometimes the index it's trying to access is not 0, although it mostly is, I've seen other values like 3, 7 or 15 as well.
the relevant code is:
input and history are both TextViews, goban is a wrapper for a text view (it's essentially the main component of the go game).
Why does this crash? I'm surprised it does since in the wiki it says that
Which yes, in this case IS the only thing I'm doing.
The commented out "highlight row/column" methods surround a line with [color]...[white] text, and then call SetText again with the updated string, but since it also panics without the call to the highlight functions, i figured that's not the root cause of the issue. Removing one of the Fprintf calls but leaving in the highlight functions has never crashed so far
Am I simply missing a check for concurrency somewhere or is this indeed a bug?
(I'm happy to provide more source code if necessary)
@tslocum commented on GitHub (Feb 13, 2020):
It looks like this error is from Application.Draw being called on the main thread while TextView.Clear is called on another thread. Only writing to TextViews is officially supported from any goroutine. Otherwise, you must queue your code to execute on the application's main thread.
See https://rocketnine.space/post/tview-and-you/#thread-safety
@rivo commented on GitHub (Feb 19, 2020):
@tslocum may be right. Here's code that should fix it:
But I'm not 100% sure because I can't run your program.
You should also update to the latest commit. I had to introduce a small change to
Application.QueueUpdate()to make it return after the provided function has executed. Otherwise, there could have been situations in your code where text is written to theTextViewbefore it is cleared.Please let me know if the panic still occurs.