diff --git a/GUI/host/host.cpp b/GUI/host/host.cpp index bc50a70..b010cd6 100644 --- a/GUI/host/host.cpp +++ b/GUI/host/host.cpp @@ -53,15 +53,17 @@ namespace WinMutex viewMutex; }; - ThreadSafe>> textThreadsByParams; - ThreadSafe> processRecordsByIds; + ThreadSafe>, std::recursive_mutex> textThreadsByParams; + ThreadSafe, std::recursive_mutex> processRecordsByIds; ThreadParam CONSOLE{ 0, -1ULL, -1ULL, -1ULL }, CLIPBOARD{ 0, 0, -1ULL, -1ULL }; void RemoveThreads(std::function removeIf) { + std::vector> removedThreads; auto[lock, textThreadsByParams] = ::textThreadsByParams.operator->(); - for (auto it = textThreadsByParams->begin(); it != textThreadsByParams->end(); removeIf(it->first) ? it = textThreadsByParams->erase(it) : ++it); + for (auto it = textThreadsByParams->begin(); it != textThreadsByParams->end(); removeIf(it->first) ? it = textThreadsByParams->erase(it) : ++it) + if (removeIf(it->first)) removedThreads.emplace_back(std::move(it->second)); } void CreatePipe() @@ -104,7 +106,7 @@ namespace default: { auto tp = *(ThreadParam*)buffer; - if (textThreadsByParams->count(tp) == 0) textThreadsByParams->insert({ tp, std::make_shared(tp, Host::GetHookParam(tp)) }); + if (textThreadsByParams->count(tp) == 0) textThreadsByParams->insert({ tp, std::make_unique(tp, Host::GetHookParam(tp)) }); textThreadsByParams->at(tp)->Push(buffer + sizeof(tp), bytesRead - sizeof(tp)); } break; @@ -136,8 +138,8 @@ namespace Host TextThread::OnDestroy = OnDestroy; TextThread::Output = Output; processRecordsByIds->try_emplace(CONSOLE.processId, CONSOLE.processId, INVALID_HANDLE_VALUE); - textThreadsByParams->insert({ CONSOLE, std::make_shared(CONSOLE, HookParam{}, L"Console") }); - textThreadsByParams->insert({ CLIPBOARD, std::make_shared(CLIPBOARD, HookParam{}, L"Clipboard") }); + textThreadsByParams->insert({ CONSOLE, std::make_unique(CONSOLE, HookParam{}, L"Console") }); + textThreadsByParams->insert({ CLIPBOARD, std::make_unique(CLIPBOARD, HookParam{}, L"Clipboard") }); StartCapturingClipboard(); CreatePipe(); } @@ -199,9 +201,9 @@ namespace Host return processRecordsByIds->at(tp.processId).GetHook(tp.addr).hp; } - std::shared_ptr GetThread(ThreadParam tp) + TextThread* GetThread(ThreadParam tp) { - return textThreadsByParams->at(tp); + return textThreadsByParams->at(tp).get(); } void AddConsoleOutput(std::wstring text) diff --git a/GUI/host/host.h b/GUI/host/host.h index fa6e1ef..b1d02a5 100644 --- a/GUI/host/host.h +++ b/GUI/host/host.h @@ -14,7 +14,7 @@ namespace Host HookParam GetHookParam(ThreadParam tp); - std::shared_ptr GetThread(ThreadParam tp); + TextThread* GetThread(ThreadParam tp); void AddConsoleOutput(std::wstring text); inline int defaultCodepage = SHIFT_JIS; diff --git a/GUI/mainwindow.cpp b/GUI/mainwindow.cpp index b03595e..3a15b8f 100644 --- a/GUI/mainwindow.cpp +++ b/GUI/mainwindow.cpp @@ -102,13 +102,19 @@ void MainWindow::ProcessConnected(DWORD processId) void MainWindow::ProcessDisconnected(DWORD processId) { - QMetaObject::invokeMethod(this, [this, processId] { ui->processCombo->removeItem(ui->processCombo->findText(QString::number(processId, 16).toUpper() + ":", Qt::MatchStartsWith)); }); + QMetaObject::invokeMethod(this, [this, processId] + { + ui->processCombo->removeItem(ui->processCombo->findText(QString::number(processId, 16).toUpper() + ":", Qt::MatchStartsWith)); + }, Qt::BlockingQueuedConnection); } void MainWindow::ThreadAdded(TextThread* thread) { QString ttString = TextThreadString(thread) + S(thread->name) + " (" + GenerateCode(thread->hp, thread->tp.processId) + ")"; - QMetaObject::invokeMethod(this, [this, ttString] { ui->ttCombo->addItem(ttString); }); + QMetaObject::invokeMethod(this, [this, ttString] + { + ui->ttCombo->addItem(ttString); + }); } void MainWindow::ThreadRemoved(TextThread* thread) @@ -123,7 +129,7 @@ void MainWindow::ThreadRemoved(TextThread* thread) ViewThread(0); } ui->ttCombo->removeItem(threadIndex); - }); + }, Qt::BlockingQueuedConnection); } bool MainWindow::SentenceReceived(TextThread* thread, std::wstring& sentence)