在使用Qt时创建了一个camera线程用于捕获摄像头图像,其中捕获全部由opencv进行自动管理,我仅传输一个qimage至主线程UI并显示,子窗口window_a也由qt designer设计,Qt自动管理生成代码,主窗口app0的析构函数如下,已经delete window_a, delete camera,在closeEvent执行并确认按钮 , vld仍然进行大量内存报错
camera线程仅包含videocapture,所以析构函数为 if (!capture.isOpened())capture.release();
子窗口window_a.cpp
camera_index::camera_index(QWidget *parent)
: QWidget(parent),
ui(new Ui::camera_index)//指针调用必备!
{
ui->setupUi(this);
}
camera_index::~camera_index()
{
delete ui;
}
主窗口cpp app0
app0::app0(QWidget *parent)
: QMainWindow(parent)
{
ui->setupUi(this);
window_a = new camera_index();//指针实例化
camera = new Camera();
camera->moveToThread(&thread); //将camera对象放在子线程,不推荐放在主线程执行。
...connect(QImage传输至UI界面)...
}
app0::~app0()
{
writeSettings();
delete ui;
delete window_a;
delete camera;
}
在closeEvent执行后vld仍然进行大量内存报错
---------- Block 154 at 0x02D46730: 16 bytes ----------
Leak Hash: 0x7E761387, Count: 1, Total 16 bytes
Call Stack (TID 18968):
ucrtbased.dll!malloc()
f:\dd\vctools\crt\vcstartup\src\heap\new_scalar.cpp (19): app0.exe!operator new() + 0x9 bytes
d:\myproject\qt_test\app0\debug\uic\ui_camera_index.h (213): app0.exe!Ui_camera_index::setupUi() + 0x7 bytes
d:\myproject\qt_test\app0\camera_index.cpp (9): app0.exe!camera_index::camera_index()
d:\myproject\qt_test\app0\app0.cpp (14): app0.exe!app0::app0() + 0x21 bytes
d:\myproject\qt_test\app0\main.cpp (15): app0.exe!main() + 0xD bytes
f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (64): app0.exe!invoke_main() + 0x1B bytes
f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (253): app0.exe!__scrt_common_main_seh() + 0x5 bytes
f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (296): app0.exe!__scrt_common_main()
f:\dd\vctools\crt\vcstartup\src\startup\exe_main.cpp (17): app0.exe!mainCRTStartup()
KERNEL32.DLL!BaseThreadInitThunk() + 0x19 bytes
ntdll.dll!__RtlUserThreadStart() + 0x2F bytes
ntdll.dll!_RtlUserThreadStart() + 0x1B bytes
Data:
1C 96 73 00 48 5F CD 02 74 96 73 00 00 00 00 00 ..s.H_.. t.s.....
Visual Leak Detector detected 162 memory leaks (10240 bytes).
Largest number used: 10568 bytes.
Total allocations: 11132 bytes.
Visual Leak Detector is now exiting.
The program '[0x55A0] app0.exe' has exited with code -1073741510 (0xc000013a).
进行跟踪之后跳转至如下报错:
horizontalLayout_9 = new QHBoxLayout();
以及
window_a = new camera_index();//指针实例化
以及
app0 w; //main函数实例化
主窗口 closeEvent如下
void app0::closeEvent(QCloseEvent *event) //在关闭事件中退出子线程,并关闭相机,结束应用程序
{
int ret = QMessageBox::information(this, "System Quit", "退出程序?", QMessageBox::Yes | QMessageBox::No);
if (ret == QMessageBox::Yes) {
writeSettings();
thread.terminate();
camera->close();
window_a->close();
//app0::~app0();
event->accept();
qApp->exit();
}
else
event->ignore();
}
尝试过在主窗口closeEvent处直接执行~app0()及delete camera, window_a但是关闭窗口点叉并确定会直接break,应该是执行两次delete导致的异常,但是执行了析构执行了delete仍然会导致内存泄漏?
请问是什么原因导致的没有camera线程及window_a正确释放,析构函数不正确?还是closeEvent重写不正确?