Segfault in "_d_dynamic_cast ()"
Minas Mina
minas_mina1990 at hotmail.co.uk
Thu Jan 24 02:14:28 PST 2013
I am trying to create a BVH tree structure to speed up
raytracing. So far it has been fine. I have created the BVH tree.
It works for 202 triangles/spheres.
However, when the scene has more spheres/triangles, I get a
segmentation fault when the rays are traces, not when the tree is
being built.
To let you understand, there is a Surface class, from which
"BVHNode", "Sphere" and "Triangle" inherit.
I have written a small function that prints the BVH tree
recursively. Here it is:
void printBVH(Surface root, int i = 0, string str = "")
{
if( root is null )
return;
writeln("------PRINT()------");
writeln("icy");//writeln(root.boundingBox());
writeln("name = ", root.name, " depth = ", i, " [", str, "]");
writeln("------~~~~~~~------\n");
if( (cast(BVHNode)root) !is null ) // OOPS! SEG FAULT HERE
{
printBVH((cast(BVHNode)(root)).left, i+1, "left");
printBVH((cast(BVHNode)(root)).right, i+1, "right");
}
}
And I pass to printBVH() the root node that the function that
builds the tree has returned.
// replaces every two Surfaces with one that is their parent.
Proceeds until there is only one surface, which is the root.
BVHNode createBVHTree2(Surface[] objects, ubyte axis = 0, int
depth = 0)
{
import std.algorithm, std.stdio;
BVHNode root;
// sort left out for now until the seg fault is fixed
//sort!("(a.boundingBox().min.x + a.boundingBox().max.x) * 0.5f
< (b.boundingBox().min.x + b.boundingBox().max.x) * 0.5f",
SwapStrategy.unstable)(objects);
while( objects.length > 1 )
{
writeln("--- Level ---");
foreach(o; objects)
write("[", o.name, "] ");
writeln();
auto temp = new Surface[objects.length/2 + 1];
auto sz = 0UL;
for(auto i = 0UL; i < objects.length; i += 2)
{
writeln("i = ", i, " sz = ", sz+1);
BVHNode parent = new BVHNode();
parent.name = "p";
parent.left = objects[i];
if( i + 1 < objects.length )
{
parent.right = objects[i+1];
auto box1 = objects[i].boundingBox(), box2 =
objects[i+1].boundingBox();
parent.box = combine(box1, box2);
}
else
{
parent.right = null;
parent.box = objects[i].boundingBox();
}
temp[sz++] = parent;
}
temp.length = sz;
objects = temp;
}
root = cast(BVHNode)objects[0];
return root;
}
Ok, so when I print the scene using printBVH(), I get a segfault
in the line:
if( (cast(BVHNode)root) !is null )
GDB says: Program received signal SIGSEGV, Segmentation fault.
0x00000000004b55fc in _d_dynamic_cast ()
Note that I have written the createBVHTree() and printBVH()
functions in Java to see if it would be different. The code works
in Java. I guess something is wrong with the compiler here and
the way it handles recursion(?) -- By the way the code seg faults
at the 11 depth level. Are there any know bugs for this?
Thanks.
More information about the Digitalmars-d-learn
mailing list