Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Threading in QT in c++

Threading in QT in c++

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 4 Posters 986 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    shashi prasad
    wrote on last edited by kshegunov
    #1

    Hi All,

    i have created two thread.
    every thread has its own local variable mx_Server,mxx_Account.

    void Command_clicked()
    {
        for(int i=0;i<ip.size();i++){
           
            std::thread threadCmdExecutor(sendThread, ipList[i]);
            threadCmdExecutor.detach();
    
        }
    
    }
    
    
    sendThread(std::string val){ 
         std::stringstream ss(val);
         std::string item;
        std::vector<std::string> token;
         while(getline(ss,item,';')){
             token.push_back(item);
         }
         std::string ipAddrs=token[0];
         std::string dNames=token[1];
        ServerAccount  *mxx_Account = new ServerAccount(ipAddrs,dNames);
        Server *mx_Server = new Server(mxx_Account);
       bool status= mx_Server->connection();
       if(status){
           qDebug()<<"-connected------------";
            
        }
       else{
    
        }
       delete mx_Server;
       delete mxx_Account;
        return 0;
    }
    

    when i am not deleteting these variable it is working fine.

    when using

    delete mx_Server;
    delete mxx_Account;
    

    then one thread works fine and other crashes.

    reason is while other thread start using these variable then previous thred delete it.

    infact every thread has its own local variable then why crashes.

    please suggest me.

    Thanks me.

    [Added code tags ~kshegunov]

    jsulmJ 1 Reply Last reply
    0
    • Vinod KuntojiV Offline
      Vinod KuntojiV Offline
      Vinod Kuntoji
      wrote on last edited by
      #2

      @shashi-prasad ,

      connect(mx_Server, SIGNAL(finished()), mx_Server, SLOT(deleteLater()), Qt::DirectConnection);
      connect(mxx_Account, SIGNAL(finished()), mxx_Account, SLOT(deleteLater()), Qt::DirectConnection);

      C++, Qt, Qt Quick Developer,
      PthinkS, Bangalore

      1 Reply Last reply
      0
      • S shashi prasad

        Hi All,

        i have created two thread.
        every thread has its own local variable mx_Server,mxx_Account.

        void Command_clicked()
        {
            for(int i=0;i<ip.size();i++){
               
                std::thread threadCmdExecutor(sendThread, ipList[i]);
                threadCmdExecutor.detach();
        
            }
        
        }
        
        
        sendThread(std::string val){ 
             std::stringstream ss(val);
             std::string item;
            std::vector<std::string> token;
             while(getline(ss,item,';')){
                 token.push_back(item);
             }
             std::string ipAddrs=token[0];
             std::string dNames=token[1];
            ServerAccount  *mxx_Account = new ServerAccount(ipAddrs,dNames);
            Server *mx_Server = new Server(mxx_Account);
           bool status= mx_Server->connection();
           if(status){
               qDebug()<<"-connected------------";
                
            }
           else{
        
            }
           delete mx_Server;
           delete mxx_Account;
            return 0;
        }
        

        when i am not deleteting these variable it is working fine.

        when using

        delete mx_Server;
        delete mxx_Account;
        

        then one thread works fine and other crashes.

        reason is while other thread start using these variable then previous thred delete it.

        infact every thread has its own local variable then why crashes.

        please suggest me.

        Thanks me.

        [Added code tags ~kshegunov]

        jsulmJ Online
        jsulmJ Online
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @shashi-prasad Can you show the stack trace when it is crashing?
        Also what do the destructors of ServerAccount and Server do?

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        1
        • jeremy_kJ Offline
          jeremy_kJ Offline
          jeremy_k
          wrote on last edited by
          #4

          It looks like there's been a formatting error, ie std::vectorstd::string token;
          And other than a single use of qDebug(), Qt related functionality isn't invoked. It's easier to help with more complete information.

          At a glance, there's an assumption that ipAddrs is the first line of the string passed to sendThread(), and dNames is the second. There's no validation that either is in the right position, or that a first and second line even exist. Destructors relying on valid values could be the culprit.

          Asking a question about code? http://eel.is/iso-c++/testcase/

          1 Reply Last reply
          2

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved