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.2k 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.
  • Seb TurS Seb Tur

    Hi
    Basic question but drives me mad for the whole day.

    I have a class inicio_common

    For this class I declared a pointer and a method in

    inicio_common.h :

    private:
      parameter *param=nullptr;
    
    public:
     QString parameter_read(QString param_name, bool force_read = false);
    

    I define the function in
    inicio_common.cpp

    QString inicio_common::parameter_read(QString param_name, bool force_read)
    {
        qDebug()<<"param read enter";
    
        qDebug()<<"param name="+param_name;
    
    
        if (this->param==nullptr)
        {
            qDebug()<<"param obj is NULL";
            this->param = new parameter(receiver_app);
    
        }
        else
        {
            qDebug()<<"param obj ok";
    
        }
        qDebug()<<"param obj checked";
    
        QString val=this->param->read(param_name,force_read);
        qDebug()<<"param val="<<val;
        return (val);
    }
    
    
    

    This function runs fine several times and suddenly on one iteration it throws a segmantation fault on
    if (this->param==nullptr)
    and never reaches anything after that.

    Any hints for a scenario ?

    Christian EhrlicherC Offline
    Christian EhrlicherC Offline
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on last edited by
    #2

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

    Any hints for a scenario ?

    Use a debugger, see what's going wrong. I would guess 'this' is either a nullptr or garbage.

    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
    2
    • JoeCFDJ Offline
      JoeCFDJ Offline
      JoeCFD
      wrote on 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
      • Seb TurS Offline
        Seb TurS Offline
        Seb Tur
        wrote on last edited by
        #4

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

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

        Christian EhrlicherC JoeCFDJ 2 Replies Last reply
        0
        • Seb TurS Seb Tur

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

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

          Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 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
          • Seb TurS Seb Tur

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

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

            JoeCFDJ Offline
            JoeCFDJ Offline
            JoeCFD
            wrote on 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
            • Chris KawaC Online
              Chris KawaC Online
              Chris Kawa
              Lifetime Qt Champion
              wrote on 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.

              Seb TurS 1 Reply Last reply
              1
              • Chris KawaC Chris Kawa

                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.

                Seb TurS Offline
                Seb TurS Offline
                Seb Tur
                wrote on 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.

                Christian EhrlicherC 1 Reply Last reply
                0
                • JoeCFDJ Offline
                  JoeCFDJ Offline
                  JoeCFD
                  wrote on 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?

                  Seb TurS 1 Reply Last reply
                  0
                  • Seb TurS Seb Tur

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

                    Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on 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
                    • JoeCFDJ JoeCFD

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

                      Seb TurS Offline
                      Seb TurS Offline
                      Seb Tur
                      wrote on 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();

                      JoeCFDJ 1 Reply Last reply
                      0
                      • Seb TurS Seb Tur

                        @JoeCFD

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

                        than contructed at the begining of mainwindow.cpp

                        common_object = new inicio_common();

                        JoeCFDJ Offline
                        JoeCFDJ Offline
                        JoeCFD
                        wrote on last edited by
                        #12

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

                        1 Reply Last reply
                        0
                        • Chris KawaC Online
                          Chris KawaC Online
                          Chris Kawa
                          Lifetime Qt Champion
                          wrote on 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
                          • Seb TurS Offline
                            Seb TurS Offline
                            Seb Tur
                            wrote on 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;

                            Chris KawaC 1 Reply Last reply
                            0
                            • Seb TurS Seb Tur

                              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;

                              Chris KawaC Online
                              Chris KawaC Online
                              Chris Kawa
                              Lifetime Qt Champion
                              wrote on 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.

                              Christian EhrlicherC 1 Reply Last reply
                              2
                              • Chris KawaC Chris Kawa

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

                                Christian EhrlicherC Offline
                                Christian EhrlicherC Offline
                                Christian Ehrlicher
                                Lifetime Qt Champion
                                wrote on 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

                                • Login

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