hello world COLLADA DOM app...

Hi! I’m having problems with my simple COLLADA DOM based model loader.

Here’s my code. I verified that it loads and parses the file without error messages, and it prints out the correct number of meshes.

Beyond that point I have however been unable to make it work. When I added the two lines below the comment “THESE TWO LINES CAUSE SEGMENTATION FAULT” below, the app crashed on the line “delete collada_dom;”, after working properly before they were added.

How can I fix this? Did I screw up something with the reference counts when I called “thisMesh->getSource_array()”? I tried to acquire the source array in order to get the info stored within the tags <source> inside the <mesh> tag in the document I was parsing (a model exported from Blender). Is there another way of getting the info within <source> </source> within <mesh> </mesh>?


	//////////////////////////////////////
	//LOAD FILE
	DAE *collada_dom = NULL;
	collada_dom = new DAE();

	char *file_ptr;
	file_ptr = textFileRead("test1.dae");
	daeInt result = collada_dom->load("TEST1.dae", file_ptr);

	if(result != DAE_OK) {
		std::cout << result << std::endl;
		return 0;
	}

	delete file_ptr; //release temp memory allocated by textFileRead

	//////////////////////////////////////
	//READ FILE CONTENTS
	daeUInt meshCount = collada_dom->getDatabase()->getElementCount(NULL, COLLADA_TYPE_MESH, NULL);
	std::cout << "Num meshes: " << meshCount << std::endl;

	//////////////////////////////////////
	//GET THE FIRST MESH
	domMesh *thisMesh;
	daeInt el_number = 0;
	daeInt error = collada_dom->getDatabase()->getElement((daeElement**)&thisMesh, el_number, NULL, COLLADA_TYPE_MESH, NULL);

	if(error!=DAE_OK) {
		std::cout << "error getting mesh" << std::endl;
		return 0;
	}

	//////////////////////////////////////
	//READ CONTENTS OF THE MESH

//THESE TWO LINES CAUSE SEGMENTATION FAULT:
	domSource_Array sourceArr = thisMesh->getSource_array();
	std::cout << "Num sources: " << sourceArr.getCount();

	//cleanup
	collada_dom->unload("TEST1.dae");
	delete collada_dom;

Hi, welcome to collada.org!

I think your code is crashing because of this line:

domSource_Array sourceArr = thisMesh->getSource_array();

Note that domMesh::getSource_array returns a reference to a domSource_Array. That is, it returns a domSource_Array&, and that’s how you should receive it:

domSource_Array& sourceArr = thisMesh->getSource_array();

There are a couple things going on here. Firstly, there used to be a bug in the Collada DOM where copying arrays would cause a crash. That bug has since been fixed, and when I ran your code with the latest DOM code in the Subversion repository on SourceForge, it didn’t crash. But even still, you don’t really want to make a copy of the array, for efficiency reasons. So if you change to domSource_Array& you won’t be making a copy of the array, and the code should run fine. In general, it’s important to pay close attention to the return types in the Collada DOM. We use references in a lot of places, and when calling functions that return references you should put the return value into a reference instead of copying it.

I have some other quick pointers while I’m looking at your code.

DAE *collada_dom = NULL;
collada_dom = new DAE();

This will work fine, but you may as well allocate it on the stack instead:

DAE collada_dom;

This way, you don’t have to worry about freeing the memory when you’re done. In your code you have two return points where you forget to delete the collada_dom object. That’ll cause a memory leak.

char *file_ptr;
file_ptr = textFileRead("test1.dae");
daeInt result = collada_dom->load("TEST1.dae", file_ptr);

if(result != DAE_OK) {
   std::cout << result << std::endl;
   return 0;
}

delete file_ptr; //release temp memory allocated by textFileRead

The DOM can read directly from a file. This should work fine:

daeInt result = collada_dom.load("test1.dae");