title: Segmentation fault when performing bundle adjustment with g2o
body: <pre><code>void bundleAdjustment ( const vector&lt; cv::Point3f &gt; points_3d,
                           const vector&lt; cv::Point2f &gt; points_2d, 
                           const Mat&amp; K, Mat&amp; R, Mat&amp; t )
  {
  typedef g2o::BlockSolver&lt; g2o::BlockSolverTraits&lt;6,3&gt; &gt; Block;
  Block::LinearSolverType* linearSolver = new g2o::LinearSolverCSparseBlock::PoseMatrixType();
  Block* solver_ptr = new Block ( linearSolver );
  g2o::OptimizationAlgorithmLevenberg* solver = new g2o::OptimizationAlgorithmLevenberg ( solver_ptr );
  g2o::SparseOptimizer optimizer;
  optimizer.setAlgorithm ( solver );
  
  g2o::VertexSE3Expmap* pose = new g2o::VertexSE3Expmap(); // camera pose
  Eigen::Matrix3d R_mat;
  R_mat &lt;&lt;
  R.at&lt;double&gt; ( 0,0 ), R.at&lt;double&gt; ( 0,1 ), R.at&lt;double&gt; ( 0,2 ),
  R.at&lt;double&gt; ( 1,0 ), R.at&lt;double&gt; ( 1,1 ), R.at&lt;double&gt; ( 1,2 ),
  R.at&lt;double&gt; ( 2,0 ), R.at&lt;double&gt; ( 2,1 ), R.at&lt;double&gt; ( 2,2 );
  pose-&gt;setId ( 0 );
  pose-&gt;setEstimate ( g2o::SE3Quat (R_mat,
  Eigen::Vector3d ( t.at ( 0,0 ), t.at ( 1,0 ), t.at ( 2,0 ) ) ) );
  optimizer.addVertex ( pose );
  
  int index = 1;
  for (const  Point3f p:points_3d )   // landmarks, world coordinates.
  {
      g2o::VertexSBAPointXYZ* point = new g2o::VertexSBAPointXYZ();
      point-&gt;setId ( index++ ); 
      point-&gt;setEstimate ( Eigen::Vector3d ( p.x, p.y, p.z ) );
      point-&gt;setMarginalized ( true ); 
     optimizer.addVertex ( point );
  }
  
  g2o::CameraParameters* camera = new g2o::CameraParameters (K.at&lt;double&gt; ( 0,0 ), Eigen::Vector2d ( K.at&lt;double&gt; ( 0,2 ), K.at&lt;double&gt; ( 1,2 ) ), 0);
  camera-&gt;setId ( 0 );
  optimizer.addParameter ( camera ); //Add camera parameters
  
  index = 1;
  for ( const Point2f p:points_2d ) //Add edges(observed data)
  {
      g2o::EdgeProjectXYZ2UV* edge = new g2o::EdgeProjectXYZ2UV();
      edge-&gt;setId ( index );
      edge-&gt;setVertex ( 0, dynamic_cast&lt;g2o::VertexSBAPointXYZ*&gt; ( optimizer.vertex ( index ) ) );
      edge-&gt;setVertex ( 1, pose );
      edge-&gt;setMeasurement ( Eigen::Vector2d ( p.x, p.y ) );
      edge-&gt;setParameterId ( 0,0 );  //cameraparameter
      edge-&gt;setInformation ( Eigen::Matrix2d::Identity() );
      optimizer.addEdge ( edge );
      index++;
  }
  
  chrono::steady_clock::time_point t1 = chrono::steady_clock::now();
  optimizer.setVerbose ( true );
  optimizer.initializeOptimization();
  optimizer.optimize ( 100 );
  chrono::steady_clock::time_point t2 = chrono::steady_clock::now();
  chrono::duration&lt;double&gt; time_used = chrono::duration_cast&lt;chrono::duration&lt;double&gt;&gt; ( t2-t1 );
  cout&lt;&lt;"optimization costs time: "&lt;&lt;time_used.count() &lt;&lt;" seconds."&lt;&lt;endl;
  
  
  cout&lt;&lt;endl&lt;&lt;"after optimization:"&lt;&lt;endl;
  cout&lt;&lt;"T="&lt;&lt;endl&lt;&lt;Eigen::Isometry3d ( pose-&gt;estimate() ).matrix() &lt;&lt;endl;
  
  }
  </code></pre>
  
  <p>I have defined a function to perform bundle adjustment with g2o, but terminal asserted <strong>Segmentation Fault</strong> when I ran the executable. I am sure that problem comes from this function because the whole program can run properly after commenting out this function.</p>
  
  <p>My platform is Ubuntu16.04 LTS.</p>
  
  <p><strong>EDIT1</strong>: I tried to add <code>set( CMAKE_BUILD_TYPE "Release" )</code>  in my CMakeLists.txt. Then terminal asserted <strong>double free or corruption (out)</strong> after running the executable. However, it ran properly after I commenting out following code block:</p>
  
  <pre><code>g2o::CameraParameters* camera = new g2o::CameraParameters (K.at&lt;double&gt; ( 0,0 ), Eigen::Vector2d ( K.at&lt;double&gt; ( 0,2 ), K.at&lt;double&gt; ( 1,2 ) ), 0);
      camera-&gt;setId ( 0 );
      optimizer.addParameter ( camera );  
  </code></pre>
  
  <p><strong>EDIT2</strong>: The whole program could run properly once I employed default constructor of CameraParameter:</p>
  
  <pre><code>g2o::CameraParameters* camera = new g2o::CameraParameters ();
          camera-&gt;setId ( 0 );
          optimizer.addParameter ( camera );  
  </code></pre>
  
  <p>However, segmentation fault existed still.</p>
  
  <p><strong>EDIT3</strong>: No segmentation faults occur any more after commenting out following codes:</p>
  
  <pre><code>optimizer.addVertex ( pose );
   optimizer.addVertex ( point );
   optimizer.addEdge ( edge );
   optimizer.addParameter ( camera )
  </code></pre>