如何调试 Qt 命令行应用程序?帮助!

发布于 2024-10-21 14:51:05 字数 843 浏览 2 评论 0原文

我的 Qt 应用程序遇到了一些相当奇怪的问题...

应用程序被编写为在终端/命令提示符下工作,它由两个线程组成(+ 主线程仅启动其他两个线程并执行事件循环)。这两个线程不共享任何资源(它们都有自己的 QNetworkAccessManager、QSqlDatabase 等版本......),因此我不使用 QMutex 或任何其他类似的机制。问题是有时我的应用程序在启动几秒钟后就会崩溃。我不知道问题是什么,并且我无法使用 QtCreator 的内置调试器获得有用的答案(或者我不知道该怎么做)。 有趣的是,这些错误(导致我的应用程序崩溃)是随机发生的(至少我找不到模式),当我不启动两个线程之一时,一切都工作正常。但同样,他们不使用任何公共资源...

这里有一些屏幕截图,我不明白为什么堆栈跟踪中只有 Qt 框架函数(不是我编写的函数 - 就像在我的代码执行之前发生错误一样) ,但事实并非如此)...

这是我最常收到的:

http:// img859.imageshack.us/f/75996377.png/

这两个我有时会得到:

http://img690.imageshack.us/f/23373599.png/

http://img687.imageshack.us/f/25248518.png/

I'm having some rather strange problems with my Qt application...

Application is written to work in terminal/command prompt, and it consists of two threads (+ main one that only starts other two and executes event loop). These two threads don't share any resources (they both have their own version of QNetworkAccessManager, QSqlDatabase and so on ...), so I don't use QMutex or any other similar mechanism. The problem is that sometimes my application just crashes few seconds after I start it. I don't know what is the problem, and I can't get useful answer with QtCreator's built-in debugger (or I don't know how to do it).
The funny thing is that these errors (that crashes my app) are occurring randomly (at least I can't find a pattern), and when I don't start one of the two threads, everything is working fine. But again, they do not use any common resources ...

Here are few screenshots, I don't understand why there are only Qt framework functions in stack trace (not one function that I have written - like the error happens before my code executes, but that's not true)...

This is the one I get most frequently:

http://img859.imageshack.us/f/75996377.png/

And these two I get just sometime:

http://img690.imageshack.us/f/23373599.png/

http://img687.imageshack.us/f/25248518.png/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

等往事风中吹 2024-10-28 14:51:05

您只能在堆栈跟踪中看到 Qt 代码,因为错误发生在事件循环中(即 QCoreApplication::exec)。我在堆栈跟踪中看到与 线程本地存储 相关的 Win32 调用。您是在使用它们的每个线程的上下文中还是在其他线程启动之前在主线程中创建 QNetworkAccessManager、QSqlDatabase 等实例? Qt 在幕后做了一些工作,需要在拥有所有权的线程上下文中实例化这些工作。如果全局数据位于线程本地存储中,并且尝试访问该数据的线程没有其范围,那么您就会遇到问题!

否则,使用调试器查找竞争条件是非常困难的。这就是多线程编程如此难以正确执行的部分原因。我建议研究这些 Qt 对象在多线程中使用时的行为方式。检查您的架构,而不是尝试使用调试器找到答案。

You only see Qt code in the stack trace because the errors are occurring in the event loop (i.e. QCoreApplication::exec). I see Win32 calls related to thread local storage in your stack traces. Are you creating the QNetworkAccessManager, QSqlDatabase, etc. instances within the context of each thread that uses them or in the main thread before the others are started? Qt does some work behind the scenes that requires these to be instantiated in the context of the thread that has ownership. If global data is in thread local storage and the thread trying to access the data does not have scope to it, then you'll have problems!

Otherwise, using a debugger to find race conditions is extremely difficult. That's part what makes multi-threaded programming so hard to get right. I would advise researching how those Qt objects behave when used from multiple threads. Examine your architecture instead of trying to find an answer with the debugger.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文