dmedine
dmedine

Reputation: 1563

Qlist<QCameraInfo> causes access violation in QList destructor

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();

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

For example, if I do:

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());

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++

I am using Visual Studio 2013 (windows obviously).

Upvotes: 1

Views: 987

Answers (2)

dmedine
dmedine

Reputation: 1563

So after following Kuba's advice and running his test program in my environment with a freshly built Qt library, I got the same errors. Then I had the bright idea to run it in release mode rather than debug. Lo and behold, it worked perfectly, with both the freshly built Qt5 (x86, as it happened) and the pre-built binaries (64 bit) gotten from Qt's downloads page.

It seems that linking to the qt debug libraries was causing this behavior. I'm now linking to the non-debug libraries in debug mode and am nice and happy -- for the most part -- I am still a little annoyed that the qt libs suffixed with 'd' don't seem to work properly on my system. Nevertheless, I can move on with developing this project.

Thanks to all that commented and answered!

Upvotes: 0

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.

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.

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();
}

Upvotes: 1

Related Questions