Help Debugging Plugin

I am getting a crash in my code, but it often occurs after I have exited and returned to sketch up, so it has been difficult to find what in the code is causing the crash.

This is the call stack. Most of the time the error occurs two lines below at 'SketchUp.exe!.."

Any advice on how to go about debugging the issue?

I see you posted this under SketchUp SDK - but I’m confused to why SketchUp.exe is within your stack. The C API is currently not working on live models.

Can you provide more information - right now it not nearly enough to even take a guess.

What Windows version? SketchUp version? Bitness? Are you using the C API from within a Ruby C Extensions - is that how you get SketchUp.exe in your call stack?

I am using windows 7 and sketchup make 2015 64bit. I call my .so file from my ruby extension. I am using the sdk to build my models and returning to ruby the path to the file.

Without some code to reproduce this it’s impossible to tell the reason for the crash you see.

When you say “exited and returned to SketchUp” - what is exiting? An application? Or do you mean your function?

I mean when I am debugging. My ruby code calls my .so file. My c++ opens a dialog with the user to pick which kind of model they would like to insert. Once they select, I make the model using the sdk and save it. The path where it is saved is returned to Ruby. I have been stepping through the c++ code to find where the error occurs, but it occurs after I return to the ruby code. The unhandled exception pops up after the return. I think I have a memory problem somewhere. I have checked and double checked all my pointers and they are deleted properly. I am releasing my sdk model and my geometryinputref.

I was just wondering if it was common with working in the sdk to get that kind of error (of an access violation in the SketchUp.exe) and if there was advice on debugging that.

Not without having any insight to the code I’m afraid. All the access violation exception tells us is the invalid memory access being attempted.

I will keep looking. I was wondering if maybe it was in the way I create my geometry. I am creating an vector of geometryinputrefs and using those to fill the entities. Then releasing them. Am I doing this correctly or would this cause an error?

void SKPModel::FillEntities(std::vector<SUGeometryInputRef>geo_input){
	
	// Get the model entities
	SUEntitiesRef entities = SU_INVALID;
	SUModelGetEntities(model, &entities);

	size_t count = geo_input.size();

	// Loop through geometry vector filling the model entites
	for(size_t i=0; i < count; i++){
		SUEntitiesFill(entities, geo_input[i], true);
		SUResult result = SUGeometryInputRelease(&geo_input[i]);

	}//end for(size_t i=0; i < count...	
}

void SKPModel::AddFaceNoTexture(point3d *pPoints, int count, std::vector<SUGeometryInputRef>&geo_input){
	
	// Create GeometryInputRef to add face
	SUGeometryInputRef input = SU_INVALID;
	SUGeometryInputCreate(&input);

	SUPoint3D* vertices = new SUPoint3D[3];
	
	// Convert the vertex points to SUPoint3D
	// Add the face vertex points to the GeometryInputRef
	for (int i=0; i < count; i++) {
		SUPoint3D t;
		t.x = pPoints[i].x;
		t.y = pPoints[i].y;
		t.z = pPoints[i].z;
		vertices[i] = t;
		SUGeometryInputAddVertex(input, &t);
	}
	
	SULoopInputRef loop = SU_INVALID;
	SULoopInputCreate(&loop);

	for(size_t j=0; j < count; j++){
		SULoopInputAddVertexIndex(loop, j);
	}

	// Add face to the GeometryInputRef
	size_t face_index0;
	SUGeometryInputAddFace(input, &loop, &face_index0);

	// Add the geometry to the geometry vector to later fill the model entities
	geo_input.push_back(input);

	delete[] vertices;
	
}

It might be in this code. I do not release anything here.

void SKPModel::hideEdges(){

	// Get the model entities
	SUEntitiesRef entities = SU_INVALID;
	SUModelGetEntities(model, &entities);
	
	size_t edgeCount;

	// Get the edges and add to edges vector
	SUEntitiesGetNumEdges(entities, false, &edgeCount);
	std::vector<SUEdgeRef> edges(edgeCount);
	SUEntitiesGetEdges(entities, false, edgeCount, &edges[0], &edgeCount);

	std::vector<SUDrawingElementRef> elems(edgeCount);

	// Convert the SUEdgeRef edges to SUDrawingElements
	for (size_t i=0; i < edgeCount; i++) {
		SUEdgeSetSoft(edges[i], true);
		SUEdgeSetSmooth(edges[i], true);
		SUDrawingElementRef elem;
		elem = SUEdgeToDrawingElement(edges[i]);
		elems.push_back(elem);
	}

	// Use SUDrawingElementSetHidden to set the edges to hidden
	for (size_t i=0; i < elems.size(); i++) {
		SUDrawingElementSetHidden(elems[i], true);
	}
}

(Small side note:)
Are you creating a single GeometryInput per face you are creating? (You know it can accept multiple faces, right?)

Yeah, I tried that but I am getting my points 3 at a time and I could never figure out setting the vertices and the input loop.

Something curious I just found is that if I run my plugin and save out the .skp model but do not import it, it works with out crashing.

I changed mode.import to definitions.load and I have not got a crash. Is one safer to use over the other?

Shouldn’t be… Given the SKP you generate. If you save of a copy of that and manually load it via the Ruby API (import and load) do you see a crash then?