title: Ceres-Solver fails with "Terminating: Residual and Jacobian evaluation failed." when non-static variable used to define jacobian for-loop
body: <p>I have a non-linear system I'm using Ceres to solve.  It's a sparse system with a sparse block structure.  Since I'm also working on image data, I've based my code off of the 'denoising.cc' example.  </p>
  
  <p>The issue I'm encountering is that my code fails with "Terminating: Residual and Jacobian evaluation failed.".  I'm able to fix the issue by hard-coding the variable 'num_weights' in Evaluate.</p>
  
  <p>The issue persists when I call this function on one or on many pixels.  For each pixel, my weights are different.  </p>
  
  <p>Any insight as to why this is will help.</p>
  
  <p>Thanks!</p>
  
  <pre><code>Cost::Cost(const std::vector&lt;double&gt; &amp;weights) : _weights(weights)
  {
      set_num_residuals(1);
      mutable_parameter_block_sizes()-&gt;push_back(1); //has more parameters than weights
  
      for (int i = 0; i &lt; _weights.size(); ++i)
          mutable_parameter_block_sizes()-&gt;push_back(1);
  }
  
  bool Cost::Evaluate(double const* const* parameters,
                               double *residuals,
                               double **jacobians) const
  {
      int num_weights = (int)_weights.size();
  
      float d0 = parameters[0][0];
      residuals[0] = d0;
  
      for (int i = 0; i &lt; num_weights; ++i)
      {
          residuals[0] += parameters[i+1][0];
      }
  
      if (jacobians != NULL)
      {
          for (int i = 0; i &lt; num_weights+1; ++i)
          {
              if (jacobians[i] != NULL)
              {
                  jacobians[i][0] = 0;
              }
          }
      }
  
      return true;
  }
  </code></pre>