Skip to content

Threads in VST #1245

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Dzyszla opened this issue Mar 18, 2024 · 5 comments
Closed

Threads in VST #1245

Dzyszla opened this issue Mar 18, 2024 · 5 comments
Assignees
Milestone

Comments

@Dzyszla
Copy link

Dzyszla commented Mar 18, 2024

I have very hard problem - in some case, when I have window with VST in DLL, and I close it, whole application sometimes crash with AV error in unloaded DLL (especially on slow computers).

If I add sleep (f.ex. 500 ms) after window free, but before end of DLL's procedure (witch open and free my form with VST), then looks work good.

So, I think it can be some mistake in some thread... But where?

I look on VirtualTrees.WorkerThread file and Dispose procedure and have question: why is there terminate and setevent, but not waiting for really terminate thread (WaitFor)? But I'm not sure this applies to my case or this is true mistake.

It's applies to 7.x and 8.0 version.

@joachimmarder
Copy link
Contributor

What exactly make you think that the thread of Virtual TreeView causes the crash?

@Dzyszla
Copy link
Author

Dzyszla commented Mar 19, 2024

I attach demo code (two projects: exe and dll):
VST_Test.zip

On my computer have no errors, but when I run it on Windows 2012/2016/2019 Server, click button and close window (by shortcut, by mouse click - no matter) in 1 on 10 times crash (but sometime can be 10 times without error, and next every 3 run and close ends with crash).
here attach system logs:
VSTerrors.zip

@Dzyszla
Copy link
Author

Dzyszla commented Mar 19, 2024

PS - When I made changes in dispose method, then works good:

class procedure TWorkerThread.Dispose(CanBlock: Boolean);
var
  LRef: TThread;
begin
  WorkerThread.FreeOnTerminate := False;   // << NEVR USE FREE ON TERMINATE
  //WorkerThread.FreeOnTerminate := not CanBlock;  // << -- " --
  WorkerThread.Terminate();
  SetEvent(WorkerThread.FWorkEvent);
  LRef := WorkerThread;
  WorkerThread.WaitFor; // << ADD WAITING  FOR TERMINATE
  WorkerThread := nil; //Will be freed usinf TThread.FreeOnTerminate
  //if CanBlock then   << DISABLE THIS LINE - OBJECT MUST BE ALWAYS FREE
	LRef.Free;
end;

@joachimmarder
Copy link
Contributor

joachimmarder commented Mar 19, 2024

I didn't get a single crash in 30 tries with your demo project.

Can you please check if the following change makes any difference in your case: In TBaseVirtualTree.Destroy() change the call to TWorkerThread.ReleaseThreadReference to:

TWorkerThread.ReleaseThreadReference(IsLibrary);

@Dzyszla
Copy link
Author

Dzyszla commented Mar 20, 2024

I try my demo on my own system, and it's crash after about 1 on 60-80 trials (very infrequently, but possible). I think, it depends on cpu cores load.

I made the changes as suggested (and I rolled back my own previous changes) and looks all right - no more crashes.

joachimmarder added a commit that referenced this issue Mar 20, 2024
@joachimmarder joachimmarder added this to the 8.0.3 milestone Mar 20, 2024
@joachimmarder joachimmarder self-assigned this Mar 20, 2024
salvadorbs pushed a commit to salvadorbs/VirtualTreeView-Lazarus that referenced this issue Sep 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants