SU crashes on C extension (SDK C API)



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

The C++ code:

VALUE create_layer_materials(VALUE self, VALUE filename){
        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) {
        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) {
            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
        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:


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


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



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


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…


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.


Thanks Bugra, this sound like the problem.