Vector of TMatrixD stored as class member

Hello,

I want to store a std::vector of TMatrixD* objects as member variables of a class.
So far so good, this works.
But I run into a strange thing, when I access the TMatrixD* in the vector from another method (of the same class), then I get a seg fault.
This does not happen with other objects, such as histograms in a vector for example. It also works for vectors of TVectorD*.

Please check out the miminal example that’s attached:
test_tmatrix.C (792 Bytes)

The code loads a matrix from this root file:
matrix.root (1.3 KB)

I’m using 6.06 on lxplus. The same happens with root 6.10.

Any idea, what I’m doing wrong?

Thanks!
Jana

Dear Jana,

you store the reference to a local stack variable in the vector.
When the method load() ends, the memory of Matrix EV1 is freed,
and hence the pointer stored in the vector points into invalid
memory. You need to create the Matrix on the heap using new…

TMatrixD *EV = new TMatrixD(cov_eigen.GetEigenVectors());

m_matrices.push_back(EV);

Cheers,
Jochen

Hi Jochen

thank you very much for the help!
With “new” it works!

Cheers
Jana

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.