opensim-org / opensim-core

SimTK OpenSim C++ libraries and command-line applications, and Java/Python wrapping.
https://opensim.stanford.edu
Apache License 2.0
796 stars 320 forks source link

memory issue #983

Closed aaa34169 closed 1 year ago

aaa34169 commented 8 years ago

Hi I use the opensim python API and i have an issue when i try to run my program twice successively. I work with spyder but i am also try though a python shell.

briefly, i call a model with model = opensim.Model(str(osimFile))

if i run once my program that work fine. if i want my code another time. My program blocks and i have this issue : 2016-05-25 12_46_34-api_ opensim__object class reference.

i think there is a problem with the model pointer. that doesn t work as well if i add del model, at the end of my program

how can i delete this instance

regards

Fabien

chrisdembia commented 8 years ago

Hey Fabien; thank you for reporting. Could you please update the name of this issue to be more descriptive?

Are you using OpenSim 3.3 or the latest development version? If you are using OpenSim 3.3, did you use the pre-built python package or did you build it yourself? Are you using python 2 or python 3?

I'm also not sure what you are doing, exactly. Can you show complete code? Are you in an interactive python session, and then running the same exact line twice? What about putting del model before the second call?

aymanhab commented 8 years ago

We have test cases that create models and then they get garbage collected at the end (without needing to explicitly call del). The likely culprit is the code between model creation and the end of the program. Please post along with your current code base/environment as @chrisdembia suggested and we'll try to help you out. In general, issues related to live code on github belong here, while issues related to version 3.3 belong to the user forum so that others can contribute and benefit. Thank you.

aaa34169 commented 8 years ago

Hi chris

I am using the release 3.3 with pre built python package . i work under python 2.7.

try to run this code twice

osimFile =  ".\\cgm2version3test.osim"

m_model = opensim.Model(str(osimFile))

m_model.initSystem()
m_myState = m_model.initSystem()

m = opensim.Marker()
m.setOffset(opensim.Vec3(100/1000.0,100/1000.0,100/1000.0))
m.setName("marker")
m.setBodyName("Pelvis")
m.setFixed(False)

m_model.getMarkerSet().set(0,m)

it work fine firsly. then fail at second run.

cheers.

2016-05-25 20:51 GMT+01:00 Christopher Dembia notifications@github.com:

Hey Fabien; thank you for reporting. Could you please update the name of this issue to be more descriptive?

Are you using OpenSim 3.3 or the latest development version? If you are using OpenSim 3.3, did you use the pre-built python package or did you build it yourself? Are you using python 2 or python 3?

I'm also not sure what you are doing, exactly. Can you show complete code? Are you in an interactive python session, and then running the same exact line twice? What about putting del model before the second call?

— You are receiving this because you authored the thread. Reply to this email directly or view it on GitHub https://github.com/opensim-org/opensim-core/issues/983#issuecomment-221686916

Fabien

Fabien Leboeuf

Research fellow ( University of salford)

- phD Mechanics-Biomechanics (university of Poitiers, FRANCE)

College of Health & Social Care University of Salford Allerton Building Frederick Road Campus

Salford, M6 6PU, UNITED KINGDOM

chrisdembia commented 8 years ago

Thanks for sharing. How are you running the code twice? Is that code in a function and you call the function twice? Is this from a python interpreter? Does the crash occur when the whole program exits? When exactly does the crash occur?

I think the crash occurs from m_model.getMarkerSet().set(0, m). If you want to edit the marker at index 0, then just grab it and edit it in place:

marker = m_model.updMarkerSet().get(0)
marker.setOffset(...)
marker.setName(...)
...
# Then, no need to call getMarkerSet()

There is no need to call initSystem() twice; remove your first call.

There is no need to wrap the argument to Model with str().

You should also use the "upd" variants of the methods if you plan to modify the object.

@aymanhab was right, this question would have been more appropriate for the forum.

aaa34169 commented 8 years ago

Thank you Chris. i will test your snippet. and sorry ( ayman) to ask question in the wrong forum. It s pretty long to get a answer with the opensim forum ? do you know other users of the python opensim API, i could contact ?

2016-05-26 17:32 GMT+01:00 Christopher Dembia notifications@github.com:

Thanks for sharing. How are you running the code twice? Is that in a function and you call the function twice? Is this from a python interpreter? Does the crash occur when the whole program exits? When exactly does the crash occur?

I think the crash occurs from m_model.getMarkerSet().set(0, m). If you want to edit the marker at index 0, then just grab it and edit it in place:

marker = m_model.updMarkerSet().get(0) marker.setOffset(...) marker.setName(...)...# Then, no need to call getMarkerSet()

There is no need to call initSystem() twice; remove your first call.

There is no need to wrap the argument to Model with str().

You should also use the "upd" variants of the methods if you plan to modify the object.

@aymanhab https://github.com/aymanhab was right, this question would have been more appropriate for the forum.

— You are receiving this because you authored the thread. Reply to this email directly or view it on GitHub https://github.com/opensim-org/opensim-core/issues/983#issuecomment-221924199

Fabien

Fabien Leboeuf

Research fellow ( University of salford)

- phD Mechanics-Biomechanics (university of Poitiers, FRANCE)

College of Health & Social Care University of Salford Allerton Building Frederick Road Campus

Salford, M6 6PU, UNITED KINGDOM

aymanhab commented 8 years ago

@fablebGitHub There are many API users from python on the forum and they're writing large systems and quite involved so you'll definitely get help there. I agree with @chrisdembia that the likely culprit is the the line getMarkerSet().set(0,m) since it doesn't tell the python interpreter that the model takes ownership of the marker 'm' so it ends up double deleted at the end when the model and marker get out of scope causing the crash.

jenhicks commented 1 year ago

Closing given age; feel free to re-open if high priority.