0


QT中Qthread线程彻底销毁的实例与注意事项(防止线程资源内存泄露)

注意:释放线程的时候触发线程的信号与槽连接时的连接类型参数一定要是Qt::ConnectionType::DirectConnection,否则线程销毁不了会造成内存泄露,通过任务栏开启资源管理器可监视cup的线程数变化情况。

QThread* th=new QThread();

      Work* mywork=new Work ();
       mywork->moveToThread(th);
    
       SigManager sm;
       connect(th, &QThread::finished, mywork, &QObject::deleteLater);//线程结束释放工作类
       connect(th, &QThread::finished, th, &QObject::deleteLater);//线程结束释放线程指针
       QObject::connect(mywork,&Work::SigExitThread,th,&QThread::exit,Qt::ConnectionType::DirectConnection);//退出线程
        QObject::connect(mywork,&Work::SigExitThread,th,&QThread::quit,Qt::ConnectionType::DirectConnection);//退出线程
         QObject::connect(mywork,&Work::SigExitThread,th,&QThread::terminate,Qt::ConnectionType::DirectConnection);//退出线程

QObject::connect(&sm, &SigManager::Sig_ExcuteFunction,mywork,&Work::Function1);

th->start();

标签: qt ui 开发语言

本文转载自: https://blog.csdn.net/u011555996/article/details/128431594
版权归原作者 luckyone906 所有, 如有侵权,请联系我们删除。

“QT中Qthread线程彻底销毁的实例与注意事项(防止线程资源内存泄露)”的评论:

还没有评论