www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Segfault in "_d_dynamic_cast ()"

reply "Minas Mina" <minas_mina1990 hotmail.co.uk> writes:
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.
Jan 24 2013
parent reply "Maxim Fomin" <maxim maxim-fomin.ru> writes:
On Thursday, 24 January 2013 at 10:14:29 UTC, Minas Mina wrote:
 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.

 <skipped>

 Thanks.
Requests for debugging help without source code are likely to be buried in archives silently. Perhaps you can look at _d_dynamic_cast source code (https://github.com/D-Programming-Language/druntime/blob/master src/rt/cast_.d#L69) and figure out the exact statement which produces segfault.
Jan 24 2013
parent reply "Minas Mina" <minas_mina1990 hotmail.co.uk> writes:
On Thursday, 24 January 2013 at 13:22:20 UTC, Maxim Fomin wrote:
 On Thursday, 24 January 2013 at 10:14:29 UTC, Minas Mina wrote:
 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.

 <skipped>

 Thanks.
Requests for debugging help without source code are likely to be buried in archives silently. Perhaps you can look at _d_dynamic_cast source code (https://github.com/D-Programming-Language/druntime/blob/master src/rt/cast_.d#L69) and figure out the exact statement which produces segfault.
The code can be found here: https://github.com/minas1/D_Raytracing The code that creates the BVH tree is in bvh.d. The code that prints the tree is in main.d.
Jan 24 2013
parent reply "Minas Mina" <minas_mina1990 hotmail.co.uk> writes:
I found what the root of all evil was - The GC.

After disabling it, the program runs fine.
Jan 25 2013
parent reply "Maxim Fomin" <maxim maxim-fomin.ru> writes:
On Friday, 25 January 2013 at 15:15:32 UTC, Minas Mina wrote:
 I found what the root of all evil was - The GC.

 After disabling it, the program runs fine.
Perhaps you was working with C code, GC + legacy code sometimes lead to logical memory errors. However GC per se is unlikely to cause any errors (I remember Walter was telling that neither he nor anybody faced issues with GC). Also your code may still have errors but absence of GC hides them.
Jan 25 2013
next sibling parent "Minas Mina" <minas_mina1990 hotmail.co.uk> writes:
Maybe. I am re-writing the code in C++ to see, and also to 
compare the performance.
Jan 25 2013
prev sibling parent reply "Minas Mina" <minas_mina1990 hotmail.co.uk> writes:
On Friday, 25 January 2013 at 16:19:15 UTC, Maxim Fomin wrote:
 On Friday, 25 January 2013 at 15:15:32 UTC, Minas Mina wrote:
 I found what the root of all evil was - The GC.

 After disabling it, the program runs fine.
Perhaps you was working with C code, GC + legacy code sometimes lead to logical memory errors. However GC per se is unlikely to cause any errors (I remember Walter was telling that neither he nor anybody faced issues with GC). Also your code may still have errors but absence of GC hides them.
I have written the same program in C++ -- and I get no seg-fault. Well I don't know for sure that something isn't wrong with my code, but I suspect it is the GC that is messing things up.
Jan 25 2013
parent David <d dav1d.de> writes:
 I have written the same program in C++ -- and I get no seg-fault. Well I
 don't know for sure that something isn't wrong with my code, but I
 suspect it is the GC that is messing things up.
This is definitly possible, I also had random segfaults GC related. It worked fine on my PC, but crashed on someone elses Ubuntu (same DMD version, same architecture). I also had random crashes in threads when they were about to finish, GC.disable() "solved" it, also rewriting the whole code (fortunatly I had to do that anyways)
Jan 25 2013