Page 1 of 1

Destructor issues when programatically adding ControlSetController

Posted: Wed Jul 28, 2021 3:50 am
by joris997
Hi all,
I'm currently adding a ControlSetController to an existing model (loaded from an .osim file) and forward simulating. The simulation (with the controller) runs fine but issues arise when exciting the program and the destructor is called.

The addition of the controller is performed by;

Code: Select all

// Add 
createControlFileXml(controlsPath,i);
OpenSim::ControlSet controlSet(absPath + "/_controls.xml");

OpenSim::ControlSetController controller;
controller.setControlSet((OpenSim::ControlSet*)OpenSim::Object::SafeCopy(&controlSet));
model.addController(&controller);

// Set the model to the proper state again after all the changes
model.finalizeFromProperties();
I've removed some part of the code for clarity, but here is the code that repoproduces the error. It should be clear from this file what is going wrong I hope;

Code: Select all

int main(){
    std::string modelPath = absPath + "/subject5_final.osim";
    std::string statesPath = absPath + "/scaled_states.sto";
    std::string controlsPath = absPath + "/scaled_controls.sto";

    for(int i=0; i<1; i++){
        // Load and initialize model
        OpenSim::Model model(modelPath);
        model.initSystem();

        // load in state data and create 'StatesTrajectory' object which can be
        // indexed to extract the correct state.
        OpenSim::Storage storage(statesPath);
        OpenSim::StatesTrajectory stateTrajectory =
                OpenSim::StatesTrajectory::createFromStatesStorage(model,storage,false,false,true);

        // The state of this iteration
        SimTK::State state = stateTrajectory[i];
        state.setTime(0.0);
        // Set the model to the correct state
        model.realizeAcceleration(state);

        // Add controls
        createControlFileXml(controlsPath,i);
        OpenSim::ControlSet controlSet(absPath + "/_controls.xml");

        OpenSim::ControlSetController controller;
        controller.setControlSet((OpenSim::ControlSet*)OpenSim::Object::SafeCopy(&controlSet));
        model.addController(&controller);

        // Set the model to the proper state again after all the changes
        model.finalizeFromProperties();

        // Create a forward tool and connect the controls file, the initial
        // state file, and the external force file
        OpenSim::ForwardTool fwdTool;
        fwdTool.setModel(model);
        fwdTool.setResultsDir(absPath + "/forward_out/");
        fwdTool.setFinalTime(finalTime);
        fwdTool.setStatesFileName(absPath + "state.sto");

        fwdTool.print("fwd_setup.xml");

        model.setUseVisualizer(false);
        // Run forward simulation
        fwdTool.run();
    }
    return 0;
}
Subsequently, at destruction, I get a seg-fault
Image

I've tried
- `controller.setControlSet(&controlSet)'
- model.removeController() at the end of the for-loop

but this all did not work. The only way to resolve the issue is by not doing "model.addController(&controller)" which is obviously not an option. I'm wondering what the issue could be or what I am missing in my code. Please let me know if you can help me out.

Thanks!

Re: Destructor issues when programatically adding ControlSetController

Posted: Wed Jul 28, 2021 5:24 am
by tkuchida
You might try

Code: Select all

osimModel.disownAllComponents();
as has been done in testControllers.cpp (https://github.com/opensim-org/opensim- ... ollers.cpp).

Re: Destructor issues when programatically adding ControlSetController

Posted: Wed Jul 28, 2021 5:40 am
by joris997
Thank you very much, Thomas Uchida. Works like a charm!