title: cpp - valgrind - Invalid read of size 8
body: <p>I'm getting mad understanding that valgrind error. I've got a template class called Matrix that has some overloaded operators etc... to do some mathematical operations. Matrixes are used inside a class called ExtendedKalmanFilter. </p>
  
  <p>Here is the valgrind trace:</p>
  
  <pre><code>==3352== Invalid read of size 8
  ==3352==    at 0x804CC8F: BOViL::math::Matrix&lt;double&gt;::operator*(BOViL::math::Matrix&lt;double&gt; const&amp;) const (Matrix.h:285)
  ==3352==    by 0x8051F91: BOViL::algorithms::ExtendedKalmanFilter::forecastStep(double) (ExtendedKalmanFilter.cpp:48)
  ==3352==    by 0x8051F25: BOViL::algorithms::ExtendedKalmanFilter::stepEKF(BOViL::math::Matrix&lt;double&gt; const&amp;, double) (ExtendedKalmanFilter.cpp:39)
  ==3352==    by 0x804B98F: testSegmentation() (TestSegmentation.cpp:53)
  ==3352==    by 0x805266D: main (main.cpp:16)
  ==3352==  Address 0x6a8b3c0 is 0 bytes after a block of size 48 alloc'd
  ==3352==    at 0x402B454: operator new[](unsigned int) (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
  ==3352==    by 0x804C986: BOViL::math::Matrix&lt;double&gt;::operator=(BOViL::math::Matrix&lt;double&gt; const&amp;) (Matrix.h:224)
  ==3352==    by 0x8051C62: BOViL::algorithms::ExtendedKalmanFilter::setUpEKF(BOViL::math::Matrix&lt;double&gt;, BOViL::math::Matrix&lt;double&gt;, BOViL::math::Matrix&lt;double&gt;) (ExtendedKalmanFilter.cpp:23)
  ==3352==    by 0x804B74F: testSegmentation() (TestSegmentation.cpp:37)
  ==3352==    by 0x805266D: main (main.cpp:16)
  ==3352== 
  ==3352== Invalid write of size 8
  ==3352==    at 0x804CC12: BOViL::math::Matrix&lt;double&gt;::operator*(BOViL::math::Matrix&lt;double&gt; const&amp;) const (Matrix.h:283)
  ==3352==    by 0x8051F91: BOViL::algorithms::ExtendedKalmanFilter::forecastStep(double) (ExtendedKalmanFilter.cpp:48)
  ==3352==    by 0x8051F25: BOViL::algorithms::ExtendedKalmanFilter::stepEKF(BOViL::math::Matrix&lt;double&gt; const&amp;, double) (ExtendedKalmanFilter.cpp:39)
  ==3352==    by 0x804B98F: testSegmentation() (TestSegmentation.cpp:53)
  ==3352==    by 0x805266D: main (main.cpp:16)
  ==3352==  Address 0x6a8d210 is 0 bytes after a block of size 48 alloc'd
  ==3352==    at 0x402B454: operator new[](unsigned int) (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
  ==3352==    by 0x804CBD8: BOViL::math::Matrix&lt;double&gt;::operator*(BOViL::math::Matrix&lt;double&gt; const&amp;) const (Matrix.h:279)
  ==3352==    by 0x8051F91: BOViL::algorithms::ExtendedKalmanFilter::forecastStep(double) (ExtendedKalmanFilter.cpp:48)
  ==3352==    by 0x8051F25: BOViL::algorithms::ExtendedKalmanFilter::stepEKF(BOViL::math::Matrix&lt;double&gt; const&amp;, double) (ExtendedKalmanFilter.cpp:39)
  ==3352==    by 0x804B98F: testSegmentation() (TestSegmentation.cpp:53)
  ==3352==    by 0x805266D: main (main.cpp:16)
  ==3352== 
  ==3352== Invalid read of size 8
  ==3352==    at 0x804CC55: BOViL::math::Matrix&lt;double&gt;::operator*(BOViL::math::Matrix&lt;double&gt; const&amp;) const (Matrix.h:285)
  ==3352==    by 0x8051F91: BOViL::algorithms::ExtendedKalmanFilter::forecastStep(double) (ExtendedKalmanFilter.cpp:48)
  ==3352==    by 0x8051F25: BOViL::algorithms::ExtendedKalmanFilter::stepEKF(BOViL::math::Matrix&lt;double&gt; const&amp;, double) (ExtendedKalmanFilter.cpp:39)
  ==3352==    by 0x804B98F: testSegmentation() (TestSegmentation.cpp:53)
  ==3352==    by 0x805266D: main (main.cpp:16)
  ==3352==  Address 0x6a8d210 is 0 bytes after a block of size 48 alloc'd
  ==3352==    at 0x402B454: operator new[](unsigned int) (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
  ==3352==    by 0x804CBD8: BOViL::math::Matrix&lt;double&gt;::operator*(BOViL::math::Matrix&lt;double&gt; const&amp;) const (Matrix.h:279)
  ==3352==    by 0x8051F91: BOViL::algorithms::ExtendedKalmanFilter::forecastStep(double) (ExtendedKalmanFilter.cpp:48)
  ==3352==    by 0x8051F25: BOViL::algorithms::ExtendedKalmanFilter::stepEKF(BOViL::math::Matrix&lt;double&gt; const&amp;, double) (ExtendedKalmanFilter.cpp:39)
  ==3352==    by 0x804B98F: testSegmentation() (TestSegmentation.cpp:53)
  ==3352==    by 0x805266D: main (main.cpp:16)
  ==3352== 
  ==3352== Invalid write of size 8
  ==3352==    at 0x804CC95: BOViL::math::Matrix&lt;double&gt;::operator*(BOViL::math::Matrix&lt;double&gt; const&amp;) const (Matrix.h:285)
  ==3352==    by 0x8051F91: BOViL::algorithms::ExtendedKalmanFilter::forecastStep(double) (ExtendedKalmanFilter.cpp:48)
  ==3352==    by 0x8051F25: BOViL::algorithms::ExtendedKalmanFilter::stepEKF(BOViL::math::Matrix&lt;double&gt; const&amp;, double) (ExtendedKalmanFilter.cpp:39)
  ==3352==    by 0x804B98F: testSegmentation() (TestSegmentation.cpp:53)
  ==3352==    by 0x805266D: main (main.cpp:16)
  ==3352==  Address 0x6a8d210 is 0 bytes after a block of size 48 alloc'd
  ==3352==    at 0x402B454: operator new[](unsigned int) (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
  ==3352==    by 0x804CBD8: BOViL::math::Matrix&lt;double&gt;::operator*(BOViL::math::Matrix&lt;double&gt; const&amp;) const (Matrix.h:279)
  ==3352==    by 0x8051F91: BOViL::algorithms::ExtendedKalmanFilter::forecastStep(double) (ExtendedKalmanFilter.cpp:48)
  ==3352==    by 0x8051F25: BOViL::algorithms::ExtendedKalmanFilter::stepEKF(BOViL::math::Matrix&lt;double&gt; const&amp;, double) (ExtendedKalmanFilter.cpp:39)
  ==3352==    by 0x804B98F: testSegmentation() (TestSegmentation.cpp:53)
  ==3352==    by 0x805266D: main (main.cpp:16)
  ==3352== 
  --3352-- VALGRIND INTERNAL ERROR: Valgrind received a signal 11 (SIGSEGV) - exiting
  --3352-- si_code=1;  Faulting address: 0x6F666562;  sp: 0x6800fa88
  
  valgrind: the 'impossible' happened:
     Killed by fatal signal
  ==3352==    at 0x380C0AD4: ??? (in /usr/lib/valgrind/memcheck-x86-linux)
  ==3352==    by 0x380C12C5: ??? (in /usr/lib/valgrind/memcheck-x86-linux)
  ==3352==    by 0x38040A63: ??? (in /usr/lib/valgrind/memcheck-x86-linux)
  ==3352==    by 0x38040B36: ??? (in /usr/lib/valgrind/memcheck-x86-linux)
  ==3352==    by 0x3803EA4B: ??? (in /usr/lib/valgrind/memcheck-x86-linux)
  ==3352==    by 0x74206572: ???
  
  sched status:
    running_tid=1
  
  Thread 1: status = VgTs_Runnable
  ==3352==    at 0x402B454: operator new[](unsigned int) (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
  ==3352==    by 0x804BD52: BOViL::math::Matrix&lt;double&gt;::Matrix(double const*, int, int) (Matrix.h:118)
  ==3352==    by 0x804CCF3: BOViL::math::Matrix&lt;double&gt;::operator*(BOViL::math::Matrix&lt;double&gt; const&amp;) const (Matrix.h:290)
  ==3352==    by 0x8051F91: BOViL::algorithms::ExtendedKalmanFilter::forecastStep(double) (ExtendedKalmanFilter.cpp:48)
  ==3352==    by 0x8051F25: BOViL::algorithms::ExtendedKalmanFilter::stepEKF(BOViL::math::Matrix&lt;double&gt; const&amp;, double) (ExtendedKalmanFilter.cpp:39)
  ==3352==    by 0x804B98F: testSegmentation() (TestSegmentation.cpp:53)
  ==3352==    by 0x805266D: main (main.cpp:16)
  </code></pre>
  
  <p>And here fragments of the code:</p>
  
  <p>--> Matrix interface</p>
  
  <pre><code>    template &lt;typename type_&gt; 
      class Matrix{
      public:     // Main interface
          Matrix();       // Default constructor
          Matrix(int _cols, int _rows);       // Empty matrix constructor
          Matrix(const type_* _mat, int _rows, int _cols);    // Full-defined matrix constructor
          Matrix(const Matrix&amp; _mat);     // Copy constructor
          Matrix(Matrix&amp;&amp; _mat);          // Move constructor c++11
  
          ~Matrix();      // De-constructor
  
          type_* getMatrixPtr() const;
          int getWidth() const;
          int getHeight() const;
  
          void showMatrix() const;
  
     public:  // Overloaded Operators
          std::string operator&lt;&lt;(const Matrix&lt;type_&gt;&amp; _mat) const;        // Operator for cout 666 TODO:
          type_&amp; operator[](int _index);
          Matrix operator=(const Matrix&amp; _mat);               // Assignement operator
          Matrix operator+(const Matrix&amp; _mat) const;     // Add operator
          Matrix operator-(const Matrix&amp; _mat) const;     // Sub operator
          Matrix operator*(const Matrix&amp; _mat) const;     // Mul operator
          Matrix operator*(const type_ _scalar) const;        // Scalar operator
          Matrix operator^(const double _exp) const;      // Pow operator     666 TODO:
  
      public: // Other operations 666 TODO: Change names
          Matrix operator&amp;(const Matrix&amp; _mat) const;     // Projection operator._mat is projected to this
          Matrix transpose();                             // Transpose operator
          type_ determinant();                            // Determinant operator
  
      public:     // Various algorithms
          double norm();
          bool decompositionLU(Matrix&amp; _L, Matrix&amp; _U);
          bool decompositionCholesky(Matrix&amp; _L, Matrix&amp; _Lt);
          bool decompositionLDL(Matrix&amp; _L, Matrix&amp; _D, Matrix&amp; _Lt);
          bool decompositionQR_GR(Matrix&amp; _Q, Matrix&amp; _R);        // QR decomposition using Householder reflexions algorithm.
  
          Matrix inverse();       // Using QR algorithm
  
  
      private:    // Private interface
          int mCols, mRows;
          type_* mPtr;
  
      };
  </code></pre>
  
  <p>-->And here is where matrix crash:</p>
  
  <pre><code>void ExtendedKalmanFilter::forecastStep(const double _incT){
      updateJf(_incT);
  
      mXfk = mJf * mXak;  &lt;&lt;&lt;----- HERE CRASH, inside operator*
  
      mP = mJf * mP * mJf.transpose() + mQ;
  }
  </code></pre>
  
  <p>To be precise, it crash inside the constructor matrix(type_* ptr, int _cols, int _rows); while initializing the pointer</p>
  
  <pre><code>template&lt;typename type_&gt; 
  Matrix&lt;type_&gt; Matrix&lt;type_&gt;::operator* (const Matrix&lt;type_&gt;&amp; _mat) const{
      if(mCols !=_mat.mRows)
          assert(false);
  
      type_* ptr = new type_[mRows*_mat.mCols];
  
      for(int i = 0; i &lt; mRows ; i ++ ){
          for(int j = 0 ; j &lt; mCols ; j ++){
              ptr[_mat.mCols * i + j] = 0;
              for(int k = 0 ; k &lt; _mat.mRows ; k ++){
                  ptr[_mat.mCols * i + j] += mPtr[mCols * i + k] * _mat.mPtr[_mat.mCols * k + j];
              }
          }
      }
  
      Matrix&lt;type_&gt; mat(ptr, mRows, _mat.mCols); &lt;&lt;&lt; ----- HERE
      delete[] ptr;
  
      return mat;
  }
  
  
  template&lt;typename type_&gt; 
  Matrix&lt;type_&gt;::Matrix(const type_* _matPtr, int _rows, int _cols):  mPtr(new type_[_cols*_rows]),
                                                                          mCols(_cols),
                                                                          mRows(_rows)        
      { &lt;&lt;&lt;---- CRASH before getting into (So I suppose that crash in the new type_[_cols*_rows]
          for(int i = 0; i &lt; _cols*_rows ; i ++){
              mPtr[i] = _matPtr[i];
          }
      }
  </code></pre>
  
  <p>Finally, the destructor of the class is:</p>
  
  <pre><code>template&lt;typename type_&gt; 
  Matrix&lt;type_&gt;::~Matrix(){
      if(mPtr)
          delete[] mPtr;
  }
  </code></pre>
  
  <p>Can anyone help me? I cant find the trouble I tryed debugging with Visual Studio in windows and with valgrind in linux.</p>
  
  <p>Thanks in advance</p>