last day (14 days later) » 

3:13 PM
-2
Q: Need help Segmentation Fault in C++

Amos ChewI'm playing around with Octrees...and i've been getting "Program received signal SIGSEGV, Segmentation fault".....here are "sections" of my code and the comments showing which line the debugger stopped at and saying Segmentation fault....I was thinking it may be dealing with my "class Octree" con...

 
What debugging steps have you tried?
 
@immibis kinda stepping into n out or step into nxt line....not really familiar with debugging....but pls teach me if there's something else we can do with debugging in detecting the problem....^^
 
are you sure getOctantContainingPoint haven't returned a number >=8 ?
 
"Debugging" generally means "finding out why your program is broken" - so have you tried anything previously to find out why the program is broken? (Such as running it in a debugger, or in valgrind, or sprinkling printf calls everywhere so you can see what the last thing it prints before it crashes is)
(and by "steps" I just meant things you've done)
 
I see you already know how to use a debugger. So, disable all breakpoints and run the program. When it segfaults, the debugger will be able to tell you where. If you can't see the error from the code, place a breakpoint on that line, execute again, stop just before the segfault and inspect all variables in scope, looking for anything suspicious.
 
3:13 PM
@swang oh my....indeed it returned an 8 at one point....
 
Is children[octant] NULL?
 
@immibis yeap....i did put in cout here n there.....n basically detected it stopped at tht line shown above....
 
@AmosChew, if you are accessing children[8] then you've passed the boundary, it's undefined behaviour and could cause seg fault
 
@swang it's really strange why it could return an 8.....frm the function i thought it suppose to return 7 n below...
@swang i'm really sorry....totally my bad....getOctantContainingPoint() does not return an 8 at all....seen it wrongly becoz i had another "cout" in the main.cpp.....forgotten to erase tht....but i'll debug the whole code again....checking the variable values
 
Can you add the whole class definition (that is your //....other functions). Isn't there an overloading function?
 
3:13 PM
@ott-- done editing Octree.h
 
You are using a recursive algorithm. One thing that can go wrong here is if the recursion does not terminate properly, it will continue until it runs out of stack space and then it will segfault. Look at the debugger's back trace when it crashes. If it looks like an infinite list of insert calls, that's the problem.
 
Like Zan Lynx said, maybe your data points drills down very deep into one branch of your octree which causes the program to run out of stack space. In that cause you want want to add a minimum octree dimension.
 
@ZanLynx I'm now referring to the Call Stack after debugging....it has 8 situations...situation 0-4 show "?? ()".....situation 5 shows "msvcrt!malloc()".....situation 6 shows "operator new(unsigned int) ()".....situation 7-8 show "?? ()"....
 
The more I think about it the more confident I am that your program is running out of stack space because it is creating a very deep tree. All the octree implementations(in games) I have seen before always had a minimum box size, and 7 or 8 level deep.
 
@TonyJiang minimum octree dimension as in number of subdivision process..?
 
3:13 PM
@AmosChew Yes you can see it as number of subdivisions or a minimum halfDimension. You will also have to change OctreePoint *data to vector<OctreePoint*> data.
 
@TonyJiang got it....will try to implement it....ty so much for the info...^^
 

last day (14 days later) »