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. if (my_pointer==nullptr) causes segmentation fault
Forum Updated to NodeBB v4.3 + New Features

if (my_pointer==nullptr) causes segmentation fault

Scheduled Pinned Locked Moved Solved General and Desktop
16 Posts 4 Posters 3.5k Views 2 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.
  • J Offline
    J Offline
    JoeCFD
    wrote on 13 Jan 2023, 16:52 last edited by JoeCFD
    #3

    check if this = nullptr. you do not need if (this->param==nullptr) you can simply use if ( param==nullptr )

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Seb Tur
      wrote on 13 Jan 2023, 16:54 last edited by
      #4

      with if(param==nullptr) I get the same segfault

      I added this-> just in case after being clueless for hours

      C J 2 Replies Last reply 13 Jan 2023, 16:56
      0
      • S Seb Tur
        13 Jan 2023, 16:54

        with if(param==nullptr) I get the same segfault

        I added this-> just in case after being clueless for hours

        C Offline
        C Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 13 Jan 2023, 16:56 last edited by
        #5

        @Seb-Tur said in if (my_pointer==nullptr) causes segmentation fault:

        with if(param==nullptr) I get the same segfault

        Because it's exactly the same...

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        0
        • S Seb Tur
          13 Jan 2023, 16:54

          with if(param==nullptr) I get the same segfault

          I added this-> just in case after being clueless for hours

          J Offline
          J Offline
          JoeCFD
          wrote on 13 Jan 2023, 16:56 last edited by
          #6

          @Seb-Tur run your app with valgrind to check it out if you are working on Linux.

          1 Reply Last reply
          0
          • C Offline
            C Offline
            Chris Kawa
            Lifetime Qt Champion
            wrote on 13 Jan 2023, 17:11 last edited by
            #7

            This will happen when you call a method on a deleted object. this points to garbage and any access to its members results in seg fault. Make sure your object is still alive when you call this function.

            S 1 Reply Last reply 13 Jan 2023, 18:16
            1
            • C Chris Kawa
              13 Jan 2023, 17:11

              This will happen when you call a method on a deleted object. this points to garbage and any access to its members results in seg fault. Make sure your object is still alive when you call this function.

              S Offline
              S Offline
              Seb Tur
              wrote on 13 Jan 2023, 18:16 last edited by Seb Tur
              #8

              @Chris-Kawa

              I don't understand
              I'm calling this check in a method belonging to inicio_common class and param also belongs there.
              so when I create an object

              inicio_common *common_object;
              so then it gets its member from .h
              common_object->param =nullptr

              so
              method common_object->parameter_read()
              reffers to common_object->param

              Param is a sibling member , so how come it's parent "this" can be garbage? If this was garbage than its method read_paramater wouldnt't be called either?
              I don't delete param in any method in inicio_common class. It is a private member.

              C 1 Reply Last reply 13 Jan 2023, 19:01
              0
              • J Offline
                J Offline
                JoeCFD
                wrote on 13 Jan 2023, 18:18 last edited by JoeCFD
                #9

                @Seb-Tur said in if (my_pointer==nullptr) causes segmentation fault:

                inicio_common *common_object;

                how did you create this one: inicio_common *common_object{}; ? is it nullptr?

                S 1 Reply Last reply 13 Jan 2023, 19:17
                0
                • S Seb Tur
                  13 Jan 2023, 18:16

                  @Chris-Kawa

                  I don't understand
                  I'm calling this check in a method belonging to inicio_common class and param also belongs there.
                  so when I create an object

                  inicio_common *common_object;
                  so then it gets its member from .h
                  common_object->param =nullptr

                  so
                  method common_object->parameter_read()
                  reffers to common_object->param

                  Param is a sibling member , so how come it's parent "this" can be garbage? If this was garbage than its method read_paramater wouldnt't be called either?
                  I don't delete param in any method in inicio_common class. It is a private member.

                  C Offline
                  C Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on 13 Jan 2023, 19:01 last edited by
                  #10

                  @Seb-Tur said in if (my_pointer==nullptr) causes segmentation fault:

                  If this was garbage than its method read_paramater wouldnt't be called either?

                  Why not? Who would prevent it?

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  1 Reply Last reply
                  0
                  • J JoeCFD
                    13 Jan 2023, 18:18

                    @Seb-Tur said in if (my_pointer==nullptr) causes segmentation fault:

                    inicio_common *common_object;

                    how did you create this one: inicio_common *common_object{}; ? is it nullptr?

                    S Offline
                    S Offline
                    Seb Tur
                    wrote on 13 Jan 2023, 19:17 last edited by
                    #11

                    @JoeCFD

                    declared in mainwindow.h as
                    inicio_common = *common_object =nullptr

                    than contructed at the begining of mainwindow.cpp

                    common_object = new inicio_common();

                    J 1 Reply Last reply 13 Jan 2023, 19:26
                    0
                    • S Seb Tur
                      13 Jan 2023, 19:17

                      @JoeCFD

                      declared in mainwindow.h as
                      inicio_common = *common_object =nullptr

                      than contructed at the begining of mainwindow.cpp

                      common_object = new inicio_common();

                      J Offline
                      J Offline
                      JoeCFD
                      wrote on 13 Jan 2023, 19:26 last edited by
                      #12

                      @Seb-Tur I guess it is better for you to show more code.

                      1 Reply Last reply
                      0
                      • C Offline
                        C Offline
                        Chris Kawa
                        Lifetime Qt Champion
                        wrote on 13 Jan 2023, 19:27 last edited by Chris Kawa
                        #13

                        @Seb-Tur It's not the param member that is deleted. It's the instance of class inicio_common.

                        One case where this would happen:

                        inicio_common *common_object;       // common_object is an uninitialized variable
                        common_object->parameter_read(...); // calling method with garbage "this" -> crash
                        

                        another way the same happens:

                        inicio_common *common_object = new inicio_common(); // common_object is initialized, ok
                        common_object->parameter_read(...);                 // works
                        delete common_object;                               // common_object now points to garbage
                        common_object->parameter_read(...);                 // calling method with garbage "this" -> crash
                        

                        If this was garbage than its method read_paramater wouldnt't be called either?

                        There's nothing in C++ language preventing that. You can create pointers pointing to random memory and call methods on them. For example:

                        QString* foo = (QString*)12345; // because why not
                        foo->clear();                   // garbage "this" -> crash
                        
                        1 Reply Last reply
                        1
                        • S Offline
                          S Offline
                          Seb Tur
                          wrote on 13 Jan 2023, 20:19 last edited by Seb Tur
                          #14

                          Thanks all for the support, your hints directed me to the right path to find an error I had no idea was an error.

                          Here's the scenario :

                          I used common_object in mainwindow doing various activities and all was fine;

                          at some point I was passing the common_object pointer in a constructor to another object of action_class that needed some properties of inicio_common object.,

                          action_class.h contained

                          inicio_common *ref_common = nullptr;
                          

                          but action class cunstructor was capturing the common_object

                          action_class::action_class(inicio_common *common)
                          {
                            ref_common = common;
                          }
                          

                          so in the mainwindow I had common_object used by mainwindow and action_class_object because I run:

                          action_class *action_class_object = new (common_object);
                          

                          After I was done with the actions I deleted action_class_object with

                          delete(action_class_object);

                          and this seems to have deleted the common_object by deleting ref_common in cascade .... I guess
                          Does it make sense or is it just my wicked thinking?

                          Anyway I still don't get why if(param==nullptr) would crash instead of returning true;

                          C 1 Reply Last reply 13 Jan 2023, 20:43
                          0
                          • S Seb Tur
                            13 Jan 2023, 20:19

                            Thanks all for the support, your hints directed me to the right path to find an error I had no idea was an error.

                            Here's the scenario :

                            I used common_object in mainwindow doing various activities and all was fine;

                            at some point I was passing the common_object pointer in a constructor to another object of action_class that needed some properties of inicio_common object.,

                            action_class.h contained

                            inicio_common *ref_common = nullptr;
                            

                            but action class cunstructor was capturing the common_object

                            action_class::action_class(inicio_common *common)
                            {
                              ref_common = common;
                            }
                            

                            so in the mainwindow I had common_object used by mainwindow and action_class_object because I run:

                            action_class *action_class_object = new (common_object);
                            

                            After I was done with the actions I deleted action_class_object with

                            delete(action_class_object);

                            and this seems to have deleted the common_object by deleting ref_common in cascade .... I guess
                            Does it make sense or is it just my wicked thinking?

                            Anyway I still don't get why if(param==nullptr) would crash instead of returning true;

                            C Offline
                            C Offline
                            Chris Kawa
                            Lifetime Qt Champion
                            wrote on 13 Jan 2023, 20:43 last edited by Chris Kawa
                            #15

                            @Seb-Tur said:

                            and this seems to have deleted the common_object by deleting ref_common in cascade .... I guess

                            delete something does not delete "something". It deletes the thing that "something" points to. "something" points to garbage after that. If you copy a pointer you copy a pointer. You're not copying the object it points to, so when you delete the object via one pointer both the original and the copied pointer point to the same garbage memory.

                            Anyway I still don't get why if(param==nullptr) would crash instead of returning true;

                            It's not the if that is crashing. It can't return anything because it's not even executed. It doesn't get to the comparison operator. this points to garbage, so this->param is trying to read a value from garbage memory. Reading from garbage memory is access violation and terminates your app.

                            C 1 Reply Last reply 14 Jan 2023, 08:55
                            2
                            • C Chris Kawa
                              13 Jan 2023, 20:43

                              @Seb-Tur said:

                              and this seems to have deleted the common_object by deleting ref_common in cascade .... I guess

                              delete something does not delete "something". It deletes the thing that "something" points to. "something" points to garbage after that. If you copy a pointer you copy a pointer. You're not copying the object it points to, so when you delete the object via one pointer both the original and the copied pointer point to the same garbage memory.

                              Anyway I still don't get why if(param==nullptr) would crash instead of returning true;

                              It's not the if that is crashing. It can't return anything because it's not even executed. It doesn't get to the comparison operator. this points to garbage, so this->param is trying to read a value from garbage memory. Reading from garbage memory is access violation and terminates your app.

                              C Offline
                              C Offline
                              Christian Ehrlicher
                              Lifetime Qt Champion
                              wrote on 14 Jan 2023, 08:55 last edited by
                              #16

                              @Chris-Kawa said in if (my_pointer==nullptr) causes segmentation fault:

                              this points to garbage, so this->param is trying to read a value from garbage memory. Reading from garbage memory is access violation and terminates your app.

                              Not that I said it in my first post... :)

                              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                              Visit the Qt Academy at https://academy.qt.io/catalog

                              1 Reply Last reply
                              1

                              12/16

                              13 Jan 2023, 19:26

                              • Login

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