Qlist< QCameraInfo>导致QList析构函数中的访问冲突 [英] Qlist<QCameraInfo> causes access violation in QList destructor

查看:381
本文介绍了Qlist< QCameraInfo>导致QList析构函数中的访问冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Qt5用c ++编写一个视频捕获应用程序.我正在遵循他们的示例代码,并查看用于获取相机信息的文档: http://doc.qt.io/qt-5/qcamerainfo.html

I am writing a video grabbing application in c++ using Qt5. I am following their example code and looking at the documentation for getting the camera info: http://doc.qt.io/qt-5/qcamerainfo.html

我遇到的问题是,在使用规定的技术获取相机数据后(非常有效):

The problem I have is that after I use the prescribed technique for getting camera data (which works perfectly):

QList<QCameraInfo>cameraInfos = QCameraInfo::availableCameras();

每当cameraInfos超出范围时,我都会收到一个Access violation错误.

I get an Access violation error whenever cameraInfos goes out of scope.

例如,如果我这样做:

void readDeviceInfo(void) {

    // Camera devices:
    QList<QCameraInfo>cameraInfos = QCameraInfo::availableCameras()
    for (QList<QCameraInfo>::Iterator it = cameraInfos.begin();
    it != cameraInfos.end(); ++it)
        std::cout << it->description().toStdString().c_str() << std::endl; 


}

此函数返回时发生崩溃.如果我这样做:

The crash occurs on the return of this function. If I do:

foreach(const QCameraInfo &ci, QCameraInfo::availableCameras());

崩溃发生在foreach循环的评估中.同样,如果我将QList<QCameraInfo> cameraInfos声明为类中的字段,则在销毁该类时会发生崩溃.我的调用堆栈的输出对此进行了验证:

The crash occurs in the evaluation of the foreach loop. Likewise, if I declare QList<QCameraInfo> cameraInfos as a field in a class, the crash happens when the class is destroyed. This is verified by the output of my call stack:

    ntdll.dll!000000007750eef1()    Unknown
    kernel32.dll!00000000773c1a0a() Unknown
>   VideoCapture.exe!free(void * pBlock) Line 51    C
    VideoCapture.exe!QCameraInfo::`scalar deleting destructor'(unsigned int)    C++
    VideoCapture.exe!QList<QCameraInfo>::node_destruct(QList<QCameraInfo>::Node * from, QList<QCameraInfo>::Node * to) Line 484 C++
    VideoCapture.exe!QList<QCameraInfo>::dealloc(QListData::Data * data) Line 857   C++
    VideoCapture.exe!QList<QCameraInfo>::~QList<QCameraInfo>() Line 817 C++

我正在使用Visual Studio 2013(显然是Windows).

I am using Visual Studio 2013 (windows obviously).

推荐答案

您需要自己编译Qt,然后在调试器下运行测试用例,然后查看崩溃的地方.您还需要一个最小的,独立的测试用例-这必须是问题的一部分( SSCCE ).实际上,您更有可能破坏其他地方的内存,而看到的故障则是堆损坏的结果,而不是Qt错误.

You need to compile Qt yourself, then run your test case under a debugger and see where it crashes. You also need a minimal, self-contained test case for this - and that must be the part of the question (SSCCE). As it is, it's more likely that you're corrupting memory elsewhere and the failure you're seeing is the outcome of a corrupted heap, not a Qt bug.

侧边栏:您需要精通Qt Creator中的小示例.可以说,Qt Creator附带的模板并不是很好.您可以使用此模板,该模板可在其他项目"->简单qmake"中找到,制作快速的原型.

Sidebar: You need to be proficient in running small examples in Qt Creator. Arguably, the templates Qt Creator comes with aren't very good for that. You can use this template, available as Other Projects->Simple qmake, to make quick prototypes.

对于在OS X 10.9和Windows 10/VS 2015上当前Qt上的1个摄像头,以下操作对我来说很好.您使用的std::cout是红色鲱鱼,也可以使用qDebug().

The following works fine for me with 1 camera on current Qt on both OS X 10.9 and Windows 10/VS 2015. The std::cout you're using is red herring, you can use qDebug() as well.

// https://github.com/KubaO/stackoverflown/tree/master/questions/camlist-37603946
#include <QtWidgets>
#include <QtMultimedia>

int main(int argc, char ** argv) {
   QApplication app{argc, argv};
   QComboBox combo;
   QObject::connect(&combo, &QComboBox::currentTextChanged, [&]{
      std::cout << combo.currentText().toStdString() << std::endl;
   });
   for (auto const & info : QCameraInfo::availableCameras())
      combo.addItem(info.description());
   combo.show();
   return app.exec();
}

这篇关于Qlist&lt; QCameraInfo&gt;导致QList析构函数中的访问冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆