Need help with a segfault problem
-
i am completing a program (sevenSegmentdisplay program) , i am getting a segfault( i believe ) after line 414 i belive. I am not sure how to resolve it . i added 2 cout statements too , just to find out where last line was getting executed ( not good with debugger )
code : https://ghostbin.com/paste/3qqvq
i did gdb a.exe
run
then i got this outputProgram received signal SIGSEGV, Segmentation fault. 0x00401979 in MultiSegmentLogic::deallocate (this=0x61fe7c) at deepmem.cpp:233 233 delete[] segs;
-
@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)
-
I'd strongly recommend initialising
segs
tonullptr
in theMultiSegmentLogic
constructor. Your current logic certainly allowssegs
to be accessed uninitialised, given certain values fornumSegs
.Try:
MultiSegmentLogic::MultiSegmentLogic(int numSegs = 0) : numSegs(0), segs(nullptr) { ... }
-
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; }
-
Good. Now you also have to do the same in
BooleanFunc::deallocate()
as @jsulm suggested forMultiSegmentLogic::deallocate()
, ievoid BooleanFunc::deallocate() { if (truthTable) { delete[] truthTable; truthTable = nullptr; } }
This is related to the crash point, because the
delete
inMultiSegmentLogic
invokes theBooleanFunc
destructors, and those destructors in turn callBooleanFunc::deallocate()
.Also, don't forget to set the breakpoint as @jsulm suggested. After that, try valgrind.
Cheers.
-
@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)
-
updated code here : https://ghostbin.com/paste/cvfq7
changes
void BooleanFunc::deallocate() { if (truthTable) { delete[] truthTable; truthTable = nullptr; } }
-
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.
-
this section right . but the
deallocate()
will only be called if this != rhs . still not sure how to fix itMultiSegmentLogic::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 .
-
Hi @jsulm and @Paul-Colby,
if (segs) { delete[] segs; segs = nullptr; }
This is an anti-pattern! Just make sure segs is
nullptr
before you callnew
and set it tonullptr
afterdelete
. No need to check fornullptr
beforedelete
:delete segs; segs = nullptr;
Quoting the C++ Reference:
"If this is a null-pointer, the function does nothing."
Regards