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?

Qt Application crashed. Why?

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 5 Posters 1.3k 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 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);
    
    KroMignonK 1 Reply Last reply
    0
    • B bogong

      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);
      
      KroMignonK Offline
      KroMignonK Offline
      KroMignon
      wrote on 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
      4
      • KroMignonK KroMignon

        @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 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.

        KroMignonK 1 Reply Last reply
        0
        • B bogong

          @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.

          KroMignonK Offline
          KroMignonK Offline
          KroMignon
          wrote on 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
          3
          • KroMignonK KroMignon

            @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 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 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 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 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 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
                    5
                    • mrjjM mrjj

                      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 last edited by
                      #10

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

                      1 Reply Last reply
                      0

                      • Login

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