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. Object initialize
Forum Updated to NodeBB v4.3 + New Features

Object initialize

Scheduled Pinned Locked Moved Unsolved General and Desktop
16 Posts 3 Posters 666 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 satyanarayana143

    channelinfoobj *m_nchannelinfoobj[4];
    for (int i = 0; i < 4; ++i)
    {
    m_nchannelinfoobj[i] = nullptr;
    m_nchannelinfoobj[i] = new channelinfoobj ();
    }

    this loop function will call every 5 sec then i have to validate new is already created or not . m_nchannelinfoobj[i] = nullptr; every time it will be null.
    m_nchannelinfoobj[i] using by other .

    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on last edited by jsulm
    #6

    @satyanarayana143 said in Object initialize:

    every time it will be null

    No, because you're assigning it a valid pointer just after assigning nullptr (I have no idea why you're doing this!), so it will be always != nullptr (unless you assign nullptr somewhere else):

    for (int i = 0; i < 4; ++i)
    {
        m_nchannelinfoobj[i] = nullptr; // Why?! Next line you're assigning a pointer to it.
        m_nchannelinfoobj[i] = new channelinfoobj (); // Now it is not nullptr any-more
    }
    

    "then i have to validate new is already created or not " - isn't that as simple as:

    if (m_nchannelinfoobj[i]) {
        // Do something
    }
    

    ?

    Can you please explain better what you want to achieve and what the problem is?

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

    1 Reply Last reply
    1
    • S Offline
      S Offline
      satyanarayana143
      wrote on last edited by
      #7

      void EdgeClient::processPendingDatagrams()
      {
      struct uftp_h *header;
      int packetlen = 0;

      header = (struct uftp_h *)malloc(sizeof(struct uftp_h));
      
      struct ChannelInfo_Recieve* channelData;
      
      QByteArray datagram;
      datagram.resize(udpSocket->pendingDatagramSize());
      udpSocket->readDatagram(datagram.data(),datagram.size());
      
      unsigned char *pCmdBuffer = (unsigned char*)datagram.data();
      
      memcpy(header,pCmdBuffer,sizeof(struct uftp_h));
      packetlen+= sizeof(struct uftp_h);
      
      qDebug() <<  "Socket Read Bytes:" << datagram.size() << "####### Header Info  #########" << "Vernum:" << header->version
                << "Announce:" << header->func <<"channel Count:" << header->channelcnt ;
      
      channelInfoStruct *m_nchannelInfoStruct[header->channelcnt];
      
      for(int i = 0;i < header->channelcnt;i++)
      {
          m_nchannelInfoStruct[i] = new channelInfoStruct();
          channelData = (struct ChannelInfo_Recieve *)malloc(sizeof(struct ChannelInfo_Recieve));
          uint16_t *pgrp = (uint16_t *)malloc(channelData->groupsize*sizeof(uint16_t));
      
          if(packetlen+sizeof(struct ChannelInfo_Recieve) > MAX_BUFFERSIZE)
          {
              qDebug() << "######### Receive Buffer size Exceeded ##############";
              exit(0);
          }
      
          memcpy(channelData,pCmdBuffer+packetlen,sizeof(struct ChannelInfo_Recieve));
          packetlen+= sizeof(struct ChannelInfo_Recieve);
      
          struct in_addr Inaddr = {channelData->InputIPAddress};
          struct in_addr outaddr = {channelData->outputIPAddress};
      
          qDebug() << "######################################################";
      
          qDebug() <<"ChannelType:" << channelData->channelType << "OutputPort:" << channelData->OutputPort
                  <<"Inputport:" << channelData->Inputport  << "GroupSize:" << channelData->groupsize
                 <<"InputIPAddress:"<<inet_ntoa(Inaddr)<< "outputIPAddress:"<<inet_ntoa(outaddr) << endl;
      
      
          qDebug() << "OutputPort:" << channelData->OutputPort;
      
          if(packetlen+(channelData->groupsize*sizeof(uint16_t)) > MAX_BUFFERSIZE)
          {
              qDebug() << "######### Receive Buffer size Exceeded ##############";
              exit(0);
          }
      
          memcpy(pgrp,pCmdBuffer+packetlen,channelData->groupsize*sizeof(uint16_t));
          packetlen+= channelData->groupsize*sizeof(uint16_t);
      
      
          //        for(int gid = 0;gid < schData->groupsize;gid++)
          //        {
          //            qDebug() << "sGroupID:" << pgrp[gid];
          //        }
      
          QString m_SystemIp ;
          struct ifaddrs *ifaddr, *ifa;
          int family, s;
          char host[NI_MAXHOST];
      
          if (getifaddrs(&ifaddr) == -1)
          {
              perror("getifaddrs");
              exit(EXIT_FAILURE);
          }
          for (ifa = ifaddr; ifa != nullptr; ifa = ifa->ifa_next)
          {
              if (ifa->ifa_addr == nullptr)
                  continue;
      
              s = getnameinfo(ifa->ifa_addr,sizeof(struct sockaddr_in),host, NI_MAXHOST, nullptr,0, NI_NUMERICHOST);
      
              if( ((strcmp(ifa->ifa_name,"wlan0")==0)&&(ifa->ifa_addr->sa_family==AF_INET)) ||  ((strcmp(ifa->ifa_name,"enp3s0")==0)&&(ifa->ifa_addr->sa_family==AF_INET))
                      || ((strcmp(ifa->ifa_name,"eth0")==0)&&(ifa->ifa_addr->sa_family==AF_INET)))
              {
                  if (s != 0)
                  {
                      qDebug("getnameinfo() failed: %s\n", gai_strerror(s));
                      exit(EXIT_FAILURE);
                  }
                  qDebug("Interface : %s ",ifa->ifa_name );
                  qDebug("Address : %s", host);
      
                  m_SystemIp = host;
                  qDebug() << "m_SystemIp:" << m_SystemIp;
              }
          }
      
          freeifaddrs(ifaddr);
      
      
          if(channelData->channelType == FILETYPE)
          {
      
              if(m_nchannelInfoStruct[i]->FilePublicIp == inet_ntoa(Inaddr) && m_nchannelInfoStruct[i]->FileOutputport == channelData->OutputPort)
              {
                  qDebug() << "########## File MultiCast IpAddress and port Used i ############";
              }
              else
              {
                  qDebug() << "########## File MultiCast IpAddress and Port not Used i ############";
      
                  //Store File PublicMulticast IP and Port
                  if (m_nchannelInfoStruct[i]->m_nchannelProcess.state() == QProcess::NotRunning)
                  {
                      m_nchannelInfoStruct[i]->FilePublicIp  = inet_ntoa(Inaddr);
                      m_nchannelInfoStruct[i]->FileOutputport = channelData->OutputPort;
      
                      qDebug().noquote() << "############ File Process not Running i ##############" << i << "FileOutputport:"
                                         << m_nchannelInfoStruct[i]->FileOutputport << "FilePublicIp:" << m_nchannelInfoStruct[i]->FilePublicIp;
      
                      QString program = qApp->applicationDirPath()+"/uftpd";
      
      
                      QStringList arguments;
                      arguments << "-M"<< m_nchannelInfoStruct[i]->FilePublicIp << "-p" << QString::number(m_nchannelInfoStruct[i]->FileOutputport) << "-D" << RECEIVE_FILE_PATH;
                      qDebug() <<"File Program:"<< program <<"File arguments:" << arguments;
                      m_nchannelInfoStruct[i]->m_nchannelProcess.start(program,arguments);
                  }
                  else
                  {
                      qDebug().noquote() << "############ File Process Already Running i ##############";
                  }
      
              }
          }
          else if(channelData->channelType == LIVESTREAMTYPE)
          {
              if (m_nchannelInfoStruct[i]->m_nchannelProcess.state() == QProcess::NotRunning)
              {
                  qDebug().noquote() << "############ OnStream Process not Running j ##############";
                  if(m_nchannelInfoStruct[i]->onStreamIP == inet_ntoa(outaddr) && m_nchannelInfoStruct[i]->onStreamport ==  channelData->OutputPort)
                  {
                      qDebug() << "############## Stream Ip and Port Already Used ###############";
                  }
                  else
                  {
                      m_nchannelInfoStruct[i]->onStreamIP   = inet_ntoa(outaddr);
                      m_nchannelInfoStruct[i]->onStreamport =  channelData->OutputPort;
                  }
                  QString Streamfeed = "feed"+QString::number(i+1);
                  QStringList  argumentsList ;
                  if(m_nchannelInfoStruct[i]->onStreamIP >= "224.0.0.0 " && m_nchannelInfoStruct[i]->onStreamIP <= "239.255.255.255")
                  {
                      argumentsList << m_nchannelInfoStruct[i]->onStreamIP << QString::number(m_nchannelInfoStruct[i]->onStreamport)
                                    << Streamfeed;
                  }
                  else
                  {
                      argumentsList << m_nchannelInfoStruct[i]->onStreamIP << QString::number(m_nchannelInfoStruct[i]->onStreamport)
                                    << Streamfeed;
                  }
      
                  QString program = qApp->applicationDirPath()+"/StreamTest.sh";
                  qDebug() <<"Porcess and Arguments" <<  program << argumentsList;
                  m_nchannelInfoStruct[i]->m_nchannelProcess.start(program,argumentsList);
              }
          }
          else
          {
              qDebug() << "########### Invalid Channel Type ############";
          }
      
      }
      

      }

      this function will call every 5 sec once

      m_nchannelInfoStruct[i]->m_nchannelProcess.start() it will be running state if c create new it will be destroyed what is running in process.

      jsulmJ 1 Reply Last reply
      0
      • S satyanarayana143

        void EdgeClient::processPendingDatagrams()
        {
        struct uftp_h *header;
        int packetlen = 0;

        header = (struct uftp_h *)malloc(sizeof(struct uftp_h));
        
        struct ChannelInfo_Recieve* channelData;
        
        QByteArray datagram;
        datagram.resize(udpSocket->pendingDatagramSize());
        udpSocket->readDatagram(datagram.data(),datagram.size());
        
        unsigned char *pCmdBuffer = (unsigned char*)datagram.data();
        
        memcpy(header,pCmdBuffer,sizeof(struct uftp_h));
        packetlen+= sizeof(struct uftp_h);
        
        qDebug() <<  "Socket Read Bytes:" << datagram.size() << "####### Header Info  #########" << "Vernum:" << header->version
                  << "Announce:" << header->func <<"channel Count:" << header->channelcnt ;
        
        channelInfoStruct *m_nchannelInfoStruct[header->channelcnt];
        
        for(int i = 0;i < header->channelcnt;i++)
        {
            m_nchannelInfoStruct[i] = new channelInfoStruct();
            channelData = (struct ChannelInfo_Recieve *)malloc(sizeof(struct ChannelInfo_Recieve));
            uint16_t *pgrp = (uint16_t *)malloc(channelData->groupsize*sizeof(uint16_t));
        
            if(packetlen+sizeof(struct ChannelInfo_Recieve) > MAX_BUFFERSIZE)
            {
                qDebug() << "######### Receive Buffer size Exceeded ##############";
                exit(0);
            }
        
            memcpy(channelData,pCmdBuffer+packetlen,sizeof(struct ChannelInfo_Recieve));
            packetlen+= sizeof(struct ChannelInfo_Recieve);
        
            struct in_addr Inaddr = {channelData->InputIPAddress};
            struct in_addr outaddr = {channelData->outputIPAddress};
        
            qDebug() << "######################################################";
        
            qDebug() <<"ChannelType:" << channelData->channelType << "OutputPort:" << channelData->OutputPort
                    <<"Inputport:" << channelData->Inputport  << "GroupSize:" << channelData->groupsize
                   <<"InputIPAddress:"<<inet_ntoa(Inaddr)<< "outputIPAddress:"<<inet_ntoa(outaddr) << endl;
        
        
            qDebug() << "OutputPort:" << channelData->OutputPort;
        
            if(packetlen+(channelData->groupsize*sizeof(uint16_t)) > MAX_BUFFERSIZE)
            {
                qDebug() << "######### Receive Buffer size Exceeded ##############";
                exit(0);
            }
        
            memcpy(pgrp,pCmdBuffer+packetlen,channelData->groupsize*sizeof(uint16_t));
            packetlen+= channelData->groupsize*sizeof(uint16_t);
        
        
            //        for(int gid = 0;gid < schData->groupsize;gid++)
            //        {
            //            qDebug() << "sGroupID:" << pgrp[gid];
            //        }
        
            QString m_SystemIp ;
            struct ifaddrs *ifaddr, *ifa;
            int family, s;
            char host[NI_MAXHOST];
        
            if (getifaddrs(&ifaddr) == -1)
            {
                perror("getifaddrs");
                exit(EXIT_FAILURE);
            }
            for (ifa = ifaddr; ifa != nullptr; ifa = ifa->ifa_next)
            {
                if (ifa->ifa_addr == nullptr)
                    continue;
        
                s = getnameinfo(ifa->ifa_addr,sizeof(struct sockaddr_in),host, NI_MAXHOST, nullptr,0, NI_NUMERICHOST);
        
                if( ((strcmp(ifa->ifa_name,"wlan0")==0)&&(ifa->ifa_addr->sa_family==AF_INET)) ||  ((strcmp(ifa->ifa_name,"enp3s0")==0)&&(ifa->ifa_addr->sa_family==AF_INET))
                        || ((strcmp(ifa->ifa_name,"eth0")==0)&&(ifa->ifa_addr->sa_family==AF_INET)))
                {
                    if (s != 0)
                    {
                        qDebug("getnameinfo() failed: %s\n", gai_strerror(s));
                        exit(EXIT_FAILURE);
                    }
                    qDebug("Interface : %s ",ifa->ifa_name );
                    qDebug("Address : %s", host);
        
                    m_SystemIp = host;
                    qDebug() << "m_SystemIp:" << m_SystemIp;
                }
            }
        
            freeifaddrs(ifaddr);
        
        
            if(channelData->channelType == FILETYPE)
            {
        
                if(m_nchannelInfoStruct[i]->FilePublicIp == inet_ntoa(Inaddr) && m_nchannelInfoStruct[i]->FileOutputport == channelData->OutputPort)
                {
                    qDebug() << "########## File MultiCast IpAddress and port Used i ############";
                }
                else
                {
                    qDebug() << "########## File MultiCast IpAddress and Port not Used i ############";
        
                    //Store File PublicMulticast IP and Port
                    if (m_nchannelInfoStruct[i]->m_nchannelProcess.state() == QProcess::NotRunning)
                    {
                        m_nchannelInfoStruct[i]->FilePublicIp  = inet_ntoa(Inaddr);
                        m_nchannelInfoStruct[i]->FileOutputport = channelData->OutputPort;
        
                        qDebug().noquote() << "############ File Process not Running i ##############" << i << "FileOutputport:"
                                           << m_nchannelInfoStruct[i]->FileOutputport << "FilePublicIp:" << m_nchannelInfoStruct[i]->FilePublicIp;
        
                        QString program = qApp->applicationDirPath()+"/uftpd";
        
        
                        QStringList arguments;
                        arguments << "-M"<< m_nchannelInfoStruct[i]->FilePublicIp << "-p" << QString::number(m_nchannelInfoStruct[i]->FileOutputport) << "-D" << RECEIVE_FILE_PATH;
                        qDebug() <<"File Program:"<< program <<"File arguments:" << arguments;
                        m_nchannelInfoStruct[i]->m_nchannelProcess.start(program,arguments);
                    }
                    else
                    {
                        qDebug().noquote() << "############ File Process Already Running i ##############";
                    }
        
                }
            }
            else if(channelData->channelType == LIVESTREAMTYPE)
            {
                if (m_nchannelInfoStruct[i]->m_nchannelProcess.state() == QProcess::NotRunning)
                {
                    qDebug().noquote() << "############ OnStream Process not Running j ##############";
                    if(m_nchannelInfoStruct[i]->onStreamIP == inet_ntoa(outaddr) && m_nchannelInfoStruct[i]->onStreamport ==  channelData->OutputPort)
                    {
                        qDebug() << "############## Stream Ip and Port Already Used ###############";
                    }
                    else
                    {
                        m_nchannelInfoStruct[i]->onStreamIP   = inet_ntoa(outaddr);
                        m_nchannelInfoStruct[i]->onStreamport =  channelData->OutputPort;
                    }
                    QString Streamfeed = "feed"+QString::number(i+1);
                    QStringList  argumentsList ;
                    if(m_nchannelInfoStruct[i]->onStreamIP >= "224.0.0.0 " && m_nchannelInfoStruct[i]->onStreamIP <= "239.255.255.255")
                    {
                        argumentsList << m_nchannelInfoStruct[i]->onStreamIP << QString::number(m_nchannelInfoStruct[i]->onStreamport)
                                      << Streamfeed;
                    }
                    else
                    {
                        argumentsList << m_nchannelInfoStruct[i]->onStreamIP << QString::number(m_nchannelInfoStruct[i]->onStreamport)
                                      << Streamfeed;
                    }
        
                    QString program = qApp->applicationDirPath()+"/StreamTest.sh";
                    qDebug() <<"Porcess and Arguments" <<  program << argumentsList;
                    m_nchannelInfoStruct[i]->m_nchannelProcess.start(program,argumentsList);
                }
            }
            else
            {
                qDebug() << "########### Invalid Channel Type ############";
            }
        
        }
        

        }

        this function will call every 5 sec once

        m_nchannelInfoStruct[i]->m_nchannelProcess.start() it will be running state if c create new it will be destroyed what is running in process.

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by jsulm
        #8

        @satyanarayana143 said in Object initialize:

        datagram.resize(udpSocket->pendingDatagramSize());

        What is the problem? I don't have time to analyse all this code, please be so kind and tell what the problem is, or which specific part of the code you mean.

        Just a note: you're writing quite low-level code (C style, memcpy, malloc), shouldn't be necessary with Qt/C++.

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

        1 Reply Last reply
        1
        • S Offline
          S Offline
          satyanarayana143
          wrote on last edited by
          #9

          channelInfoStruct *m_nchannelInfoStruct[header->channelcnt];

          for(int i = 0;i < header->channelcnt;i++)
          {
          m_nchannelInfoStruct[i] = new channelInfoStruct();
          }

          jsulmJ S 2 Replies Last reply
          0
          • S satyanarayana143

            channelInfoStruct *m_nchannelInfoStruct[header->channelcnt];

            for(int i = 0;i < header->channelcnt;i++)
            {
            m_nchannelInfoStruct[i] = new channelInfoStruct();
            }

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #10

            @satyanarayana143 ?

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

            1 Reply Last reply
            0
            • S satyanarayana143

              channelInfoStruct *m_nchannelInfoStruct[header->channelcnt];

              for(int i = 0;i < header->channelcnt;i++)
              {
              m_nchannelInfoStruct[i] = new channelInfoStruct();
              }

              S Offline
              S Offline
              satyanarayana143
              wrote on last edited by
              #11

              channelInfoStruct *m_nchannelInfoStruct[header->channelcnt];

              for(int i = 0;i < header->channelcnt;i++)
              {
              m_nchannelInfoStruct[i] = new channelInfoStruct();
              }
              m_nchannelInfoStruct[i] i am starting process it is in running mode foe every 5 sec i am calling this function new of m_nchannelInfoStruct[i] is creating when i create new if process is running i am getting Qprocess destroyed i want to check
              if m_nchannelInfoStruct[i] = new channelInfoStruct(); already created i dont want to create new again.

              jsulmJ 1 Reply Last reply
              0
              • S satyanarayana143

                channelInfoStruct *m_nchannelInfoStruct[header->channelcnt];

                for(int i = 0;i < header->channelcnt;i++)
                {
                m_nchannelInfoStruct[i] = new channelInfoStruct();
                }
                m_nchannelInfoStruct[i] i am starting process it is in running mode foe every 5 sec i am calling this function new of m_nchannelInfoStruct[i] is creating when i create new if process is running i am getting Qprocess destroyed i want to check
                if m_nchannelInfoStruct[i] = new channelInfoStruct(); already created i dont want to create new again.

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #12

                @satyanarayana143 said in Object initialize:

                if m_nchannelInfoStruct[i] = new channelInfoStruct(); already created i dont want to create new again.

                I already provided code for that, did you read it?
                Here once more:

                if (m_nchannelinfoobj[i]) {
                    // Do something
                }
                

                So, in your case:

                if (!m_nchannelinfoobj[i]) {
                    m_nchannelInfoStruct[i] = new channelInfoStruct();
                }
                

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

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  satyanarayana143
                  wrote on last edited by
                  #13

                  if (!m_nchannelinfoobj[i]) {
                  m_nchannelInfoStruct[i] = new channelInfoStruct();
                  }

                  when i create like this and starting QProcess it is crashing

                  jsulmJ 1 Reply Last reply
                  0
                  • S satyanarayana143

                    if (!m_nchannelinfoobj[i]) {
                    m_nchannelInfoStruct[i] = new channelInfoStruct();
                    }

                    when i create like this and starting QProcess it is crashing

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #14

                    @satyanarayana143 said in Object initialize:

                    when i create like this and starting QProcess it is crashing

                    Then use debugger to see what happens...
                    Also, if you delete m_nchannelInfoStruct[i] you need to set it to nullptr:

                    delete m_nchannelInfoStruct[i];
                    m_nchannelInfoStruct[i] = nullptr;
                    

                    And: m_nchannelInfoStruct is a local variable, so it goes out of scope and is destroyed as soon as it leaves its scope...
                    m_ prefix is usually used to mark member variables but m_nchannelInfoStruct is a local variable in your code...

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

                    1 Reply Last reply
                    2
                    • S Offline
                      S Offline
                      satyanarayana143
                      wrote on last edited by
                      #15

                      delete m_nchannelInfoStruct[i];
                      m_nchannelInfoStruct[i] = nullptr;

                      i am not calling delete

                      JonBJ 1 Reply Last reply
                      0
                      • S satyanarayana143

                        delete m_nchannelInfoStruct[i];
                        m_nchannelInfoStruct[i] = nullptr;

                        i am not calling delete

                        JonBJ Online
                        JonBJ Online
                        JonB
                        wrote on last edited by JonB
                        #16

                        @satyanarayana143
                        As @jsulm is telling you:

                        void EdgeClient::processPendingDatagrams()
                        {
                            channelInfoStruct *m_nchannelInfoStruct[header->channelcnt];
                        
                            for(int i = 0;i < header->channelcnt;i++)
                            {
                                  m_nchannelInfoStruct[i] = new channelInfoStruct(); 
                                  m_nchannelInfoStruct[i]->m_nchannelProcess.start(program,argumentsList);
                            }
                        
                            // m_nchannelInfoStruct destroyed here
                        }
                        

                        m_nchannelInfoStruct is a local variable in processPendingDatagrams() function. When that exits the array is destroyed.

                        Then your m_nchannelProcess, you are starting a new process, on the same QProcess variable, each time round. Dunno what's going on there.

                        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