Robot example crashes

Post here for help on using FreeCAD's graphical user interface (GUI).
Forum rules
and Helpful information
IMPORTANT: Please click here and read this first, before asking for help

Also, be nice to others! Read the FreeCAD code of conduct!
User avatar
federico.carminati
Posts: 66
Joined: Thu May 18, 2017 1:01 pm
Location: Geneva, Switzerland

Robot example crashes

Post by federico.carminati »

Hello,
I apologize in advance if this is a known problem which I have missed, and also if this is the wrong forum.

I have a fresh installation of FreeCAD head and when I run it on the Robot example

Code: Select all

/Applications/FreeCAD.app/Contents/MacOS/FreeCAD /Applications/FreeCAD.app//Contents/data/examples/RobotExample.FCStd
I have the following crash

Code: Select all

lldb /Applications/FreeCAD.app/Contents/MacOS/FreeCAD /Applications/FreeCAD.app//Contents/data/examples/RobotExample.FCStd
(lldb) target create "/Applications/FreeCAD.app/Contents/MacOS/FreeCAD"
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/copy.py", line 52, in <module>
    import weakref
  File "/usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/weakref.py", line 14, in <module>
    from _weakref import (
ImportError: cannot import name _remove_dead_weakref
Current executable set to '/Applications/FreeCAD.app/Contents/MacOS/FreeCAD' (x86_64).
(lldb) settings set -- target.run-args  "/Applications/FreeCAD.app//Contents/data/examples/RobotExample.FCStd"
(lldb) r
Process 41405 launched: '/Applications/FreeCAD.app/Contents/MacOS/FreeCAD' (x86_64)
FreeCAD 0.18, Libs: 0.18R14323 (Git)
© Juergen Riegel, Werner Mayer, Yorik van Havre 2001-2018
  #####                 ####  ###   ####
  #                    #      # #   #   #
  #     ##  #### ####  #     #   #  #   #
  ####  # # #  # #  #  #     #####  #   #
  #     #   #### ####  #    #     # #   #
  #     #   #    #     #    #     # #   #  ##  ##  ##
  #     #   #### ####   ### #     # ####   ##  ##  ##

Process 41405 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=2, address=0x7ffface64058)
    frame #0: 0x00007ffface64058 libc++abi.dylib`vtable for __cxxabiv1::__si_class_type_info + 16
libc++abi.dylib`vtable for __cxxabiv1::__si_class_type_info:
->  0x7ffface64058 <+16>: popq   %rsi
Target 0: (FreeCAD) stopped.
(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=2, address=0x7ffface64058)
  * frame #0: 0x00007ffface64058 libc++abi.dylib`vtable for __cxxabiv1::__si_class_type_info + 16
    frame #1: 0x0000000123731910 Robot.so`Robot::Trajectory::generateTrajectory(this=0x00007ffeefbfb500) at Trajectory.cpp:226 [opt]
    frame #2: 0x000000012372ce6a Robot.so`Robot::PropertyTrajectory::Restore(this=0x0000000123212518, reader=<unavailable>) at PropertyTrajectory.cpp:140 [opt]
    frame #3: 0x0000000100f17be5 libFreeCADApp.dylib`App::PropertyContainer::Restore(this=<unavailable>, reader=<unavailable>) at PropertyContainer.cpp:278 [opt]
    frame #4: 0x0000000100e7064b libFreeCADApp.dylib`App::Document::readObjects(this=0x00000001072a8600, reader=<unavailable>) at Document.cpp:1533 [opt]
    frame #5: 0x0000000100e6f7bf libFreeCADApp.dylib`App::Document::Restore(this=0x00000001072a8600, reader=<unavailable>) at Document.cpp:1402 [opt]
    frame #6: 0x0000000100e734b4 libFreeCADApp.dylib`App::Document::restore(this=0x00000001072a8600) at Document.cpp:1786 [opt]
    frame #7: 0x0000000100f62ac3 libFreeCADApp.dylib`App::Application::openDocument(this=<unavailable>, FileName="/Applications/FreeCAD.app//Contents/data/examples/RobotExample.FCStd") at Application.cpp:526 [opt]
    frame #8: 0x0000000100f776d4 libFreeCADApp.dylib`App::Application::processFiles(files=size=1) at Application.cpp:1592 [opt]
    frame #9: 0x00000001003a3998 libFreeCADGui.dylib`Gui::MainWindow::delayedStartup(this=<unavailable>) at MainWindow.cpp:1049 [opt]
    frame #10: 0x000000010328e9f1 QtCore`QObject::event(QEvent*) + 769
    frame #11: 0x00000001025f55bf QtWidgets`QWidget::event(QEvent*) + 4799
    frame #12: 0x0000000102709a35 QtWidgets`QMainWindow::event(QEvent*) + 277
    frame #13: 0x000000010039fb8b libFreeCADGui.dylib`Gui::MainWindow::event(this=0x00007ffeefbfe5a0, e=0x0000000120327270) at MainWindow.cpp:617 [opt]
    frame #14: 0x00000001025ba46d QtWidgets`QApplicationPrivate::notify_helper(QObject*, QEvent*) + 269
    frame #15: 0x00000001025bb897 QtWidgets`QApplication::notify(QObject*, QEvent*) + 583
    frame #16: 0x000000010010b29b libFreeCADGui.dylib`Gui::GUIApplication::notify(this=0x00007ffeefbfe0d0, receiver=0x00007ffeefbfe5a0, event=0x0000000120327270) at GuiApplication.cpp:91 [opt]
    frame #17: 0x0000000103264f64 QtCore`QCoreApplication::notifyInternal2(QObject*, QEvent*) + 164
    frame #18: 0x00000001032660d7 QtCore`QCoreApplicationPrivate::sendPostedEvents(QObject*, int, QThreadData*) + 791
    frame #19: 0x000000010d02beb9 libqcocoa.dylib`___lldb_unnamed_symbol554$$libqcocoa.dylib + 313
    frame #20: 0x000000010d02c770 libqcocoa.dylib`___lldb_unnamed_symbol566$$libqcocoa.dylib + 32
    frame #21: 0x00007fff4cc38a11 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    frame #22: 0x00007fff4ccf242c CoreFoundation`__CFRunLoopDoSource0 + 108
    frame #23: 0x00007fff4cc1b470 CoreFoundation`__CFRunLoopDoSources0 + 208
    frame #24: 0x00007fff4cc1a8ed CoreFoundation`__CFRunLoopRun + 1293
    frame #25: 0x00007fff4cc1a153 CoreFoundation`CFRunLoopRunSpecific + 483
    frame #26: 0x00007fff4bf04d96 HIToolbox`RunCurrentEventLoopInMode + 286
    frame #27: 0x00007fff4bf04a0f HIToolbox`ReceiveNextEventCommon + 366
    frame #28: 0x00007fff4bf04884 HIToolbox`_BlockUntilNextEventMatchingListInModeWithFilter + 64
    frame #29: 0x00007fff4a1b5a73 AppKit`_DPSNextEvent + 2085
    frame #30: 0x00007fff4a94be34 AppKit`-[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 3044
    frame #31: 0x00007fff4a1aa885 AppKit`-[NSApplication run] + 764
    frame #32: 0x000000010d02b5bf libqcocoa.dylib`___lldb_unnamed_symbol548$$libqcocoa.dylib + 3007
    frame #33: 0x00000001032603de QtCore`QEventLoop::exec(QFlags<QEventLoop::ProcessEventsFlag>) + 430
    frame #34: 0x0000000103265552 QtCore`QCoreApplication::exec() + 130
    frame #35: 0x00000001000c6f79 libFreeCADGui.dylib`Gui::Application::runApplication() at Application.cpp:1946 [opt]
    frame #36: 0x000000010000ae63 FreeCAD`main(argc=<unavailable>, argv=<unavailable>) at MainGui.cpp:291 [opt]
    frame #37: 0x00007fff74a5b015 libdyld.dylib`start + 1
I am running on a Mac OS X High Sierra (10.13.6) 2.5GHz Intel Core i7.

Code: Select all

Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 9.1.0 (clang-902.0.39.2)
Target: x86_64-apple-darwin17.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
Found CUDA installation: /usr/local/cuda, version 8.0
Should I open a bug? Thanks for your help
Federico Carminati
Co-Founder and Scientific Director of Transmutex SA
Switzerland/Suisse
wmayer
Founder
Posts: 20308
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Robot example crashes

Post by wmayer »

Hi,

I think this or a similar issue has been reported in the past (there might also be a ticket in the bug tracker). I once looked into it but couldn't reproduce it. Now the call stack you posted is very detailed as it directly points to the line of the crash which is Trajectory.cpp:226:

Code: Select all

pcTrajectory->Add(pcTrak.release());
Now, I don't how which of these variables are invalid. pcTrajectory is created at the top of the function and for pcTrak it's checked that it's not null.
Are you able to display the value of these variables: pcTrajectory, pcTrak and this?
User avatar
federico.carminati
Posts: 66
Joined: Thu May 18, 2017 1:01 pm
Location: Geneva, Switzerland

Re: Robot example crashes

Post by federico.carminati »

Hmmm.... looks funny...

Code: Select all

(lldb) print pcTrajectory
(KDL::Trajectory_Composite *) $1 = 0x000000012332a3a0

(lldb) print pcTrak
(std::__1::unique_ptr<KDL::Trajectory_Segment, std::__1::default_delete<KDL::Trajectory_Segment> >) $2 = {
  __ptr_ = {
    std::__1::__compressed_pair_elem<KDL::Trajectory_Segment *, 0, false> = {
      __value_ = 0x0000000127506a60
    }
  }
}
so pcTrajectory seems OK, however if I inspect the class

Code: Select all

(lldb) print *pcTrajectory
(KDL::Trajectory_Composite) $0 = {
  vt = size=0 {}
  vd = size=0 {}
  duration = 0
  path = 0x0000000000000000
}
path has not been initialized, which is funny, since Trajectory_Composite has *one* creator that definitely initializes path (see kdl/trajectory_composite.cpp:26). What I really do not like is the Destroy method (trajectory_composite.cpp:99), if you delete a variable (apart in a destructor), it would be better to set it to 0. In a previous test, path had the value of 5! But this is just a wild guess. Let me know if I can help more, now I have a version compiled with debug option.
Federico Carminati
Co-Founder and Scientific Director of Transmutex SA
Switzerland/Suisse
User avatar
federico.carminati
Posts: 66
Joined: Thu May 18, 2017 1:01 pm
Location: Geneva, Switzerland

Re: Robot example crashes

Post by federico.carminati »

I think these two corrections are reasonable

Code: Select all

diff --git a/src/Mod/Robot/App/Trajectory.cpp b/src/Mod/Robot/App/Trajectory.cpp
index 24d9b47..d9a18b8 100644
--- a/src/Mod/Robot/App/Trajectory.cpp
+++ b/src/Mod/Robot/App/Trajectory.cpp
@@ -222,7 +222,7 @@ void Trajectory::generateTrajectory(void)
                 }

                 // add the segment if no continuous block is running
-                if (!pcRoundComp && pcTrak)
+                if (!pcRoundComp && pcTrak.get())^M
                     pcTrajectory->Add(pcTrak.release());
             }
         }
diff --git a/src/Mod/Robot/App/kdl_cp/trajectory_composite.cpp b/src/Mod/Robot/App/kdl_cp/trajectory_composite.cpp
index 73bec46..f7b6dca 100644
--- a/src/Mod/Robot/App/kdl_cp/trajectory_composite.cpp
+++ b/src/Mod/Robot/App/kdl_cp/trajectory_composite.cpp
@@ -105,6 +105,7 @@ namespace KDL {
         vd.erase(vd.begin(),vd.end());

         delete path; // FreeCAD change
+       path = 0; // fca change
     }

     Trajectory_Composite::~Trajectory_Composite() {
 
but the code still crashes in the same way.
Federico Carminati
Co-Founder and Scientific Director of Transmutex SA
Switzerland/Suisse
wmayer
Founder
Posts: 20308
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Robot example crashes

Post by wmayer »

Apparently these are some FreeCAD specific changes added by JRiegel git commit 9304f7a78a. Not quite clear why they are needed. Also, this causes problems when trying to use the original KDL library as it may raise build failures.
wmayer
Founder
Posts: 20308
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Robot example crashes

Post by wmayer »

I think these two corrections are reasonable
Nullifying the pointer is a must but for the if check it doesn't make a difference if you write if (!pcRoundComp && pcTrak) or if (!pcRoundComp && pcTrak.get()) because unique_ptr implements operator bool() const which will be automatically used.

But what's missing is to check the pointer of path when adding a trajectory as it's possible to have called Destroy() beforehand.

But weird is that everything works correctly here. Maybe you should check if you use the internal or an external KDL.
User avatar
federico.carminati
Posts: 66
Joined: Thu May 18, 2017 1:01 pm
Location: Geneva, Switzerland

Re: Robot example crashes

Post by federico.carminati »

OK. these are the changes I propose up to now, however this still does not fix the problem

Code: Select all

diff --git a/src/Mod/Robot/App/kdl_cp/trajectory_composite.cpp b/src/Mod/Robot/App/kdl_cp/trajectory_composite.cpp
index 73bec46..e128781 100644
--- a/src/Mod/Robot/App/kdl_cp/trajectory_composite.cpp
+++ b/src/Mod/Robot/App/kdl_cp/trajectory_composite.cpp
@@ -93,6 +93,7 @@ namespace KDL {
         vt.insert(vt.end(),elem);
         duration += elem->Duration();
         vd.insert(vd.end(),duration);
+	if(!path) path = new Path_Composite();
         path->Add(elem->GetPath(),false); // FreeCAD change
     }

@@ -105,6 +106,7 @@ namespace KDL {
         vd.erase(vd.begin(),vd.end());

         delete path; // FreeCAD change
+	path = 0; // fca change
     }

     Trajectory_Composite::~Trajectory_Composite() {
diff --git a/src/Mod/Robot/App/kdl_cp/trajectory_stationary.hpp b/src/Mod/Robot/App/kdl_cp/trajectory_stationary.hpp
index 824dbef..c45f21b 100644
--- a/src/Mod/Robot/App/kdl_cp/trajectory_stationary.hpp
+++ b/src/Mod/Robot/App/kdl_cp/trajectory_stationary.hpp
@@ -32,8 +32,11 @@ namespace KDL {
 		VelocityProfile* prof; // FreeCAD change
 		Path*      path; // FreeCAD change
 	public:
+      	        Trajectory_Stationary()
+		  duration(0),pos(0),prof(nullptr),path(nullptr) {}
+
 		Trajectory_Stationary(double _duration,const Frame& _pos):
-		  duration(_duration),pos(_pos) {}
+		  duration(_duration),pos(_pos),prof(nullptr),path(nullptr) {}

         // FreeCAD change
 	    virtual Path* GetPath() {
Federico Carminati
Co-Founder and Scientific Director of Transmutex SA
Switzerland/Suisse
wmayer
Founder
Posts: 20308
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Robot example crashes

Post by wmayer »

I think more and more the real problem is that there is mix-up of the KDL in the source tree and an installed version of the library.
FYI, there is a switch (FREECAD_USE_EXTERNAL_KDL) to use the installed KDL. Then I think it's best to check for the dependencies of the Robot.so and RobotGui.so modules using ldd (or whatever the equivalent is on macOS).
User avatar
federico.carminati
Posts: 66
Joined: Thu May 18, 2017 1:01 pm
Location: Geneva, Switzerland

Re: Robot example crashes

Post by federico.carminati »

I recompiled with -sanitize=memory, but I had a short and brutish crash as usual with no interesting message before. It seems that it is not a memory corruption but rather a honest programming error... I will try your suggestion.
Federico Carminati
Co-Founder and Scientific Director of Transmutex SA
Switzerland/Suisse
User avatar
federico.carminati
Posts: 66
Joined: Thu May 18, 2017 1:01 pm
Location: Geneva, Switzerland

Re: Robot example crashes

Post by federico.carminati »

In fact I had FREECAD_USE_EXTERNAL_KDL=ON. If I set it to OFF everything works, but with the following corrections, which I do not know whether are essential or not

Code: Select all

diff --git a/src/Mod/Robot/App/kdl_cp/trajectory_stationary.hpp b/src/Mod/Robot/App/kdl_cp/trajectory_stationary.hpp
index 824dbef..f366a60 100644
--- a/src/Mod/Robot/App/kdl_cp/trajectory_stationary.hpp
+++ b/src/Mod/Robot/App/kdl_cp/trajectory_stationary.hpp
@@ -32,8 +32,11 @@ namespace KDL {
                VelocityProfile* prof; // FreeCAD change
                Path*      path; // FreeCAD change
        public:
+               Trajectory_Stationary() :
+                 duration(0),prof(nullptr),path(nullptr) {}
+
                Trajectory_Stationary(double _duration,const Frame& _pos):
-                 duration(_duration),pos(_pos) {}
+                 duration(_duration),pos(_pos),prof(nullptr),path(nullptr) {}

         // FreeCAD change
            virtual Path* GetPath() {
Federico Carminati
Co-Founder and Scientific Director of Transmutex SA
Switzerland/Suisse
Post Reply