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. Qt Application crashed. Why?
Forum Updated to NodeBB v4.3 + New Features

Qt Application crashed. Why?

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 5 Posters 1.0k 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.
  • B Offline
    B Offline
    bogong
    wrote on 23 Jul 2020, 14:54 last edited by bogong
    #1

    Hello!

    I am just playing around CPP pointers (just for refreshing my memory). And got the issue when all is OK when it's plain CPP application (here the full code) and crashing when I am including it into QT/QML application (here the full code). This code:

    	int* oPointerInteger1;
    	float* oPointerFloat2 = new float(1.23456);
    
    	*oPointerInteger1 = static_cast<int>(*oPointerFloat2);   << This line is crashing
    
    	ALOG << "oPointerFloat2: " << *oPointerFloat2 << endl;
    	ALOG << "oPointerInteger1: " << oPointerInteger1 << endl;
    	ALOG << "oPointerInteger1 value: " << *oPointerInteger1 << endl;
    	*oPointerFloat2 = 2.3456f;
    	ALOG << "oPointerFloat2: " << *oPointerFloat2 << endl;
    	ALOG << "oPointerInteger1: " << oPointerInteger1 << endl;
    	ALOG << "oPointerInteger1 value: " << *oPointerInteger1 << endl;
    
    	*oPointerInteger1 = 10;   << This line crashing too
    
    	ALOG << "oPointerFloat2: " << *oPointerFloat2 << endl;
    	ALOG << "oPointerInteger1: " << oPointerInteger1 << endl;
    	ALOG << "oPointerInteger1 value: " << *oPointerInteger1 << endl;
    

    The question is what is killing Qt/QML application? In this case it's working perfectly when it's plain CPP application. What am I missing?

    All is working when I am changing this line:

    int* oPointerInteger1;
    

    onto

    int* oPointerInteger1 = new int(0);
    
    K 1 Reply Last reply 23 Jul 2020, 15:12
    0
    • B bogong
      23 Jul 2020, 14:54

      Hello!

      I am just playing around CPP pointers (just for refreshing my memory). And got the issue when all is OK when it's plain CPP application (here the full code) and crashing when I am including it into QT/QML application (here the full code). This code:

      	int* oPointerInteger1;
      	float* oPointerFloat2 = new float(1.23456);
      
      	*oPointerInteger1 = static_cast<int>(*oPointerFloat2);   << This line is crashing
      
      	ALOG << "oPointerFloat2: " << *oPointerFloat2 << endl;
      	ALOG << "oPointerInteger1: " << oPointerInteger1 << endl;
      	ALOG << "oPointerInteger1 value: " << *oPointerInteger1 << endl;
      	*oPointerFloat2 = 2.3456f;
      	ALOG << "oPointerFloat2: " << *oPointerFloat2 << endl;
      	ALOG << "oPointerInteger1: " << oPointerInteger1 << endl;
      	ALOG << "oPointerInteger1 value: " << *oPointerInteger1 << endl;
      
      	*oPointerInteger1 = 10;   << This line crashing too
      
      	ALOG << "oPointerFloat2: " << *oPointerFloat2 << endl;
      	ALOG << "oPointerInteger1: " << oPointerInteger1 << endl;
      	ALOG << "oPointerInteger1 value: " << *oPointerInteger1 << endl;
      

      The question is what is killing Qt/QML application? In this case it's working perfectly when it's plain CPP application. What am I missing?

      All is working when I am changing this line:

      int* oPointerInteger1;
      

      onto

      int* oPointerInteger1 = new int(0);
      
      K Offline
      K Offline
      KroMignon
      wrote on 23 Jul 2020, 15:12 last edited by
      #2

      @bogong said in Qt Application crashed. Why?:

      *oPointerInteger1 = static_cast<int>(*oPointerFloat2); << This line is crashing

      ==> as far as I can see, you did not initialise oPointerInteger1, so you write on undefined address.
      C/C++ pointers basics => https://www.tutorialspoint.com/cprogramming/c_pointers.htm

      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

      B 1 Reply Last reply 23 Jul 2020, 15:15
      4
      • K KroMignon
        23 Jul 2020, 15:12

        @bogong said in Qt Application crashed. Why?:

        *oPointerInteger1 = static_cast<int>(*oPointerFloat2); << This line is crashing

        ==> as far as I can see, you did not initialise oPointerInteger1, so you write on undefined address.
        C/C++ pointers basics => https://www.tutorialspoint.com/cprogramming/c_pointers.htm

        B Offline
        B Offline
        bogong
        wrote on 23 Jul 2020, 15:15 last edited by bogong
        #3

        @KroMignon But it's working perfectly on Plain CPP application. There are only trouble with Qt/QML application. There are published examples. The declaring and assigning - is the solution to avoid the crash? The only declaring - is there reason of Qt/QML crash. I am trying to get why is that only for Qt/QML and OK for plain CPP. From the point of view of syntax of pure CPP - all is OK. The question is about QT/QML.

        K 1 Reply Last reply 23 Jul 2020, 15:24
        0
        • B bogong
          23 Jul 2020, 15:15

          @KroMignon But it's working perfectly on Plain CPP application. There are only trouble with Qt/QML application. There are published examples. The declaring and assigning - is the solution to avoid the crash? The only declaring - is there reason of Qt/QML crash. I am trying to get why is that only for Qt/QML and OK for plain CPP. From the point of view of syntax of pure CPP - all is OK. The question is about QT/QML.

          K Offline
          K Offline
          KroMignon
          wrote on 23 Jul 2020, 15:24 last edited by
          #4

          @bogong said in Qt Application crashed. Why?:

          But it's working perfectly on Plain CPP application.

          No this cannot work, uninitialized pointer == undefined behavior.
          This is wrong/bad coding:

          int* oPointerInteger1;
          float* oPointerFloat2 = new float(1.23456);
          
          *oPointerInteger1 = static_cast<int>(*oPointerFloat2);
          
          

          This is correct:

          int* oPointerInteger1;
          float* oPointerFloat2 = new float(1.23456);
          
          int myBeautifulVariable;
          
          oPointerInteger1 = &myBeautifulVariable;
          *oPointerInteger1 = static_cast<int>(*oPointerFloat2);
          
          

          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

          B 1 Reply Last reply 23 Jul 2020, 15:28
          3
          • K KroMignon
            23 Jul 2020, 15:24

            @bogong said in Qt Application crashed. Why?:

            But it's working perfectly on Plain CPP application.

            No this cannot work, uninitialized pointer == undefined behavior.
            This is wrong/bad coding:

            int* oPointerInteger1;
            float* oPointerFloat2 = new float(1.23456);
            
            *oPointerInteger1 = static_cast<int>(*oPointerFloat2);
            
            

            This is correct:

            int* oPointerInteger1;
            float* oPointerFloat2 = new float(1.23456);
            
            int myBeautifulVariable;
            
            oPointerInteger1 = &myBeautifulVariable;
            *oPointerInteger1 = static_cast<int>(*oPointerFloat2);
            
            
            B Offline
            B Offline
            bogong
            wrote on 23 Jul 2020, 15:28 last edited by bogong
            #5

            @KroMignon Just try it ... And see how it works ...

            Screenshot 2020-07-23 at 18.27.59.png

            It's working perfectly when plain CPP.
            And crashing when it's inside of Qt/QML.

            Screenshot 2020-07-23 at 18.32.08.png

            1 Reply Last reply
            0
            • fcarneyF Offline
              fcarneyF Offline
              fcarney
              wrote on 23 Jul 2020, 15:35 last edited by
              #6

              You are playing programming roulette. Eventually you will shoot yourself.

              C++ is a perfectly valid school of magic.

              1 Reply Last reply
              3
              • B Offline
                B Offline
                Bonnie
                wrote on 23 Jul 2020, 15:35 last edited by Bonnie
                #7

                Dude, that is exactly what undefined behavior means.
                Not crash doesn't mean it is not wrong.

                1 Reply Last reply
                7
                • fcarneyF Offline
                  fcarneyF Offline
                  fcarney
                  wrote on 23 Jul 2020, 16:04 last edited by
                  #8

                  Also, you didn't copy the "published" code correctly:

                  int* oPointerInteger1;
                  

                  Should be:

                  int* oPointerInteger1 = new int(0);
                  

                  C++ is a perfectly valid school of magic.

                  1 Reply Last reply
                  0
                  • mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on 23 Jul 2020, 20:07 last edited by
                    #9

                    Hi

                    But it's working perfectly on Plain CPP application.

                    Actually, in a plain cpp app, there is less it can randomly hit that will cause a crash.
                    But with QML/Qt loaded, far more to damage by writing int to random memory location.

                    so its random and will also crash in a pure CPP app if it randomly hits that
                    is extra bad.

                    B 1 Reply Last reply 25 Jul 2020, 07:03
                    5
                    • mrjjM mrjj
                      23 Jul 2020, 20:07

                      Hi

                      But it's working perfectly on Plain CPP application.

                      Actually, in a plain cpp app, there is less it can randomly hit that will cause a crash.
                      But with QML/Qt loaded, far more to damage by writing int to random memory location.

                      so its random and will also crash in a pure CPP app if it randomly hits that
                      is extra bad.

                      B Offline
                      B Offline
                      bogong
                      wrote on 25 Jul 2020, 07:03 last edited by
                      #10

                      @mrjj Thx. I've got it. Issue closed.

                      1 Reply Last reply
                      0

                      1/10

                      23 Jul 2020, 14:54

                      • Login

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