0

I am trying to grab the names of webcams plugged into my computer and shove them into a combobox, then access the name later. Here is my code:

#include <QApplication>
#include <QComboBox>
#include <QCameraInfo>
#include <iostream>

int main(int argc, char *argv[])
{


    QApplication app{ argc, argv };
    QComboBox combo;
    QList<QCameraInfo> info = QCameraInfo::availableCameras();

    foreach(QCameraInfo i, info)
        combo.addItem(i.description());
    combo.show();

    std::cout << combo.currentText().toStdString() << std::endl;
    return app.exec();


}

The code creates and shows a combo box that has the name of a webcam that I have plugged in to the computer. It will then toss me an access violation exception in trying to print the combo box string to the console.

If I comment the cout line out, all is well, but on exit I get a Debug Assertion Failed! message:

Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)

which I take to mean I am deleting an object that has been deleted (the QString in the combobox???).

If I change the code to fill the combo box with dummies:

#include <QApplication>
#include <QComboBox>
#include <QCameraInfo>
#include <iostream>

int main(int argc, char *argv[])
{


    QApplication app{ argc, argv };

    for(int i=0; i<2; i++)
        combo.addItem(QString("la la la");
    combo.show();

    std::cout << combo.currentText().toStdString() << std::endl;
    return app.exec();


}

I get the same error on the cout, but if I comment that line out, the application exits correctly. I am using Visual Studio 2013, Windows 7, and Qt5.

dmedine
  • 1,430
  • 8
  • 25
  • Are you using Qt binaries for your compiler version? Binaries built for any other version of Visual Studio will have UB / heap corruption due to having more than 1 CRT + standard library incompatibilities. – drescherjm Jun 29 '16 at 19:33
  • I think so. Qt5Multimedia.dll is in the folder with the target. x64. – dmedine Jun 29 '16 at 19:35
  • Maybe I should build from source. I downloaded them from sourceforge. I think I probably got the right version though. I will check. What you say is consistent with the behavior I am experiencing. – dmedine Jun 29 '16 at 19:36
  • I double checked and I am quite sure I am using Qt built with msvc2013. – dmedine Jun 29 '16 at 21:31
  • Then there is some other reason for heap corruption. I can't see it in the code however. – drescherjm Jun 29 '16 at 21:37
  • Are there any other 3rd party libs / dlls that you are linking with? – drescherjm Jun 29 '16 at 21:38
  • Nope, just qt5 libs. I realize now that I did in fact build qt from source. I used the flag -platform win32-msvc2013, but I am pretty sure I did from a plain old command prompt. I am in the process of re-building using the vs2012 command prompt set up for 2013 stuff as per this: http://stackoverflow.com/questions/21476588/where-is-developer-command-prompt-for-vs2013 In a few hours I'll find out if this makes a difference. – dmedine Jun 29 '16 at 21:53
  • 1
    I can confirm that both your code snippets (with `std::cout` and without `std::cout`) execute and exit without any errors here, using Qt 5.6 with MinGw 32-bit, MSVC2013 32-bit and MSVC2013 64-bit kits. I think there is a problem with the Qt binaries you are using as pointed out by @drescherjm . . . – Mike Jun 29 '16 at 22:19
  • I just installed a fresh copy of Qt5.6.1 (downloaded the installer https://www.qt.io/download-open-source/#section-2). Same errors. Once upon a time, all of this worked. Maybe there is some other source of heap corruption that we haven't thought of. In the meantime I will try 5.6.0 etc – dmedine Jun 30 '16 at 19:04
  • As a sanity check, I compiled the code above in VS2009 – dmedine Jun 30 '16 at 20:06
  • As a sanity check, I compiled the code above (without camera stuff) in VS2008 using qt4 and the same problem occurs. I also added a std::cout from a combobox->currentText().toStdString() in another, working Qt based application (also VS2008 qt4) and everything there is lovely. What the hell is going on here? – dmedine Jun 30 '16 at 20:14

1 Answers1

0

Now it works. I kept the same source code, but completely scrapped the existing project and started a new one from scratch.

I've discovered that if I set the Runtime Library flag to Multi-Threaded DLL Debug, I will get access violation errors. If I set it to Multi-Threaded DLL, it is fine.

There may have been some other project settings that contributed, but this seems to be the main culprit.

dmedine
  • 1,430
  • 8
  • 25