unique_ptr and ERROR: Run-Time Check Failure #2 - Stack around the variable 'w' was corrupted in QT

224 Views Asked by At

I'm writing an app in QT. I'd like to use QButtonGroup, so I declear it in header file of user interface class:

std::unique_ptr<QButtonGroup> examTypeSelectGroup;

But when I close application, I recive the error:

Run-Time Check Failure #2 - Stack around the variable 'w' was corrupted.

in last line of main.cpp file, which was a mian file of a project. Call stack when error occures have only main():

>   OCT_main.exe!main(int argc, char * * argv) Line 14
    [External Code]

Error occures both, when I tried to use object, and when I don't use it (just declare it). Have you got any idea, what happend.

main.cpp:

1   #include "oct_main.h"
2
3   #include <QApplication>
4
5   int main(int argc, char *argv[])
6   {
7       QApplication a(argc, argv);
8
9       //run window
10      OCT_main w;
11      w.show();
12
13      return a.exec();
14  }

EDIT: using QButtonGroup examTypeSelectGroup; make error during destruct a button group, so it was bad way too.

1

There are 1 best solutions below

0
On

First rule of Qt (more precisely, any QObject derived class), never call delete on it.

So a basic unique_ptr like you are using is probably a bad idea, you either have to write a custom deleter to call deleteLater(), or use Qt's own smart pointers

https://wiki.qt.io/Smart_Pointers

And even then, I don't think they provide smart pointers working with QObject derived classes.

Qt has an older style memory management, so it doesn't mix well with the new standard stuff out of the box.