Qt,QMutex:破坏锁定的互斥锁然后应用程序崩溃

时间:2016-05-22 10:03:47

标签: qt qtconcurrent

我使用QNetworkAccessManager进行http操作(get,post等...)。我在paralel中运行了一些“get”操作。为此,我使用QtConcurrent :: run(this,& RestWebservice :: GetHTTPData)进行多HTTP操作。 我的问题是当我在HTTP操作未完成之前关闭应用程序时,应用程序崩溃。应用程序输出写入此行 QMutex:销毁锁定的互斥锁然后写程序意外完成。 我的客人问题出现在这一行

void RestWebservice::get()
{
 //  mutex.lock();
  m_networkManager.get(m_networkrequest);
//  mutex.unlock();


}

但我不确定,因为QtCreater调试器不像VS.By方式,GetHTTPData属于不同类。

MY CODE for start network操作:(MobileOperation.cpp)。为exapmle getUserAccount metod启动一个http操作。

void MobileOperations::getWorkOrderListT(int ekipId) {


  MyGlobal::Metods metod=MyGlobal::EkipIsEmriListesi;
    QString parameters="{EkipId}";
    QMap<QString,QVariant> paramlist;

    paramlist["EkipId"]=ekipId;

    GetHTTPData(metod,parameters,paramlist);

    if(m_workorder.IsSuccess==true)
    {

        // emit successupdatewo();

            if(m_workorder.workorders.count()>0)
            {
                InsertWo(json.workorder->workorders);
             emit processstop("İş Emri Listesi Güncellendi");




             // QThread::sleep(2);
            }
           else
            {
                emit processstop(json.workorder->ReturnMessage);
            }

            emit successworkstart();

    }
    else
    {
        emit processstop("Bağlantı Başarısız Oldu");
    }


}
  void MobileOperations::getUserAccount(QString kullaniciAdi, QString sifre,bool isremember)
{
    json.user=m_user;

    QtConcurrent::run(this,&MobileOperations::getUserAccountT,kullaniciAdi,sifre,isremember);
   // getUserAccountT(kullaniciAdi,sifre,isremember);
    processstart("Baglaniyor");




}






void MobileOperations::GetHTTPData(MyGlobal::Metods MetodName, QString Parameters, QMap<QString, QVariant> paramlist)
{

    try
    {
    parameter=new HttpRequest();
    parameter->url=m_url;
    parameter->metodname=MetodName;
    parameter->resource=m_path;
    parameter->appid=m_appid;
    parameter->apppass=m_apppass;
    parameter->parametersname=Parameters;
    parameter->params=paramlist;
    rest= new RestWebservice(parameter->GenerateHTTPQuery(),MetodName);
   // json=new JSonParser();
  //  loop=new QEventLoop();

     loop=new QEventLoop();
     QObject::connect(rest,SIGNAL(sendhttpdata(QByteArray,MyGlobal::Metods)),&json,SLOT(onGetData(QByteArray,MyGlobal::Metods)));
     QObject::connect(&json,SIGNAL(serilazitionCompleted()),loop,SLOT(quit()));
     rest->get();
     loop->exec();
    }

    catch(std::string &exp)
    {
        qDebug()<<"Sonlandırıldı";
    }

}

MY CODE of classes For HTTP operatins:

#include "restwebservice.h"
#include <QJsonDocument>
#include<QJsonArray>
#include <QJsonObject>
#include<QJsonValue>
#include<QList>
#include <QThread>




RestWebservice::RestWebservice(QNetworkRequest request,
                               MyGlobal::Metods metod,
                               QObject* parent):QObject(parent),m_networkrequest(request),m_metodname(metod)
{


    connect(&m_networkManager, SIGNAL(finished(QNetworkReply*)),this, SLOT(onResult(QNetworkReply*)));
//    connect(&m_networkManager,SIGNAL())

}

void RestWebservice::get()
{
  //  mutex.lock();
  m_networkManager.get(m_networkrequest);
//  mutex.unlock();


}

void RestWebservice::post(QString request)
{

   QByteArray requestA= request.toUtf8();
    m_networkManager.post(m_networkrequest,requestA);
}

void RestWebservice::onResult(QNetworkReply* reply)
{
    try
    {
      if (reply->error() != QNetworkReply::NoError)
        {



              qDebug()<<reply->error()<<":"<<reply->errorString();
               MyGlobal::NetworkStatus=reply->errorString();
              emit sendhttpdata(m_data,m_metodname);
              return;

             //  throw(reply->errorString().toStdString());



        }



         QByteArray data = reply->readAll();

         reply->deleteLater();
         m_data=data;

         MyGlobal::NetworkStatus="Tablolar Yüklendi";
        emit sendhttpdata(m_data,m_metodname);


}

    catch(std::string exp)
    {
        qDebug()<<"Exception:"<<QString::fromStdString(exp);


    }

   catch(std::exception &exp)
    {
        qDebug()<<"Exception:"<<QString::fromStdString(exp.what());
    }


}

void RestWebservice::onError()
{


    qDebug()<<"Hata VAR";
}


HttpRequest::HttpRequest(QObject *parent) :
    QObject(parent)
{

}

QNetworkRequest HttpRequest::GenerateHTTPQuery()
{

  // QString path="";
   QString path=QString("/%1/%2/%3/%4/%5").arg(resource).arg(MyGlobal::getMetodName(metodname)).arg(appid).arg(apppass).arg(parametersname);
   foreach (QString param, params.keys()) {

       path.replace("{"+param+"}",params[param].toString());

   }

    QUrl m_url(url);
    m_url.setPath(path);
    m_request.setUrl(m_url);
    m_request.setRawHeader("Content-Type","application/json;charset=utf-8");
  //  m_request.setRawHeader("SOAPAction","http://tempuri.org/IMobileClient/UserAuth");
    qDebug()<<m_url.url();


    return m_request;

}


QNetworkRequest HttpRequest::GenerateHTTPQueryPost()
{

  // QString path="";
   QString path=QString("/%1/%2").arg(resource).arg(MyGlobal::getMetodName(metodname));
    QUrl m_url(url);
    m_url.setPath(path);
    m_request.setUrl(m_url);
    m_request.setRawHeader("Content-Type","application/json;charset=utf-8");
  //  m_request.setRawHeader("SOAPAction","http://tempuri.org/IMobileClient/UserAuth");
    qDebug()<<m_url.url();


    return m_request;

}

1 个答案:

答案 0 :(得分:1)

你是互斥体是你班上的一员吗?在这种情况下,互斥锁在解锁之前被破坏(因为我认为包含类超出范围),这会导致您看到的消息。当类被破坏时,会调用互斥体的析构函数,同时保持锁定。这是个问题。通常,您必须找到一种在网络请求期间无限期阻止的方法。