文章目录

  • 摘要
  • 尝试通过加锁解决
    • lock&unlock
    • QMutexLocker
    • try_lock
  • 改变量属性
  • Q_ASSERT
  • 参考

关键字: ASSERTisDetached崩溃QVectorDetach

摘要

今天在公司填坑的时候,有随机获得了一个新的BUG,就是一直报ASSERT:”isDetached()in file xxxxxx/qvector.h line 392如下图所示。

具体代码如下图所示:

虽然知道这是典型的多线程共享数据的BUG,但是我还是不知道该怎么处理。

尝试通过加锁解决

这里我都要奔溃了,各种加锁,包括在读QVector 的时候,基本算是凡是用到了QVector 就加锁的底部,还是会崩溃,整的我整个人都开始怀疑人生了。

lock&unlock

lock

Locks the mutex. If another thread has locked the mutex then this call will block until that thread has unlocked it.

Calling this function multiple times on the same mutex from the same thread is allowed if this mutex is a recursive mutex. If this mutex is a non-recursive mutex, this function will dead-lock when the mutex is locked recursively.

unlock

Unlocks the mutex. Attempting to unlock a mutex in a different thread to the one that locked it results in an error. Unlocking a mutex that is not locked results in undefined behavior.

最开始是直接使用lock 和unlock,来实现加锁和解算,但是发现不解决问题,其实这个在上一个项目中实践是好使用的,但是在这个项目里面都开始怀疑人生了。

QMutexLocker

和面百度到一个这个锁,官方对其描述如下:

Locking and unlocking a QMutex in complex functions and statements or in exception handling code is error-prone and difficult to debug. QMutexLocker can be used in such situations to ensure that the state of the mutex is always well-defined.

QMutexLocker should be created within a function where a QMutex needs to be locked. The mutex is locked when QMutexLocker is created. You can unlock and relock the mutex with unlock() and relock(). If locked, the mutex will be unlocked when the QMutexLocker is destroyed.

我理解就是更加智能一点锁,防止我们在加锁的时候,忘记解锁。但是还是没有解决。

try_lock

Attempts to lock the mutex. This function returns true if the lock was obtained; otherwise it returns false.

This function is provided for compatibility with the Standard Library concept Lockable. It is equivalent to tryLock().

The function returns true if the lock was obtained; otherwise it returns false

This function was introduced in Qt 5.8.

到这里,我就开始怀疑是不是我加锁失败了,导致资源竞争了,但是呢,还是失败了。通过判断,发现每次都可以成功加锁。但是还是不可以解决问题。

到这里,这个问题已经折腾我一天了。还是没有头绪。

改变量属性

开始我是在函数内部又 auto了一个QVector来接收线程的QVector,就瞎几把想是不是在下一次调用函数的时候,又auto了一个,到时其他函数在访问的时候,地址变了,出现了问题,所以给了一个全局的QVector,结果还是没有效果。

Q_ASSERT

void Q_ASSERT(bool test)

Prints a warning message containing the source code file name and line number if test is false.

Q_ASSERT() is useful for testing pre- and post-conditions during development. It does nothing if QT_NO_DEBUG was defined during compilation.

Example:

 // File: div.cpp#include <QtGlobal>int divide(int a, int b){Q_ASSERT(b != 0);return a / b;}

If b is zero, the Q_ASSERT statement will output the following message using the qFatal() function:

 ASSERT: "b != 0" in file div.cpp, line 7

See also Q_ASSERT_X(), qFatal(), and Debugging Techniques.

这个,用Qt 开发,避免不了,在做大型软件开发的时候,需要对程序进行调试,以确保程序内存的运算结果符合我们的预期,在不符合预期结果的时候,直接将程序断下,以便修改,

而 Q_ASSERT是一个在调试程序过程中用到的一个宏,在程序运行时,它计算传入参数表达式的值,若表达式结果为FALSE,程序将报告错误,并终止执行,如果表达式不为0,则继续执行后面的语句,他的作用是终止程序以免导致更严重的后果,同时也便于查找错误。

到这里,就要出现我的解决方案了,不过这里要申明一点,这个是重点

我的程序不会都在最终用户哪里使用,仅仅是为了某一种证书而开发的演示程序;我所避免崩溃的有方法基本就是哪痛割那的方法这个法子仅仅能在我当前的程序的里面试用,没有任何的参考价值,在正式的项目开中,最好还是要排查一下多线程共享数据的逻辑,已经互斥锁加的位置等等,绝对不推荐我这种方法,当然,我目前使用的Qt的版本是5.12.3,交叉编译的Qt版本也是厂家提供,这里不排除可能有Qt 的BUG,不过这个可能性基本可以说没有,我看了我当前使用的代码以及5.15.2版本的代码,如下图所示

5.15.2 是直接使用了std的realloc,而我所使用的5.12.3里面试用的是Qt 自己的一个,仅此而已。所以,我的方法就是把Q_ASSER给注释掉,因为当前是演示程序,所以数据有一定的风险,是我能接受的结果。但是,这里极度不推荐,不推荐,不推荐大家怎么搞。

参考

https://blog.csdn.net/danshiming/article/details/124644757

https://forum.qt.io/topic/90180/assert-isdetached-with-multithreaded-qvectors/2

https://blog.csdn.net/y363703390/article/details/79790716


Qt QVector “isDetached()“相关推荐

  1. QT,QVector 基本用法,遍历[实例讲解]

    QVector,是Qt对所有数组的封装,比如我们想要一个int类型数组,我们原先会写int array[10],我们在Qt里可以写QVector <int>  array(10),赋值的时 ...

  2. Qt QVector 详解:从底层原理到高级用法

    目录标题 引言:QVector的重要性与简介 QVector的常用接口 QVector和std::Vector 迭代器:遍历QVector 中的元素(Iterators: Traversing Ele ...

  3. Qt QVector常见使用方法

    仅在此简单介绍QVector的一些常见函数,有兴趣的可以查下QT,在QT中介绍的很详细 构造函数,QVector的构造函数很多样化,常见的有 1 QVector() 无参的构造函数 2 3 QVect ...

  4. Qt QVector简单用法

    添加元素 QVector<QString> strArray; strArray.append("Hello"); 遍历 QVector<QString>: ...

  5. Qt —— QVector

    说明      QVector类是一个提供动态数组的模板类,提供基于索引的快速访问.        Vector优点:           1.内存连续,导致增加.查询元素效率较快.          ...

  6. 遍历qvector_转:Qt 之 QVector

    QVector类是一个提供动态数组的模板类. QVector是Qt普通容器类的一种.它将自己的每一个对象存储在连续的内存中,可以使用索引号来快速访问它们.QList.QLinkedList和 QVar ...

  7. QT QVectorQPairQString, qint64 qSort 排序

    QT的算法与容器之类的与存C++有一些区别. 头文件: #include <qalgorithms.h> //这个用于qt排序算法qSort的. #include <QVector& ...

  8. QT学习之路-资料收藏集锦

    目录 C++知识 Qt项目与文件 Qt基础类 Qt图形控件和GUI Qt图形绘制和图像处理 Qt与计算机硬件 常见问题 C++知识 C++ 枚举类型作用域的思考 C++中enum用法 C语言使用枚举类 ...

  9. Qt中的问题和方法总结

    1. QString与String的转换 //1.QString转换String QString qstr = "hello"; string str = qstr.toStdSt ...

最新文章

  1. Windows编译OpenSSL
  2. CSS3的transform:将元素旋转,缩放,移动,倾斜
  3. 浅谈 Python 中的多线程。
  4. 在什么时候需要使用“常引用”?
  5. MySQL备份失败,一波三折的问题分析和处理
  6. 美国道富java开发面试题_从事Java开发五年,面试9家拿到7家offer,1096面试+67笔试题...
  7. 20181224每日一句
  8. Android 离线文字转语音功能-TTS(Text To Speech)
  9. Win10安装Ubuntu双系统导致Win10时间有问题
  10. Notepad++的列编辑模式_小技巧
  11. 禅定是否一定要打坐,为什么?
  12. 香港TVB40年武侠情侣
  13. Python网络爬虫之数据解析(一)
  14. 河南的抗疫英雄(C语言嘞)
  15. RxJava2 背压
  16. Jenkins配置报错-Problem accessing /jenkins/. Reason
  17. 阿拉伯数字改为汉字的大写
  18. 【C++】setw()函数
  19. [EDI实施案例] 耐世特/Nexteer DESADV报文的业务解读
  20. 编程小知识之 Lua split 函数

热门文章

  1. C语言GUI编程之数字记忆游戏——项目构建
  2. 拉格朗日Lagrange插值多项式
  3. JanusGraph ,生产环境安装
  4. php万年历源代码!源代码![上一年、上一月、下一月、下一年、附加当天日期加背景颜色]-私聊源码
  5. freeRTOS — 软件定时器的使用
  6. nginx 启动、重启、配置重新加载
  7. IDEA 注释模板,这样配置才够逼格!
  8. [面经整理] 机器/深度学习补充篇
  9. QT QColor颜色选择器并获取rgb值
  10. 海拥,一枚热爱分享技术与快乐的博主