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. Segmentation fault SIGSEGV
QtWS25 Last Chance

Segmentation fault SIGSEGV

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 1.3k Views
  • 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
    sudharsan10
    wrote on 29 Mar 2021, 12:40 last edited by sudharsan10
    #1

    Hey all. I am using ubuntu and I have a library provided by the manufacturer and when I tried to debug it, I got segmentation fault.

    My mentor said he was able to solve it by adding the following in .pro file.
    QMAKE_CXXFLAGS += -std=c++0x -pthread
    LIBS += -pthread

    But when I ran the same project in my PC, I still got the error but it was resolved in his PC.
    Can anyone please tell me where can the problem be?

    This is the function where I am getting the issue. The debugger stops at " int allocsize = strlen(key + strlen(szKey)) + 1;"

    /*! Parses the configuration file

    • \param szFile File name
    • \param szKey Key to search for
    • \param szValue Returned value (if key was found)
    • \return != 0 on success /
      /*****************************************************************************/
      static int GetConfigString(const char
      szFile, const char* szKey, char** szValue)
      {
      int ret = 0;
      FILE* fd = fopen(szFile, "r");

    if(NULL != fd)
    {
    /* File is open /
    char
    buffer = malloc(PARSER_BUFFER_SIZE);

    /* Read file line by line */
    while(NULL != fgets(buffer, PARSER_BUFFER_SIZE, fd))
    {
      char* key;
    
      /* '#' marks a comment line in the device.conf file */
      if(buffer[0] == '#')
        continue;
    
      /* Search for key in the input buffer */
      key = (char*)strcasestr(buffer, szKey);
    
      if(NULL != key)
      {
        /* We've found the key */
        int   allocsize  = strlen(key + strlen(szKey)) + 1;
        int   valuelen;
        char* tempstring = (char*)malloc(allocsize);
    
        strcpy(tempstring, key + strlen(szKey));
        valuelen = strlen(tempstring);
    
        /* strip all trailing whitespaces */
        while( (tempstring[valuelen - 1] == '\n') ||
               (tempstring[valuelen - 1] == '\r') ||
               (tempstring[valuelen - 1] == ' ') )
        {
          tempstring[valuelen - 1] = '\0';
          --valuelen;
        }
    
        *szValue = tempstring;
        ret = 1;
        break;
      }
    }
    
    free(buffer);
    fclose(fd);
    

    }

    return ret;
    }

    J 1 Reply Last reply 29 Mar 2021, 12:40
    0
    • S sudharsan10
      29 Mar 2021, 12:40

      Hey all. I am using ubuntu and I have a library provided by the manufacturer and when I tried to debug it, I got segmentation fault.

      My mentor said he was able to solve it by adding the following in .pro file.
      QMAKE_CXXFLAGS += -std=c++0x -pthread
      LIBS += -pthread

      But when I ran the same project in my PC, I still got the error but it was resolved in his PC.
      Can anyone please tell me where can the problem be?

      This is the function where I am getting the issue. The debugger stops at " int allocsize = strlen(key + strlen(szKey)) + 1;"

      /*! Parses the configuration file

      • \param szFile File name
      • \param szKey Key to search for
      • \param szValue Returned value (if key was found)
      • \return != 0 on success /
        /*****************************************************************************/
        static int GetConfigString(const char
        szFile, const char* szKey, char** szValue)
        {
        int ret = 0;
        FILE* fd = fopen(szFile, "r");

      if(NULL != fd)
      {
      /* File is open /
      char
      buffer = malloc(PARSER_BUFFER_SIZE);

      /* Read file line by line */
      while(NULL != fgets(buffer, PARSER_BUFFER_SIZE, fd))
      {
        char* key;
      
        /* '#' marks a comment line in the device.conf file */
        if(buffer[0] == '#')
          continue;
      
        /* Search for key in the input buffer */
        key = (char*)strcasestr(buffer, szKey);
      
        if(NULL != key)
        {
          /* We've found the key */
          int   allocsize  = strlen(key + strlen(szKey)) + 1;
          int   valuelen;
          char* tempstring = (char*)malloc(allocsize);
      
          strcpy(tempstring, key + strlen(szKey));
          valuelen = strlen(tempstring);
      
          /* strip all trailing whitespaces */
          while( (tempstring[valuelen - 1] == '\n') ||
                 (tempstring[valuelen - 1] == '\r') ||
                 (tempstring[valuelen - 1] == ' ') )
          {
            tempstring[valuelen - 1] = '\0';
            --valuelen;
          }
      
          *szValue = tempstring;
          ret = 1;
          break;
        }
      }
      
      free(buffer);
      fclose(fd);
      

      }

      return ret;
      }

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 29 Mar 2021, 12:40 last edited by
      #2

      @sudharsan10 said in Segmentation fault SIGSEGV:

      I got segmentation fault

      Please post the stack trace

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

      1 Reply Last reply
      1
      • S Offline
        S Offline
        sudharsan10
        wrote on 29 Mar 2021, 12:50 last edited by
        #3

        Hi @jsulm Do you mean this?

        e84bac62-46db-4c47-a15b-e98fd5c53590-image.png

        J 1 Reply Last reply 29 Mar 2021, 12:53
        0
        • S sudharsan10
          29 Mar 2021, 12:50

          Hi @jsulm Do you mean this?

          e84bac62-46db-4c47-a15b-e98fd5c53590-image.png

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 29 Mar 2021, 12:53 last edited by
          #4

          @sudharsan10 What is in TCP_Connector.c at line 729?
          Also check your code in nextTransport_demo.c line 200
          And I don't see anything related to Qt...

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

          1 Reply Last reply
          0
          • S Offline
            S Offline
            sudharsan10
            wrote on 29 Mar 2021, 13:08 last edited by
            #5

            You shall find line 729 in the image and also in the code I have posted above :)
            I was wondering if it could be something related to my project settings, Since there was no issue while running the same project in another PC.

            JonBJ 1 Reply Last reply 29 Mar 2021, 14:10
            0
            • S sudharsan10
              29 Mar 2021, 13:08

              You shall find line 729 in the image and also in the code I have posted above :)
              I was wondering if it could be something related to my project settings, Since there was no issue while running the same project in another PC.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on 29 Mar 2021, 14:10 last edited by JonB
              #6

              @sudharsan10

              • Your question seems to have nothing at all to do with Qt.

              • If the reason the changed link flags make things work is to do with pthread, you have said nothing about whether your program uses any threads.

              • If any line is longer than PARSER_BUFFER_SIZE your code will not work correctly.

              • int allocsize = strlen(key + strlen(szKey)) + 1;
                This line looks potentially dodgy. Print out allocsize, and consider how you are using that malloc()ed area to copy things into.

              • /* strip all trailing whitespaces */
                What happens in your code if the value is all whitespace? I do not see why your should not incorrectly keep decrementing and go too far to the left, potentially overwriting unallocated memory?

              • Since there was no issue while running the same project in another PC.

              More likely than a project/compilation issue is that your code, somewhere, uses an uninitialized value or overwrites/underwrites a random area of memory. This can and will behave/show up differently from one PC to another, for no discernible reason....

              1 Reply Last reply
              1
              • S Offline
                S Offline
                sudharsan10
                wrote on 2 Apr 2021, 19:30 last edited by
                #7

                @JonB Thank you for pointing out at the potential problems. For now, I found it to be a project/compilation issue. I changed the compiler version to Clang just like the other PC. Also I changed the debugger settings as suggested here https://forum.qt.io/topic/100958/catching-unix-interrupt-signal-on-console-application-when-debugging-with-qtcreator/3 and it seems to work.

                1 Reply Last reply
                0

                1/7

                29 Mar 2021, 12:40

                • Login

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