解决一个Qt程序崩溃的问题

文章讲述了在Qt中使用QProcess并发启动进程时遇到的致命错误,尤其是在主线程中主动退出进程可能导致程序崩溃。作者怀疑是由于进程未正确结束导致主线程阻塞。建议避免在主线程中调用QProcess的waitForFinished(),以防UI冻结。
摘要由CSDN通过智能技术生成

当Qt崩溃时,使用Qt Creator调试,也无法定位到是哪个地方除了问题。只发现日志报出一个Fatal。

Fatal:  2023-11-05 00:16:26 [io\qprocess_win.cpp:826]  ASSERT: "pid" in file io\qprocess_win.cpp, line 826

触发的情况,就是使用QProcess同时启动多个进程时,主动退出进程导致的。
定位到代码的位置在:

void zip7z::terminal()
{
    process.kill();
    process.waitForFinished();
}

这个 terminal 是在主线程被调用的。在process主动退出前,调用这个接口,就会卡死。
注释掉 process.waitForFinished(); 即可解决这个问题。怀疑是进程没有被kill掉,导致主线程一直阻塞,然后崩溃的。仅仅是怀疑,但我没有证据…

Qt官方文档对于这个接口的解释:

Blocks until the process has finished and the finished() signal has been emitted, or until msecs milliseconds have passed.
Returns true if the process finished; otherwise returns false (if the operation timed out, if an error occurred, or if this QProcess is already finished).
This function can operate without an event loop. It is useful when writing non-GUI applications and when performing I/O operations in a non-GUI thread.
Warning: Calling this function from the main (GUI) thread might cause your user interface to freeze.
If msecs is -1, this function will not time out

总之,就是不要在主线程中使用这个接口,否则会导致UI卡主。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值