robotology / osqp-eigen

Simple Eigen-C++ wrapper for OSQP library
https://robotology.github.io/osqp-eigen/
BSD 3-Clause "New" or "Revised" License
394 stars 118 forks source link

Segmentation fault (core dumped) #53

Closed NUDTUGVexplorer closed 4 years ago

NUDTUGVexplorer commented 4 years ago

When I run the example MPCExample.cpp it has an error: Segmentation fault (core dumped). Then I debug the program, it shows the error in the following lines.

solver.settings()->setWarmStart(true);

how to solve the issue? Thanks a lot.

S-Dafarra commented 4 years ago

Hi @NUDTUGVexplorer, can you give us some more information about your system? For example:

NUDTUGVexplorer commented 4 years ago

1.OS: ubuntu 14.04 LTS gcc version 4.8.4 2.osqp-eigen: I just git from your repo on March 1st, 2020. 3.osqp: OSQP v0.5.0 - Operator Splitting QP Solver

  1. I didn't change the cmake configurations. But when I compile osqp-eigen, there is an issue as follows: /osqp-eigen-master/build$ make Scanning dependencies of target OsqpEigen [ 25%] Building CXX object CMakeFiles/OsqpEigen.dir/src/Data.cpp.o [ 50%] Building CXX object CMakeFiles/OsqpEigen.dir/src/Settings.cpp.o [ 75%] Building CXX object CMakeFiles/OsqpEigen.dir/src/Solver.cpp.o /home/ugv/software/osqp-eigen-master/src/Solver.cpp: In constructor ‘OsqpEigen::Solver::Solver()’: /home/ugv/software/osqp-eigen-master/src/Solver.cpp:19:18: error: ‘make_unique’ is not a member of ‘std’ m_settings = std::make_unique(); ^ /home/ugv/software/osqp-eigen-master/src/Solver.cpp:19:54: error: expected primary-expression before ‘>’ token m_settings = std::make_unique(); ^ /home/ugv/software/osqp-eigen-master/src/Solver.cpp:19:56: error: expected primary-expression before ‘)’ token m_settings = std::make_unique(); ^ /home/ugv/software/osqp-eigen-master/src/Solver.cpp:20:14: error: ‘make_unique’ is not a member of ‘std’ m_data = std::make_unique(); ^ /home/ugv/software/osqp-eigen-master/src/Solver.cpp:20:46: error: expected primary-expression before ‘>’ token m_data = std::make_unique(); ^ /home/ugv/software/osqp-eigen-master/src/Solver.cpp:20:48: error: expected primary-expression before ‘)’ token m_data = std::make_unique(); ^ make[2]: [CMakeFiles/OsqpEigen.dir/src/Solver.cpp.o] Error 1 make[1]: [CMakeFiles/OsqpEigen.dir/all] Error 2 make: *** [all] Error 2

In order to fix the issue, I found some advise from the others. So I change the code in Solver.cpp, //m_settings = std::unique_ptr(); //m_data = std::unique_ptr();

m_settings = std::make_unique<OsqpEigen::Settings>();
m_data = std::make_unique<OsqpEigen::Data>();

Then it could be successfully compiled now. /osqp-eigen-master/build$ make Scanning dependencies of target OsqpEigen [ 25%] Building CXX object CMakeFiles/OsqpEigen.dir/src/Solver.cpp.o [ 50%] Linking CXX shared library lib/libOsqpEigen.so [100%] Built target OsqpEigen

Thanks!

NUDTUGVexplorer commented 4 years ago

make test, the results are as follows: Running tests... Test project /home/lxh/software/osqp-eigen-master/build Start 1: SparseMatrixTest 1/5 Test #1: SparseMatrixTest ................. Passed 0.01 sec Start 2: QPTest 2/5 Test #2: QPTest ...........................Exception: SegFault 0.12 sec Start 3: UpdateMatricesTest 3/5 Test #3: UpdateMatricesTest ...............Exception: SegFault 0.10 sec Start 4: MPCTest 4/5 Test #4: MPCTest ..........................Exception: SegFault 0.10 sec Start 5: MPCUpdateMatricesTest 5/5 Test #5: MPCUpdateMatricesTest ............Exception: SegFault 0.10 sec

20% tests passed, 4 tests failed out of 5

Total Test time (real) = 0.42 sec

The following tests FAILED: 2 - QPTest (SEGFAULT) 3 - UpdateMatricesTest (SEGFAULT) 4 - MPCTest (SEGFAULT) 5 - MPCUpdateMatricesTest (SEGFAULT) Errors while running CTest

S-Dafarra commented 4 years ago

Indeed, the reason is exactly this one:

/home/ugv/software/osqp-eigen-master/src/Solver.cpp:19:18: error: ‘make_unique’ is not a member of ‘std’

The make_unique has been implemented in gcc starting from version 4.9.0: https://isocpp.org/blog/2014/04/gcc-4.9.0. I am afraid your compiler is not supported.

By commenting out the lines https://github.com/robotology/osqp-eigen/blob/master/src/Solver.cpp#L23-L24 you are not allocating the Settings and Data object, causing the segfault.

NUDTUGVexplorer commented 4 years ago

Thanks for your help. After I update the gcc and g++ version to be 4.9.4. Now it woks. I appreciate your kind help.

mzahana commented 4 years ago

@S-Dafarra I am facing the same issue Segmentation fault (core dumped). I am using Ubuntu 18 with g++ and gcc version 7.5.0 . I am compiled osqp from source as described here.

The following lines execute with no errors:

//solver.settings()->setVerbosity(false);
   qpSolver_.settings()->setWarmStart(true);
   // set the initial data of the QP solver
   qpSolver_.data()->setNumberOfVariables((num_of_states_+num_of_inputs_)*mpcWindow);
   qpSolver_.data()->setNumberOfConstraints((2*num_of_states_+num_of_inputs_)*mpcWindow);
   Eigen::SparseMatrix<double> hessian = hessian_.sparseView();
   Eigen::SparseMatrix<double> Ac = Ac_.sparseView();
   if(!qpSolver_.data()->setHessianMatrix(hessian)) return false;
   if(!qpSolver_.data()->setGradient(gradient_)) return false;
   if(!qpSolver_.data()->setLinearConstraintsMatrix(Ac)) return false;
   if(!qpSolver_.data()->setLowerBound(lowerBounds_)) return false;
   if(!qpSolver_.data()->setUpperBound(upperBounds_)) return false;

However, I get the segmentation fault error when the following is executed

qpSolver_.updateBounds(lowerBounds_, upperBounds_);

I made sure that dimensions of the matrices are correct.

What could be the issue?

Thanks in advance.

mzahana commented 4 years ago

Can the OsqpEigen::Solver obj; be a class member? It seems to ave an issue when I use it inside a class as a class member.

mzahana commented 4 years ago

Problem is solved. It was a silly mistake that I didn't initSolver() !