SU crashes on C extension (SDK C API)

sdkapic

#1

I do something wrong but I can’t find the problem.

The C++ code:

VALUE create_layer_materials(VALUE self, VALUE filename){
        SUInitialize();
        SUModelRef model = SU_INVALID;
        
        // Load the SketchUp model.
        std::string path = GetRubyString(filename);
        SUModelCreateFromFile(&model, path.c_str());
       
        // Load materials
        size_t num_materials;
        size_t count;
        
        SUModelGetNumMaterials(model, &num_materials);
        std::vector<SUMaterialRef> materials(num_materials);
        
        for (size_t i = 0; i < num_materials; ++i) {
            SUSetInvalid(materials[i]);
        }
        
        SUModelGetMaterials(model, num_materials, &materials[0], &count);
        
        // Create layers
        std::vector<SULayerRef> add_layers(num_materials);
        
        SUTextureRef texture = SU_INVALID;
        SUMaterialRef layer_material = SU_INVALID;
        
        for (size_t i = 0; i < num_materials; ++i) {
            SUSetInvalid(add_layers[i]);
            SULayerCreate(&add_layers[i]);
            
            std::string new_name = "Layer " + std::to_string(i);
            SULayerSetName(add_layers[i], new_name.c_str());
            
            SULayerGetMaterial(add_layers[i], &layer_material);
            SUMaterialGetTexture(materials[i], &texture);
            SUMaterialSetTexture(layer_material, texture);
        }
        
        SUModelAddLayers(model, num_materials, &add_layers[0]);
        
        // Save the in-memory model to a file
        SUModelSaveToFile(model, path.c_str());
        
        //Release model
        SUModelRelease(&model);
        add_layers.clear();
        materials.clear();
        SUTerminate();
        
        return Qtrue;
    }

The skp-file is created correctly, but SketchUp crashes.
If I remove the line:

SUMaterialSetTexture(layer_material, texture);

SU doesn’t crash but of course the result isn’t what I need.

If I remove the line:

SUModelRelease(&model);

SU doesn’t crash and I get the result I need. But not releasing the model is bad.


#2

is this running on the active model in an open instance of SU?

john


#3

No, this runs on a saved model (a component saved from inside the active model)


#4

are you cd’ing into the skp’s folder before running the cmd for your exe?
we had trouble when not doing that in the past…


#5

Hi Guy,
Materials own their texture objects so you should not make two different materials own the same texture object. We will clarify this in the docs.


#6

Thanks Bugra, this sound like the problem.