This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c++/33599] New: segfault in program compiled by g++ 4.2, corrupted reference
- From: "jacob at math dot jussieu dot fr" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 30 Sep 2007 08:55:10 -0000
- Subject: [Bug c++/33599] New: segfault in program compiled by g++ 4.2, corrupted reference
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
Hello,
I am developing a C++ template library (a matrix library with expression
templates). Upgrading from g++-4.1 to g++-4.2, the programs using this library
run much (4x) faster, but some segfault!
I have fixed for this bug report an archive containing the bare minimum to
reproduce this bug.
To reproduce:
tar xfzv gcc-bug-report.tar.gz
cd gcc-bug-report
Then you can check that it works with g++-4.1:
g++-4.1 test.cpp -o test && ./test
And you can check that it segfaults with g++-4.2:
g++-4.2 test.cpp -o test && ./test
Now let us look into where exactly it segfaults. This happens in
EiMatrixConstRef::_read(), so let us look at the file:
src/internal/MatrixRef.h.
Here is the class EiMatrixConstRef:
template<typename MatrixType> class EiMatrixConstRef
: public EiObject<typename MatrixType::Scalar, EiMatrixConstRef<MatrixType> >
{
public:
typedef typename MatrixType::Scalar Scalar;
friend class EiObject<Scalar, EiMatrixConstRef>;
EiMatrixConstRef(const MatrixType& matrix) : m_matrix(matrix)
{
std::cout << "contruct ref " << this << " on matrix " << &m_matrix <<
std::endl;
}
EiMatrixConstRef(const EiMatrixConstRef& other) : m_matrix(other.m_matrix)
{
std::cout << "contruct ref " << this << " from ref " << &other << " on
matrix " << &m_matrix <<
std::endl;
}
~EiMatrixConstRef() {std::cout << "destruct ref " << this << std::endl;}
EI_INHERIT_ASSIGNMENT_OPERATORS(EiMatrixConstRef)
private:
int _rows() const { return m_matrix.rows(); }
int _cols() const { return m_matrix.cols(); }
const Scalar& _read(int row, int col) const
{
std::cout << "ref " << this << " reading in matrix " << &m_matrix <<
std::endl;
return m_matrix._read(row, col);
}
const MatrixType& m_matrix;
};
So what happens is that the last call to EiMatrixConstRef::_read() segfaults
because the reference m_matrix is corrupted, i.e. as a pointer it has a bad
value.
This is strange because the cout's that I have added to the construcors and
destructor show that this EiMatrixConstRef object is properly constructed with
a good m_matrix reference, and not destructed since.
So at some point the m_matrix reference gets corrupted. The fact that this
didn't happen with g++-4.1 suggests to me that this might be a bug in g++-4.2.
Of course I might be wrong, I'm not an expert.
Cheers,
Benoit
--
Summary: segfault in program compiled by g++ 4.2, corrupted
reference
Product: gcc
Version: 4.2.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: jacob at math dot jussieu dot fr
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=33599