SUEntitiesFill throw the exception: Access violation reading location 0x0000000000000000

I have tried to add entities into the component definition, then create the instance of the component definition, add those into a model. However, the SketchupApi throw the exception: Access violation reading location 0x0000000000000000 with the function SUEntitiesFill. And the following is code segment.

SUEntitiesRef entities = SU_INVALID;
SUComponentDefinitionRef comp_def = SU_INVALID;
SUEntitiesRef comp_def_entities = SU_INVALID;
SUComponentInstanceRef instance = SU_INVALID;
SUModelGetEntities(model, &entities);
for (int i = 0; i < ObjCount; i++) {
	SUSetInvalid(comp_def);
	SUSetInvalid(comp_def_entities);
	SUSetInvalid(instance);
	SU_CALL(SUComponentDefinitionCreate(&comp_def));
	SU_CALL(SUComponentDefinitionGetEntities(comp_def, &comp_def_entities));
	SU_CALL(SUEntitiesFill(comp_def_entities, input[i], false));
	string comp_name = "component_" + std::to_string(i);
	SU_CALL(SUComponentDefinitionSetName(comp_def, comp_name.c_str()));
	SUComponentBehavior behavior;
	behavior.component_always_face_camera = true;
	SU_CALL(SUComponentDefinitionSetBehavior(comp_def, &behavior));
	SU_CALL(SUModelAddComponentDefinitions(model, 1, &comp_def));
	SU_CALL(SUComponentDefinitionCreateInstance(comp_def, &instance));
	SU_CALL(SUEntitiesAddInstance(entities, instance, NULL));
}

When I test another way, just add SUGeometryInputRef directly into the model, it works well. And the none error code segment is as follow:

SUEntitiesRef entities = SU_INVALID;
SUModelGetEntities(model, &entities);
for (int i = 0; i<ObjCount; i++) {
	SU_CALL(SUEntitiesFill(entities, input[i], false));
}

Could anyone help to check the code with component addition? What’s wrong of my completion?
Thanks in advance!

Perhaps you omitted a call to SUComponentDefinitionGetBehavior to get a valid SUComponentBehavior Struct before assigning field values ?

Thanks for your reply, but I don’t think it’s the reason. I have tried to delete the code about assign the behavior to the ComponentDefinition. It also has an exception at the function SUEntitiesFill.

Otherwise, a wired thing, in the for loop, when the i=0, all the code could be executed successfully.

Besides, I construct input[i] in the same way…

Or could you help to provide the right way to create a ComponentDefinition?

Looking at the XML importer samples provided with the SDK, the code comments seems to indicate that you should immediately after creating the definition object, add it to the model object’s definition collection before doing any other operations upon the definition object.

So your code looks to be out of order. Try it in this order …

for (int i = 0; i < ObjCount; i++) {
    SUSetInvalid(comp_def);
    SUSetInvalid(comp_def_entities);
    SUSetInvalid(instance);
    // Create the definition
    SU_CALL(SUComponentDefinitionCreate(&comp_def));
    SU_CALL(SUModelAddComponentDefinitions(model, 1, &comp_def));
    // Give it a name
    string comp_name = "component_" + std::to_string(i);
    SU_CALL(SUComponentDefinitionSetName(comp_def, comp_name.c_str()));
    // Fill the definition's entities collection
    SU_CALL(SUComponentDefinitionGetEntities(comp_def, &comp_def_entities));
    SU_CALL(SUEntitiesFill(comp_def_entities, input[i], false));
    // Set the behvaior
    SUComponentBehavior behavior;
    behavior.component_always_face_camera = true;
    SU_CALL(SUComponentDefinitionSetBehavior(comp_def, &behavior));
    // Create the instance
    SU_CALL(SUComponentDefinitionCreateInstance(comp_def, &instance));
    SU_CALL(SUEntitiesAddInstance(entities, instance, NULL));
}

Lucky~ It works well! Many thanks for Dan!

Also, it’s the right way to create ComponentDifinition, and the trick is paying attention to the order of the code.

PS: In Sketchup C API, there are many wired bugs caused by the code’s order, for a ‘noob’, plz check the official sample code more and listen to Dan’s suggestion. LOL

Or is there any other advice to avoid similar bug? Master Dan :slight_smile:

1 Like

I’ve filed a documentation issue in the tracker …

There is a primer page on using the C API that every coder should read.

http://extensions.sketchup.com/developers/sketchup_c_api/sketchup/index.html

Also there are many good code snippets in the supplied "samples" folder of the SDK package.

Got it! Appreciate for all.

1 Like