Page 1 of 2

Segfault on initSystem

Posted: Thu Jan 14, 2016 1:53 am
by mjhmilla
Hello,

I would like to use an OpenSim model with an optimal control code. The OpenSim header file and the header file of the optimal control code do not play nicely together. In an effort to isolate the two codes from each other I've made a separate class for the OpenSim model (OpenSimPendulum) and used forward referencing so that I can keep the call to include OpenSim.h in OpenSimPendulum.cc. The class only needs to do 2 things: make the model, and calculate its state derivative. The problem is that when I call initSystem() I get a segfault at line 948 of State.cpp in the function CacheEntryInfo::registerWithPrerequisites(StateImpl& stateImpl).

As a note, I've used this approach to successfully isolate a Simbody pendulum from this optimal control code. Finally, I'm working in Ubuntu 14.04 in case that makes any difference.

With kind regards,

Matt

Re: Segfault on initSystem

Posted: Thu Jan 14, 2016 11:03 am
by sherm
Matt, can you clarify which versions of Simbody and OpenSim you are using? From the error message I presume that is Simbody 3.6 (the master branch) -- is that right? What source did you use for OpenSim?

Thanks, Sherm

Re: Segfault on initSystem

Posted: Fri Jan 15, 2016 3:44 am
by mjhmilla
I pulled from the master branches of both OpenSim and Simbody on git hub, so I should have:
OpenSim 4.0
Simbody 3.6
As a double check I looked at the version numbers mentioned in doxygen, and everything is consistent.

Cheers, Matt

Re: Segfault on initSystem

Posted: Mon Jan 18, 2016 10:18 pm
by sherm
Hi, Matt. I built your program on Windows using OpenSim 4.0 and Simbody 3.6 Debug builds and it blew up on line 128 of OpenSimPendulum.cc:

Code: Select all

*modelState = model->initSystem();
At that point "modelState" is an uninitialized pointer so this doesn't make sense. I replaced it with

Code: Select all

modelState = new SimTK::State(model->initSystem());
which ran to completion with this output:

Code: Select all

createOpenSimPendulum:
        :Adding gravity
        :Adding link 0
        :getDefaultState
        :Number of model states: NU: 1, NQ: 1, NZ: 2
createStateDerivative:
position        velocity        activation
0.000000e+00    0.000000e+00    1.000000e+00
        Copying state over
        realize to acceleration
        getUDot
        0: 0.005000
Although that seems to fix the problem, I don't think "modelState" should be a pointer at all -- you should just declare it

Code: Select all

SimTK::State modelState;
so that you can write

Code: Select all

modelState = model->initSystem()
Regards,
Sherm

Re: Segfault on initSystem

Posted: Wed Feb 17, 2016 6:06 am
by mjhmilla
Thank you for the detailed responses Sherm! Though now our two systems behave differently. When I update line 128 to this

Code: Select all

modelState = new SimTK::State(model->initSystem());
I get a segfault at this line when the program is run. In addition, when I change modelState from a pointer to an object

Code: Select all

SimTK::State modelState;
the compiler complains that
field 'modelState' has incomplete type
. I'll keep digging - that you were able to fix this in VS (I'm assuming) gives me hope that a fix is around the corner.

Cheers,
Matt

Re: Segfault on initSystem

Posted: Wed Feb 17, 2016 11:01 pm
by sherm
The "incomplete type" is because no definition of SimTK::State is available; for a pointer SimTK::State* only a declaration is needed, not a definition. If you include Simbody.h you'll have the definition; if that is a problem you could keep the pointer.

I don't understand the seg fault -- might be more info in a Debug build.

Sherm

Re: Segfault on initSystem

Posted: Thu Feb 18, 2016 12:57 pm
by mjhmilla
Indeed I do have to keep the pointers: putting Simbody.h in the header causes conflicts with the other libraries I'm using.

I've built OpenSim and Simbody in debug and rebuilt the project in debug. Oddly it looks like my program is linking against the non-debug versions of the OpenSim libraries. This line in the CMakeFile

Code: Select all

message("OpenSim_LIBRARIES = " ${OpenSim_LIBRARIES})
yields
OpenSim_LIBRARIES = oSimTools
but not oSimTools_d. Any idea why? Note I'm using ccmake incase this makes a difference.

I do get some new errors on

Code: Select all

modelState = new SimTK::State(model->initSystem());
. At the output console I see
:getDefaultState
EXCEPTION: createOpenSimPendulum: SimTK Exception thrown at StateImpl.h:1198:
Error detected by Simbody method State::allocateCacheEntryWithPrerequisites(): Prerequisite cache entry (2,3) has depends-on stage Time but this one would have lower depends-on stage Instance. That would mean the prerequisite could get invalidated without invalidating this one; not good.
(Required condition 'prereq.getDependsOnStage() <= earliest' was not met.)
which seems odd to me. The stack trace is even stranger
1 ?? 0x21
2 SimTK::CollisionDetectionAlgorithm::AlgorithmMap::~AlgorithmMap CollisionDetectionAlgorithm.cpp 41 0x7ffff5d2dc47
3 __cxa_finalize cxa_finalize.c 56 0x7ffff47925ea
4 __do_global_dtors_aux 0x7ffff336d183
5 ?? 0x7fffffffe310
6 _dl_fini dl-fini.c 252 0x7ffff7dea73a
(keep in mind that the regular OpenSim libraries are being linked against).

Does anything spring to mind?

Re: Segfault on initSystem

Posted: Thu Feb 18, 2016 1:56 pm
by sherm
Mixing debug with non-debug libraries is a fatal problem.

Hopefully Chris Dembia can quickly spot what's wrong.

Re: Segfault on initSystem

Posted: Sun Feb 21, 2016 10:13 pm
by chrisdembia
The fact that `OpenSim_LIBRARIES` is `osimTools` does not mean you are using debug libraries. In fact, `OpenSim_LIBRARIES` is set to `osimTools` no matter what. In this context, `osimTools` is the name of a CMake target (in any of its configurations), not of a specific library file. You have to look at the link command that is actually executed by your linker, or perhaps using Dependency Walker on Windows.

Re: Segfault on initSystem

Posted: Fri Mar 04, 2016 8:51 am
by mjhmilla
Thank you for the explanation Chris - that helps a lot. Using the command 'ldd' in linux I got a list of the dependencies - see below (I've built the target in Debug).

Do you see a similar list when you run dependency walker? Should I be concerned that it appears that both the release and debug versions of the libSimTK libraries appear to be dependencies? If thoughts on how to fix it?

In case this is relevant, initSystem is now working just fine, but the program later dies when I try to set the state. Details below.

Thank you again for your help!

Matt

----------------------------------------
Seemingly duplicated dependencies
----------------------------------------

libSimTKsimbody.so.3.6 libSimTKsimbody_d.so.3.6
libSimTKcommon.so.3.6 libSimTKsimbody_d.so.3.6
libSimTKmath.so.3.6 libSimTKmath_d.so.3.6
libSimTKsimbody.so.3.6 libSimTKsimbody_d.so.3.6

----------------------------------------
Full list of dependencies
----------------------------------------

linux-vdso.so.1 => (0x00007ffc2b5f2000)
libosimTools_d.so => /home/mjhmilla/dev/opensim-core-install/lib/x86_64-linux-gnu/libosimTools_d.so (0x00007f5c24469000)
libosimAnalyses_d.so => /home/mjhmilla/dev/opensim-core-install/lib/x86_64-linux-gnu/libosimAnalyses_d.so (0x00007f5c24159000)
libosimActuators_d.so => /home/mjhmilla/dev/opensim-core-install/lib/x86_64-linux-gnu/libosimActuators_d.so (0x00007f5c23d58000)
libosimSimulation_d.so => /home/mjhmilla/dev/opensim-core-install/lib/x86_64-linux-gnu/libosimSimulation_d.so (0x00007f5c23369000)
libosimCommon_d.so => /home/mjhmilla/dev/opensim-core-install/lib/x86_64-linux-gnu/libosimCommon_d.so (0x00007f5c22f16000)
libSimTKcommon.so.3.6 => /home/mjhmilla/dev/opensim-core-install/lib/x86_64-linux-gnu/libSimTKcommon.so.3.6 (0x00007f5c229bd000)
libSimTKmath.so.3.6 => /home/mjhmilla/dev/opensim-core-install/lib/x86_64-linux-gnu/libSimTKmath.so.3.6 (0x00007f5c223b4000)
libSimTKsimbody.so.3.6 => /home/mjhmilla/dev/opensim-core-install/lib/x86_64-linux-gnu/libSimTKsimbody.so.3.6 (0x00007f5c21d29000)

libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f5c219fd000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f5c216f7000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f5c214e0000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f5c2111b000)
libSimTKsimbody_d.so.3.6 => /home/mjhmilla/dev/opensim-core-install/lib/x86_64-linux-gnu/libSimTKsimbody_d.so.3.6 (0x00007f5c204eb000)
libSimTKmath_d.so.3.6 => /home/mjhmilla/dev/opensim-core-install/lib/x86_64-linux-gnu/libSimTKmath_d.so.3.6 (0x00007f5c1fb36000)
libSimTKcommon_d.so.3.6 => /home/mjhmilla/dev/opensim-core-install/lib/x86_64-linux-gnu/libSimTKcommon_d.so.3.6 (0x00007f5c1f392000)

liblapack.so.3 => /usr/lib/liblapack.so.3 (0x00007f5c1ebf6000)
libosimLepton_d.so => /home/mjhmilla/dev/opensim-core-install/lib/x86_64-linux-gnu/libosimLepton_d.so (0x00007f5c1e977000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f5c1e773000)
libf77blas.so.3 => /usr/lib/libf77blas.so.3 (0x00007f5c1e552000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f5c1e334000)
librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007f5c1e12c000)
/lib64/ld-linux-x86-64.so.2 (0x00005599c87a2000)
libblas.so.3 => /usr/lib/libblas.so.3 (0x00007f5c1c922000)
libgfortran.so.3 => /usr/lib/x86_64-linux-gnu/libgfortran.so.3 (0x00007f5c1c608000)
libcblas.so.3 => /usr/lib/libcblas.so.3 (0x00007f5c1c3e6000)
libatlas.so.3 => /usr/lib/libatlas.so.3 (0x00007f5c1be53000)
libquadmath.so.0 => /usr/lib/x86_64-linux-gnu/libquadmath.so.0 (0x00007f5c1bc17000)

----------------------------------------
New errors on run
----------------------------------------
In case this helps, the program now makes it past initSystem, but later dies near line 210

Code: Select all

modelState->updU()[i] = generalizedVelocities.at(i);
with the complaint
EXCEPTION: createOpenSimPendulum: SimTK Exception thrown at StateImpl.h:1198:
Error detected by Simbody method State::allocateCacheEntryWithPrerequisites(): Prerequisite cache entry (2,3) has depends-on stage Time but this one would have lower depends-on stage Instance. That would mean the prerequisite could get invalidated without invalidating this one; not good.
(Required condition 'prereq.getDependsOnStage() <= earliest' was not met.)