Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Need help with a segfault problem
QtWS25 Last Chance

Need help with a segfault problem

Scheduled Pinned Locked Moved Solved C++ Gurus
c++11segfaultc++
13 Posts 4 Posters 3.8k Views
  • 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.
  • Q Qjay
    20 Feb 2018, 11:40

    @jsulm said in Need help with a segfault problem:

    void MultiSegmentLogic::deallocate()
    {
    if (segs) {
    delete[] segs;
    segs = nullptr;
    }
    }

    hey @jsulm thanks for quick reply but there is still same error

    Program received signal SIGSEGV, Segmentation fault. 0x00401983 in MultiSegmentLogic::deallocate (this=0x61fe7c) at deepmem.cpp:234 234 delete[] segs; (gdb)

    J Offline
    J Offline
    jsulm
    Lifetime Qt Champion
    wrote on 20 Feb 2018, 11:46 last edited by
    #4

    @Qjay Then please put a break point at delete[] segs; and see the status (segs pointing to valid chunk of memory?)

    https://forum.qt.io/topic/113070/qt-code-of-conduct

    Q 1 Reply Last reply 20 Feb 2018, 12:05
    2
    • P Offline
      P Offline
      Paul Colby
      wrote on 20 Feb 2018, 11:48 last edited by Paul Colby
      #5

      I'd strongly recommend initialising segs to nullptr in the MultiSegmentLogic constructor. Your current logic certainly allows segs to be accessed uninitialised, given certain values for numSegs.

      Try:

      MultiSegmentLogic::MultiSegmentLogic(int numSegs = 0) : numSegs(0), segs(nullptr)
      {
         ...
      }
      
      1 Reply Last reply
      5
      • Q Offline
        Q Offline
        Qjay
        wrote on 20 Feb 2018, 11:54 last edited by
        #6

        hey , i tried still same error

        here is updated code : https://ghostbin.com/paste/q8d5e

        changes

        
        MultiSegmentLogic::MultiSegmentLogic(int numSegs = 0) : numSegs(0), segs(nullptr){
            
           if (!setNumSegs(numSegs))
              numSegs = 0;
        }
        
        1 Reply Last reply
        0
        • P Offline
          P Offline
          Paul Colby
          wrote on 20 Feb 2018, 12:03 last edited by Paul Colby
          #7

          Good. Now you also have to do the same in BooleanFunc::deallocate() as @jsulm suggested for MultiSegmentLogic::deallocate(), ie

          void BooleanFunc::deallocate()
          {
              if (truthTable) {
                  delete[] truthTable;
                  truthTable = nullptr;
              }
          }
          

          This is related to the crash point, because the delete in MultiSegmentLogic invokes the BooleanFunc destructors, and those destructors in turn call BooleanFunc::deallocate().

          Also, don't forget to set the breakpoint as @jsulm suggested. After that, try valgrind.

          Cheers.

          Q 1 Reply Last reply 20 Feb 2018, 12:09
          1
          • J jsulm
            20 Feb 2018, 11:46

            @Qjay Then please put a break point at delete[] segs; and see the status (segs pointing to valid chunk of memory?)

            Q Offline
            Q Offline
            Qjay
            wrote on 20 Feb 2018, 12:05 last edited by
            #8

            @jsulm i did breakpoint with this

            break MultiSegmentLogic::deallocate

            output

            Breakpoint 1, MultiSegmentLogic::deallocate (this=0x61fe6c) at deepmem.cpp:234
            234         if (segs) {
            (gdb) step
            235             delete[] segs;
            (gdb) step
            
            Program received signal SIGSEGV, Segmentation fault.
            0x00401995 in MultiSegmentLogic::deallocate (this=0x61fe6c) at deepmem.cpp:235
            235             delete[] segs;
            (gdb)
            
            
            1 Reply Last reply
            0
            • P Paul Colby
              20 Feb 2018, 12:03

              Good. Now you also have to do the same in BooleanFunc::deallocate() as @jsulm suggested for MultiSegmentLogic::deallocate(), ie

              void BooleanFunc::deallocate()
              {
                  if (truthTable) {
                      delete[] truthTable;
                      truthTable = nullptr;
                  }
              }
              

              This is related to the crash point, because the delete in MultiSegmentLogic invokes the BooleanFunc destructors, and those destructors in turn call BooleanFunc::deallocate().

              Also, don't forget to set the breakpoint as @jsulm suggested. After that, try valgrind.

              Cheers.

              Q Offline
              Q Offline
              Qjay
              wrote on 20 Feb 2018, 12:09 last edited by
              #9

              @Paul-Colby

              updated code here : https://ghostbin.com/paste/cvfq7

              changes

              void BooleanFunc::deallocate()
              {
                  if (truthTable) {
                      delete[] truthTable;
                      truthTable = nullptr;
                  }
              }
              
              1 Reply Last reply
              0
              • P Offline
                P Offline
                Paul Colby
                wrote on 20 Feb 2018, 12:16 last edited by
                #10

                The (next) problem is with your copy constructor. (valgrind show'd it immediately)

                > g++ foo.cpp && valgrind ./a.out
                ...
                ==9657== Conditional jump or move depends on uninitialised value(s)
                ==9657==    at 0x109314: MultiSegmentLogic::deallocate() (in /home/paul/src/extern/forum-qt-io/segfault/a.out)
                ==9657==    by 0x109405: MultiSegmentLogic::operator=(MultiSegmentLogic const&) (in /home/paul/src/extern/forum-qt-io/segfault/a.out)
                ==9657==    by 0x1093D8: MultiSegmentLogic::MultiSegmentLogic(MultiSegmentLogic const&) (in /home/paul/src/extern/forum-qt-io/segfault/a.out)
                ==9657==    by 0x109FB3: main (in /home/paul/src/extern/forum-qt-io/segfault/a.out)
                ==9657== 
                ==9657== Conditional jump or move depends on uninitialised value(s)
                ==9657==    at 0x109325: MultiSegmentLogic::deallocate() (in /home/paul/src/extern/forum-qt-io/segfault/a.out)
                ==9657==    by 0x109405: MultiSegmentLogic::operator=(MultiSegmentLogic const&) (in /home/paul/src/extern/forum-qt-io/segfault/a.out)
                ==9657==    by 0x1093D8: MultiSegmentLogic::MultiSegmentLogic(MultiSegmentLogic const&) (in /home/paul/src/extern/forum-qt-io/segfault/a.out)
                ==9657==    by 0x109FB3: main (in /home/paul/src/extern/forum-qt-io/segfault/a.out)
                ==9657== 
                ==9657== Use of uninitialised value of size 8
                ==9657==    at 0x10933B: MultiSegmentLogic::deallocate() (in /home/paul/src/extern/forum-qt-io/segfault/a.out)
                ==9657==    by 0x109405: MultiSegmentLogic::operator=(MultiSegmentLogic const&) (in /home/paul/src/extern/forum-qt-io/segfault/a.out)
                ==9657==    by 0x1093D8: MultiSegmentLogic::MultiSegmentLogic(MultiSegmentLogic const&) (in /home/paul/src/extern/forum-qt-io/segfault/a.out)
                ==9657==    by 0x109FB3: main (in /home/paul/src/extern/forum-qt-io/segfault/a.out)
                ==9657== 
                ==9657== Conditional jump or move depends on uninitialised value(s)
                ==9657==    at 0x10935A: MultiSegmentLogic::deallocate() (in /home/paul/src/extern/forum-qt-io/segfault/a.out)
                ==9657==    by 0x109405: MultiSegmentLogic::operator=(MultiSegmentLogic const&) (in /home/paul/src/extern/forum-qt-io/segfault/a.out)
                ==9657==    by 0x1093D8: MultiSegmentLogic::MultiSegmentLogic(MultiSegmentLogic const&) (in /home/paul/src/extern/forum-qt-io/segfault/a.out)
                ==9657==    by 0x109FB3: main (in /home/paul/src/extern/forum-qt-io/segfault/a.out)
                ==9657== 
                ==9657== Use of uninitialised value of size 8
                ==9657==    at 0x108F76: BooleanFunc::deallocate() (in /home/paul/src/extern/forum-qt-io/segfault/a.out)
                ==9657==    by 0x108CFF: BooleanFunc::~BooleanFunc() (in /home/paul/src/extern/forum-qt-io/segfault/a.out)
                ==9657==    by 0x109367: MultiSegmentLogic::deallocate() (in /home/paul/src/extern/forum-qt-io/segfault/a.out)
                ==9657==    by 0x109405: MultiSegmentLogic::operator=(MultiSegmentLogic const&) (in /home/paul/src/extern/forum-qt-io/segfault/a.out)
                ==9657==    by 0x1093D8: MultiSegmentLogic::MultiSegmentLogic(MultiSegmentLogic const&) (in /home/paul/src/extern/forum-qt-io/segfault/a.out)
                ==9657==    by 0x109FB3: main (in /home/paul/src/extern/forum-qt-io/segfault/a.out)
                ==9657== 
                ==9657== Invalid read of size 8
                ==9657==    at 0x108F76: BooleanFunc::deallocate() (in /home/paul/src/extern/forum-qt-io/segfault/a.out)
                ==9657==    by 0x108CFF: BooleanFunc::~BooleanFunc() (in /home/paul/src/extern/forum-qt-io/segfault/a.out)
                ==9657==    by 0x109367: MultiSegmentLogic::deallocate() (in /home/paul/src/extern/forum-qt-io/segfault/a.out)
                ==9657==    by 0x109405: MultiSegmentLogic::operator=(MultiSegmentLogic const&) (in /home/paul/src/extern/forum-qt-io/segfault/a.out)
                ==9657==    by 0x1093D8: MultiSegmentLogic::MultiSegmentLogic(MultiSegmentLogic const&) (in /home/paul/src/extern/forum-qt-io/segfault/a.out)
                ==9657==    by 0x109FB3: main (in /home/paul/src/extern/forum-qt-io/segfault/a.out)
                ==9657==  Address 0x7fbb2fe8 is not stack'd, malloc'd or (recently) free'd
                ==9657== 
                ==9657== 
                ==9657== Process terminating with default action of signal 11 (SIGSEGV)
                ==9657==  Access not within mapped region at address 0x7FBB2FE8
                ==9657==    at 0x108F76: BooleanFunc::deallocate() (in /home/paul/src/extern/forum-qt-io/segfault/a.out)
                ==9657==    by 0x108CFF: BooleanFunc::~BooleanFunc() (in /home/paul/src/extern/forum-qt-io/segfault/a.out)
                ==9657==    by 0x109367: MultiSegmentLogic::deallocate() (in /home/paul/src/extern/forum-qt-io/segfault/a.out)
                ==9657==    by 0x109405: MultiSegmentLogic::operator=(MultiSegmentLogic const&) (in /home/paul/src/extern/forum-qt-io/segfault/a.out)
                ==9657==    by 0x1093D8: MultiSegmentLogic::MultiSegmentLogic(MultiSegmentLogic const&) (in /home/paul/src/extern/forum-qt-io/segfault/a.out)
                ==9657==    by 0x109FB3: main (in /home/paul/src/extern/forum-qt-io/segfault/a.out)
                ==9657==  If you believe this happened as a result of a stack
                ==9657==  overflow in your program's main thread (unlikely but
                ==9657==  possible), you can try to increase the size of the
                ==9657==  main thread stack using the --main-stacksize= flag.
                ==9657==  The main thread stack size used in this run was 8388608.
                test 1test 2==9657== 
                ==9657== HEAP SUMMARY:
                ==9657==     in use at exit: 357 bytes in 8 blocks
                ==9657==   total heap usage: 11 allocs, 4 frees, 74,101 bytes allocated
                ==9657== 
                ==9657== LEAK SUMMARY:
                ==9657==    definitely lost: 0 bytes in 0 blocks
                ==9657==    indirectly lost: 0 bytes in 0 blocks
                ==9657==      possibly lost: 0 bytes in 0 blocks
                ==9657==    still reachable: 357 bytes in 8 blocks
                ==9657==                       of which reachable via heuristic:
                ==9657==                         newarray           : 80 bytes in 1 blocks
                ==9657==         suppressed: 0 bytes in 0 blocks
                ==9657== Rerun with --leak-check=full to see details of leaked memory
                ==9657== 
                ==9657== For counts of detected and suppressed errors, rerun with: -v
                ==9657== Use --track-origins=yes to see where uninitialised values come from
                ==9657== ERROR SUMMARY: 10 errors from 10 contexts (suppressed: 0 from 0)
                

                At a quick glance it looks like your copy constructor is deallocating before initialising segs.

                Cheers.

                PS I highly recommend you explore valgrind - such a handy tool for things like this.

                1 Reply Last reply
                3
                • Q Offline
                  Q Offline
                  Qjay
                  wrote on 20 Feb 2018, 12:24 last edited by Qjay
                  #11

                  this section right . but the deallocate() will only be called if this != rhs . still not sure how to fix it

                  MultiSegmentLogic::MultiSegmentLogic(const MultiSegmentLogic &rhs)
                  {
                     *this = rhs;
                  }
                  MultiSegmentLogic & MultiSegmentLogic::operator=(const MultiSegmentLogic &rhs)
                  {
                     if (this != &rhs)
                     {
                        deallocate();
                        this->numSegs = rhs.numSegs;
                        allocateSegsArray(numSegs);
                        for (int i = 0; i < numSegs; i++)
                           this->segs[i] = rhs.segs[i];
                     }
                  
                     return *this;
                  }
                  
                  

                  Thanks i will look and learn more about valgrind .

                  1 Reply Last reply
                  0
                  • Q Offline
                    Q Offline
                    Qjay
                    wrote on 20 Feb 2018, 12:32 last edited by Qjay
                    #12

                    alright i got it .

                    thanks @Paul-Colby , @jsulm .

                    the problem was indeed deallocate() . i have removed it . program completes without any errors

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      aha_1980
                      Lifetime Qt Champion
                      wrote on 22 Feb 2018, 19:23 last edited by
                      #13

                      Hi @jsulm and @Paul-Colby,

                      if (segs) {
                          delete[] segs;
                          segs = nullptr;
                      }
                      

                      This is an anti-pattern! Just make sure segs is nullptr before you call new and set it to nullptr after delete. No need to check for nullptr before delete:

                      delete segs;
                      segs = nullptr;
                      

                      Quoting the C++ Reference:

                      "If this is a null-pointer, the function does nothing."

                      Regards

                      Qt has to stay free or it will die.

                      1 Reply Last reply
                      4

                      13/13

                      22 Feb 2018, 19:23

                      • Login

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