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. Why i get segmentation fault in the following code ?
Forum Updated to NodeBB v4.3 + New Features

Why i get segmentation fault in the following code ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
14 Posts 7 Posters 3.8k Views 3 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.
  • A Offline
    A Offline
    Ahti
    wrote on 26 Aug 2016, 15:03 last edited by
    #1

    My Code:

    void EyeCare::on_startcaringButton_clicked()
    {
        QErrorMessage *errorMessage = new QErrorMessage ;
    
        errorMessage->showMessage(GetLastErrorString ());
    }
    
    QString EyeCare::GetLastErrorString()
    {
      DWORD error = GetLastError();
      if (error)
      {
        LPTSTR errorMessage;
        FormatMessage(
                    FORMAT_MESSAGE_FROM_SYSTEM |
                    FORMAT_MESSAGE_IGNORE_INSERTS |
                    FORMAT_MESSAGE_ARGUMENT_ARRAY |
                    FORMAT_MESSAGE_ALLOCATE_BUFFER,NULL,
                    error,0,(LPWSTR) &errorMessage,0, NULL );
        return QString :: fromWCharArray(errorMessage) ;
      }
    }
    

    what is a signature ?? Lol

    1 Reply Last reply
    0
    • K Offline
      K Offline
      kshegunov
      Moderators
      wrote on 26 Aug 2016, 15:09 last edited by
      #2

      You should post the stack trace from the crash.

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      0
      • V Offline
        V Offline
        VRonin
        wrote on 26 Aug 2016, 15:17 last edited by
        #3

        is errorMessage null or invalid when you pass it to fromWCharArray?

        LPTSTR errorMessage =NULL;
            FormatMessage(
                        FORMAT_MESSAGE_FROM_SYSTEM |
                        FORMAT_MESSAGE_IGNORE_INSERTS |
                        FORMAT_MESSAGE_ARGUMENT_ARRAY |
                        FORMAT_MESSAGE_ALLOCATE_BUFFER,NULL,
                        error,0,(LPWSTR) &errorMessage,0, NULL );
        Q_ASSERT(errorMessage);
            return QString :: fromWCharArray(errorMessage) ;
        

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        A 1 Reply Last reply 26 Aug 2016, 18:03
        0
        • V VRonin
          26 Aug 2016, 15:17

          is errorMessage null or invalid when you pass it to fromWCharArray?

          LPTSTR errorMessage =NULL;
              FormatMessage(
                          FORMAT_MESSAGE_FROM_SYSTEM |
                          FORMAT_MESSAGE_IGNORE_INSERTS |
                          FORMAT_MESSAGE_ARGUMENT_ARRAY |
                          FORMAT_MESSAGE_ALLOCATE_BUFFER,NULL,
                          error,0,(LPWSTR) &errorMessage,0, NULL );
          Q_ASSERT(errorMessage);
              return QString :: fromWCharArray(errorMessage) ;
          
          A Offline
          A Offline
          Ahti
          wrote on 26 Aug 2016, 18:03 last edited by Ahti
          #4

          @VRonin still displaying segmentation fault

          0_1472234832951_upload.JPG

          see the error yourself

          what is a signature ?? Lol

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 26 Aug 2016, 18:17 last edited by
            #5

            Hi
            running your GetLastErrorString()
            in win 10, mingw compiler, Qt5.7, it does not crash.

            A 1 Reply Last reply 26 Aug 2016, 18:49
            0
            • M mrjj
              26 Aug 2016, 18:17

              Hi
              running your GetLastErrorString()
              in win 10, mingw compiler, Qt5.7, it does not crash.

              A Offline
              A Offline
              Ahti
              wrote on 26 Aug 2016, 18:49 last edited by Ahti
              #6

              @mrjj i have windows 7 32bit Qt5.7 MinGW
              did you checked it ?? and GetLastErrorString is not an inbuilt function i created on my own for the purpose of getting the details of the error message with the help of error code returned by GetLastError ( which infact an inbuilt function ).

              what is a signature ?? Lol

              M 1 Reply Last reply 26 Aug 2016, 18:52
              0
              • A Ahti
                26 Aug 2016, 18:49

                @mrjj i have windows 7 32bit Qt5.7 MinGW
                did you checked it ?? and GetLastErrorString is not an inbuilt function i created on my own for the purpose of getting the details of the error message with the help of error code returned by GetLastError ( which infact an inbuilt function ).

                M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 26 Aug 2016, 18:52 last edited by mrjj
                #7

                @Ahti
                if you call GetLastErrorString directly, does it still crash?
                update: yes i ran it.

                A 1 Reply Last reply 26 Aug 2016, 19:21
                0
                • M mrjj
                  26 Aug 2016, 18:52

                  @Ahti
                  if you call GetLastErrorString directly, does it still crash?
                  update: yes i ran it.

                  A Offline
                  A Offline
                  Ahti
                  wrote on 26 Aug 2016, 19:21 last edited by
                  #8

                  @mrjj yes it does work if i run it separately but not as QErrorMessage argument why ??? :0 maybe there is a problem in conversion.

                  what is a signature ?? Lol

                  1 Reply Last reply
                  0
                  • R Offline
                    R Offline
                    Rondog
                    wrote on 26 Aug 2016, 23:27 last edited by
                    #9

                    Not sure if this is a problem but the variable 'errorMessage' is type LPTSTR but cast to LPWSTR in the function FormatMessage(...).

                    If 'DWORD error' is null then nothing is returned from this function; it should return a QString in any case. Not returning an expected return type will probably crash the program (and should generate a warning message during compilation).

                    1 Reply Last reply
                    1
                    • Paul ColbyP Offline
                      Paul ColbyP Offline
                      Paul Colby
                      wrote on 27 Aug 2016, 01:06 last edited by Paul Colby
                      #10

                      @Rondog said:

                      the variable 'errorMessage' is type LPTSTR but cast to LPWSTR in the function FormatMessage(...)

                      The cast is necessary, because he's using FORMAT_MESSAGE_ALLOCATE_BUFFER, in which case the lpBuffer parameter (errorMessage in this case) is interpreted not as an LP..., but a pointer to an LP... (ie that flags causes the function to interpret the parameter as something other than what the function signature says... love win32).

                      Having said that, the cast is wrong (probably benign though)... it should be a cast to LPTSTR, not LPWSTR - but whether that matters or not depends on whether the Microsoft headers are set to Unicode verses non-Unicode mode. Assuming the compiler isn't complaining about the cast, and well as the use of QString::fromWCharArray without a cast, he's probably using Unicode mode, in which case the Microsoft headers define LPTSTR as LPWSTR anyway, but still not good practice.

                      So, just adding what's already been said:

                      • EyeCare::GetLastErrorString should always return a value (not sure that would cause a crash though - I would have thought a default-constructed QString would be returned instead, but always better to be explicit);
                      • you should check the result of the FormatMessage (if its 0, then errorMessage is likely to be an invalid random dangling pointer);
                      • you should definitely initialise errorMessage to NULL, otherwise there's no way to distinguish between FormatMessage failing pre-allocating versus failing post-allocating.
                      • (for later) you should be creating the return QString, then freeing errorMessage, then returning; otherwise you have a memory leak.

                      I'd do something like:

                      QString EyeCare::GetLastErrorString()
                      {
                        QString message = QString::fromLatin1("no error"); // Or empty string if you prefer.
                        const DWORD error = GetLastError();
                        if (error != ERROR_SUCCESS) // I just like to be explicit.
                        {
                          // Fetch the error message.
                          LPTSTR errorMessage = NULL;
                          const DWORD size = FormatMessage(
                                      FORMAT_MESSAGE_FROM_SYSTEM |
                                      FORMAT_MESSAGE_IGNORE_INSERTS |
                                      FORMAT_MESSAGE_ARGUMENT_ARRAY |
                                      FORMAT_MESSAGE_ALLOCATE_BUFFER,NULL,
                                      error,0,(LPTSTR) &errorMessage,0, NULL );
                      
                          // Copy the error message to a QString object.
                          if (size > 0) {
                              #ifdef UNICODE
                               message = QString::fromWCharArray(errorMessage, size);
                              #else
                               message = QString::fromLocal8Bit(errorMessage, size);
                              #endif
                          }
                      
                          // Free the error message buffer.
                          if (errorMessage != NULL) {
                            HeapFree(GetProcessHeap(), errorMessage);
                          }
                        }
                        return message;
                      }
                      

                      Cheers.

                      A 1 Reply Last reply 27 Aug 2016, 07:53
                      1
                      • K Offline
                        K Offline
                        kuzulis
                        Qt Champions 2020
                        wrote on 27 Aug 2016, 07:41 last edited by
                        #11

                        Just use qt_error_string() function instead.

                        1 Reply Last reply
                        0
                        • Paul ColbyP Paul Colby
                          27 Aug 2016, 01:06

                          @Rondog said:

                          the variable 'errorMessage' is type LPTSTR but cast to LPWSTR in the function FormatMessage(...)

                          The cast is necessary, because he's using FORMAT_MESSAGE_ALLOCATE_BUFFER, in which case the lpBuffer parameter (errorMessage in this case) is interpreted not as an LP..., but a pointer to an LP... (ie that flags causes the function to interpret the parameter as something other than what the function signature says... love win32).

                          Having said that, the cast is wrong (probably benign though)... it should be a cast to LPTSTR, not LPWSTR - but whether that matters or not depends on whether the Microsoft headers are set to Unicode verses non-Unicode mode. Assuming the compiler isn't complaining about the cast, and well as the use of QString::fromWCharArray without a cast, he's probably using Unicode mode, in which case the Microsoft headers define LPTSTR as LPWSTR anyway, but still not good practice.

                          So, just adding what's already been said:

                          • EyeCare::GetLastErrorString should always return a value (not sure that would cause a crash though - I would have thought a default-constructed QString would be returned instead, but always better to be explicit);
                          • you should check the result of the FormatMessage (if its 0, then errorMessage is likely to be an invalid random dangling pointer);
                          • you should definitely initialise errorMessage to NULL, otherwise there's no way to distinguish between FormatMessage failing pre-allocating versus failing post-allocating.
                          • (for later) you should be creating the return QString, then freeing errorMessage, then returning; otherwise you have a memory leak.

                          I'd do something like:

                          QString EyeCare::GetLastErrorString()
                          {
                            QString message = QString::fromLatin1("no error"); // Or empty string if you prefer.
                            const DWORD error = GetLastError();
                            if (error != ERROR_SUCCESS) // I just like to be explicit.
                            {
                              // Fetch the error message.
                              LPTSTR errorMessage = NULL;
                              const DWORD size = FormatMessage(
                                          FORMAT_MESSAGE_FROM_SYSTEM |
                                          FORMAT_MESSAGE_IGNORE_INSERTS |
                                          FORMAT_MESSAGE_ARGUMENT_ARRAY |
                                          FORMAT_MESSAGE_ALLOCATE_BUFFER,NULL,
                                          error,0,(LPTSTR) &errorMessage,0, NULL );
                          
                              // Copy the error message to a QString object.
                              if (size > 0) {
                                  #ifdef UNICODE
                                   message = QString::fromWCharArray(errorMessage, size);
                                  #else
                                   message = QString::fromLocal8Bit(errorMessage, size);
                                  #endif
                              }
                          
                              // Free the error message buffer.
                              if (errorMessage != NULL) {
                                HeapFree(GetProcessHeap(), errorMessage);
                              }
                            }
                            return message;
                          }
                          

                          Cheers.

                          A Offline
                          A Offline
                          Ahti
                          wrote on 27 Aug 2016, 07:53 last edited by
                          #12

                          @Paul-Colby

                          Getting the following error now: :

                          0_1472284308498_sending.JPG

                          what is a signature ?? Lol

                          1 Reply Last reply
                          0
                          • R Offline
                            R Offline
                            Rondog
                            wrote on 27 Aug 2016, 11:57 last edited by
                            #13

                            I don't know if it is just me but I can't see either of the posted error messages. With only a comment that says "I'm getting this now..." and without any supporting information it is tough to guess what might be happening.

                            1 Reply Last reply
                            1
                            • M Offline
                              M Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on 27 Aug 2016, 13:39 last edited by
                              #14

                              its an image it seems
                              0_1472284308498_sending.JPG
                              (/uploads/files/1472284318241-sending-resized.jpg)
                              Might be forum bug. An update is coming afaik, so lets see.

                              1 Reply Last reply
                              0

                              1/14

                              26 Aug 2016, 15:03

                              • Login

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