API crashes when editing joint splines

Provide easy-to-use, extensible software for modeling, simulating, controlling, and analyzing the neuromusculoskeletal system.
POST REPLY
User avatar
Bryce Killen
Posts: 104
Joined: Mon Nov 24, 2014 7:12 pm

API crashes when editing joint splines

Post by Bryce Killen » Tue Jun 26, 2018 6:53 pm

Hello all,

I have a problem where when I am adding custom joint splines (to a Custom Joint) through both the MATLAB and Python API in both OpenSim version 3.2 and 3.3, the MATLAB or Python instance crashes. Even though it does crash, the splines are successfully added to the model and the model is saved correctly.

I am editing both the tibiofemoral and patellofemoral joints and the associated splines. I have included a brief summary of the API functions I use and also attached the Python script I am using.

-----------------------------

# load the model
currModel = osim.Model(dirToModel)

# safeDownCast to the joint
lKneeJoint = osim.CustomJoint.safeDownCast(currModel.getJointSet().get('knee_l'))

# get the Transform Axis of the joints
lKneeRotAxis1 = lKneeJoint.getSpatialTransform().getTransformAxis(2) # adduction

# create the splines
TFaddSpline = osim.SimmSpline()

# define the coordinate Array
lTFCoordArray = osim.ArrayStr()
lTFCoordArray.setSize(1)
lTFCoordArray.set(0,'knee_angle_l')

# loop through the solution and add the spline values
for vals in spline:
TFaddSpline.addPoint(float(TFflex[x]) , float(TFadd[x]) )

# add the spline to the function
lKneeRotAxis1.setFunction(TFaddSpline)
lKneeRotAxis1.setCoordinateNames(lTFCoordArray)

# print/save the model
currModel.printToXML(newModelDir)

---------------------------

While it isn't a problem that is crashes when adding a single spline solution, I am trying to loop through multiple solutions, in this scenario, the first model is saved but when I get to a seemingly random point in the code (calling the TransformAxis) it will crash. I initially thought it could be a problem with the looping however is still crashes even when I only add a single solution without looping.

Any experience or advice would be greatly appreciated

Thanks in advance

Bryce Killen

Tags:

User avatar
Dimitar Stanev
Posts: 1096
Joined: Fri Jan 31, 2014 5:14 am

Re: API crashes when editing joint splines

Post by Dimitar Stanev » Wed Jun 27, 2018 4:18 am

Hi,

It is probably a segmentation fault. My guess is that

Code: Select all

lKneeRotAxis1.setFunction(TFaddSpline)
is taking ownership of the TFaddSpline object. Then at the end of the program or function TFaddSpline is deleted when the object is out of scope. However, the model is responsible for deleting this object. I am not sure if there is a workaround and if you find any please let us know. I hope this helps!

User avatar
Bryce Killen
Posts: 104
Joined: Mon Nov 24, 2014 7:12 pm

Re: API crashes when editing joint splines

Post by Bryce Killen » Wed Jun 27, 2018 4:34 am

Hi Dimitar,

Thanks for your reply. If I am interpreting what you are saying correctly, the way to stop this crash would be to delete the object in correct way ? Assuming we can figure out what the correct way is.

Is my interpretation correct ?

Thanks again

User avatar
Dimitar Stanev
Posts: 1096
Joined: Fri Jan 31, 2014 5:14 am

Re: API crashes when editing joint splines

Post by Dimitar Stanev » Wed Jun 27, 2018 8:07 am

You can try to pinpoint the problem first, it may be caused by something else. I would try to comment this line and see if the problem persist:

Code: Select all

lKneeRotAxis1.setFunction(TFaddSpline)
If you were using the C++ API you would not care to delete this object because it is allocated on the heap and OpenSim will delete it for you since it takes ownership of the pointer. However, I don't know how you can tell Python or Matlab not to delete this object after the control goes out of scope (I suppose this is part of a function), except if this object is declared as global variable in the code. Usually, when you pass a pointer to a container there is an option where you can specify whether you would like the container to take ownership of the pointer or not.

https://simtk.org/api_docs/opensim/api_ ... c56ed6dafd
https://simtk.org/api_docs/opensim/api_ ... 8c430a4f1c

User avatar
Bryce Killen
Posts: 104
Joined: Mon Nov 24, 2014 7:12 pm

Re: API crashes when editing joint splines

Post by Bryce Killen » Wed Jun 27, 2018 9:45 pm

Hi Dimitar,

I've done a little bit of testing and declaring them as global vars made no difference. However as you suggested I tried to comment the .setFunction lines. Commenting these lines did stop it from crash, although obviously this is not ideal because the model is no longer updated.

I am now looking through the links you sent to see if there are any modifications to this function /lines of code to stop it crashing.

Thanks again
Bryce

User avatar
Bryce Killen
Posts: 104
Joined: Mon Nov 24, 2014 7:12 pm

Re: API crashes when editing joint splines

Post by Bryce Killen » Wed Jun 27, 2018 10:16 pm

Solved !

When inspecting the available functions associated with the class Transform Axis on the Doxygen page as you link, there was two versions of setFunction, the main difference being the ownership which I think is what you were eluding to in your reply. When inspecting the available functions in python, there was two options, setFunction (the one I previously used) and set_function. When switch from

Code: Select all

 lKneeRotAxis1.setFunction(TFaddSpline)

to

Code: Select all

 lKneeRotAxis1.set_function(TFaddSpline)


The crashing no longer occurred.

Thanks for the help and suggestions.

Bryce

User avatar
Thomas Uchida
Posts: 1804
Joined: Wed May 16, 2012 11:40 am

Re: API crashes when editing joint splines

Post by Thomas Uchida » Fri Jun 29, 2018 1:19 am

I am trying to loop through multiple solutions, in this scenario, the first model is saved but when I get to a seemingly random point in the code (calling the TransformAxis) it will crash.
The issue may be occurring because the first TransformAxis will take ownership of the function when setFunction() is called:

Code: Select all

// from TransformAxis.cpp, lines 170-175 (OpenSim 3.3)
void TransformAxis::setFunction(OpenSim::Function* func)
{
    Property<Function>& prop = updProperty_function();
    prop.clear();
    prop.adoptAndAppendValue(func);
}
You might try replacing lKneeRotAxis1.setFunction(TFaddSpline) with lKneeRotAxis1.setFunction(TFaddSpline.clone()) in your script.

POST REPLY