New user self-registration is currently disabled. Please email eigen-core-team @ if you need an account.
Bug 1381 - Segfault of expression on sparse matrices
Segfault of expression on sparse matrices
Product: Eigen
Classification: Unclassified
Component: Core - expression templates
3.3 (current stable)
All All
: Normal Crash
Assigned To: Nobody
Depends on:
  Show dependency treegraph
Reported: 2017-01-23 00:24 UTC by Yixuan Qiu
Modified: 2017-01-25 18:08 UTC (History)
3 users (show)


Description Yixuan Qiu 2017-01-23 00:24:20 UTC
Hi All,

Below is a simple example that generates a segment fault on a sparse matrix expression.


#include <Eigen/Core>
#include <Eigen/SparseCore>

typedef Eigen::SparseMatrix<double> SpMat;
using Eigen::ArrayXd;

int main()
    SpMat m(10, 10);
    ArrayXd xx = m.diagonal().array();

    return 0;


After tracing back the call stack, I found that it was due to a return value of a local address.

In the assignment operation (, the signature of the source data is "const SrcScalar& b". However, the sparse matrix expression can only return a local "Scalar" ( Therefore, a local address will be returned ( to the assignment operator that results a reading data error.

Comment 1 Gael Guennebaud 2017-01-25 15:19:03 UTC
On your side you can omit the ".array()".
Comment 2 Gael Guennebaud 2017-01-25 16:41:19 UTC
Fixed: (3.3)
Summary:     Bug 1381: fix sparse.diagonal() used as a rvalue.
The problem was that is "sparse" is not const, then sparse.diagonal() must have the
LValueBit flag meaning that sparse.diagonal().coeff(i) must returns a const reference,
const Scalar&. However, sparse::coeff() cannot returns a reference for a non-existing
zero coefficient. The trick is to return a reference to a local member of
Comment 3 Yixuan Qiu 2017-01-25 18:08:09 UTC
Thank you!

The background is that I'm preparing for an update of RcppEigen (R wrapper of Eigen), and I checked the compatibility of Eigen 3.3 with the dependent R packages. One package failed due to this bug, so I created a minimal example to mimic its behavior.

Note You need to log in before you can comment on or make changes to this bug.