尝试在我的基类中启动线程时,Visual Studio 由于调试错误而中止我的程序
When trying to start a thread in my base class, Visual Studio aborts my program due to a debug error
当我从基类启动线程时,它运行良好,然后Visual Studio告诉我存在中止错误。
我正在使用Visual Studio Community 19,当我调试程序时,Visual Studio在我创建新线程和 弹出一条错误消息,并显示一个对话框,询问我是否要中止、重试或忽略。
它看起来像这样:
Microsoft 可视化C++运行时库
(x( 调试错误!
程序:C\用户\超级\桌面\无效\调试\无效.exe
abort(( 已被调用
(按重试以调试应用程序(
如果我不创建新线程,而只是调用"run(("函数,则程序运行时没有任何调试错误。
这是启动线程的代码
void Application::start()
{
std::thread newThread(&Application::run, this);
while (!running)
{
std::cout << "Not Running!" << std::endl;
}
}
这是应用程序运行的代码
void Application::run()
{
setup();
running = true;
//using namespace std::chrono;
auto lastTime = std::chrono::high_resolution_clock::now();
auto timer = std::chrono::high_resolution_clock::now();
double delta = 0;
int ticks = 0;
while (running)
{
setTargetTPS(60);
auto now = std::chrono::high_resolution_clock::now();
delta += std::chrono::duration_cast<std::chrono::nanoseconds>(now - lastTime).count() / ns;
lastTime = now;
while (delta > 1)
{
update();
ticks++;
delta--;
}
if (std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now() - timer).count() > 1000)
{
timer = std::chrono::high_resolution_clock::now();
tps = ticks;
ticks = 0;
std::cout << "TPS: " << tps << std::endl;
}
}
}
游戏是应用程序的子类,我是这样用的
#include "Game.h"
int main()
{
Game* game = new Game();
game->start();
return 0;
}
正如 drescherjm 已经评论的那样,当Application::start
完成时(紧接在Application::run
更改running
之后true
,这发生在它开始时(,newThread
的析构函数被调用。
根据 Thread::~thread 的 cppreference 页面,当前运行的thread
的析构函数调用std::terminate()
,根据此调用(默认情况下(std::abort()
,导致您看到的消息。
简而言之,你不能让一个正在运行的Thread
超出范围。
如果合适,您可以简单地在Application::start
中调用newThread.join()
,这将保持它直到线程完成。
或者你可以打电话给newThread.detach()
.这将使newThread
从主线程中消失,使其无法join
- 这可能是好是坏,这取决于您的需求。在这种状态下,尽管线程本身仍在运行,但newThread
变量可能会被和平地破坏(因为分离线程会断开它与保存它的变量的连接(。
- C++为什么我的编译器成功了,但我的计算机给出了调试错误?
- 为什么我的 VS 代码调试器在我的C++代码周围弹跳?
- 如何在 WSL 上获取 VS 代码以调试我的C++代码?
- 为什么我的程序无法在GDB在线编译器/调试器或Visual Studio C++ 2019中运行
- 我正在调试这个C++程序.编译器不再显示语法错误,但存在隐藏的逻辑错误
- 如何调试我的 while 循环C++程序?
- 如何调试我的C++ BMI 计算器程序?
- CLion 没有调试我的目标之一 - 立即"进程已完成,退出代码为 0"
- 尝试在我的基类中启动线程时,Visual Studio 由于调试错误而中止我的程序
- 在使用 vs 代码调试文件时,如何将文件传递到我的程序中
- 为什么我的程序仅适用于调试版本
- 我的代码在发布模式下不起作用,而在Qt的调试模式下工作
- 在没有 -g 选项的情况下编译,但我想获得更详细的调试信息
- 为什么我的 UWP 游戏在发布时比在调试模式下慢
- 如何调试我的 OpenGl 问题
- 如何在我的C++程序中调试字符串处理
- 我的调试器说我有一个隔离错误错误,但无法告诉我在哪里,在非常特殊的情况下发生
- 我缺少一些东西,我试图进行一段时间的调试(我是新手C )
- 我的调试版本dll使用了可重新分发和崩溃的发布版本
- c++链接发布构建库与我的调试构建