• <bdo id='NS5FW'></bdo><ul id='NS5FW'></ul>

      <small id='NS5FW'></small><noframes id='NS5FW'>

    1. <legend id='NS5FW'><style id='NS5FW'><dir id='NS5FW'><q id='NS5FW'></q></dir></style></legend><tfoot id='NS5FW'></tfoot>
    2. <i id='NS5FW'><tr id='NS5FW'><dt id='NS5FW'><q id='NS5FW'><span id='NS5FW'><b id='NS5FW'><form id='NS5FW'><ins id='NS5FW'></ins><ul id='NS5FW'></ul><sub id='NS5FW'></sub></form><legend id='NS5FW'></legend><bdo id='NS5FW'><pre id='NS5FW'><center id='NS5FW'></center></pre></bdo></b><th id='NS5FW'></th></span></q></dt></tr></i><div id='NS5FW'><tfoot id='NS5FW'></tfoot><dl id='NS5FW'><fieldset id='NS5FW'></fieldset></dl></div>

        cpp - valgrind - 读取大小 8 无效

        cpp - valgrind - Invalid read of size 8(cpp - valgrind - 读取大小 8 无效)
          <bdo id='4Lpu3'></bdo><ul id='4Lpu3'></ul>

          • <tfoot id='4Lpu3'></tfoot>
            <legend id='4Lpu3'><style id='4Lpu3'><dir id='4Lpu3'><q id='4Lpu3'></q></dir></style></legend>

          • <i id='4Lpu3'><tr id='4Lpu3'><dt id='4Lpu3'><q id='4Lpu3'><span id='4Lpu3'><b id='4Lpu3'><form id='4Lpu3'><ins id='4Lpu3'></ins><ul id='4Lpu3'></ul><sub id='4Lpu3'></sub></form><legend id='4Lpu3'></legend><bdo id='4Lpu3'><pre id='4Lpu3'><center id='4Lpu3'></center></pre></bdo></b><th id='4Lpu3'></th></span></q></dt></tr></i><div id='4Lpu3'><tfoot id='4Lpu3'></tfoot><dl id='4Lpu3'><fieldset id='4Lpu3'></fieldset></dl></div>

                <small id='4Lpu3'></small><noframes id='4Lpu3'>

                    <tbody id='4Lpu3'></tbody>
                  本文介绍了cpp - valgrind - 读取大小 8 无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我对这个 valgrind 错误感到非常生气.我有一个名为 Matrix 的模板类,它有一些重载的运算符等......来做一些数学运算.矩阵在称为 ExtendedKalmanFilter 的类中使用.

                  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.

                  这是 valgrind 跟踪:

                  Here is the valgrind trace:

                  ==3352== Invalid read of size 8
                  ==3352==    at 0x804CC8F: BOViL::math::Matrix<double>::operator*(BOViL::math::Matrix<double> const&) 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<double> const&, 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<double>::operator=(BOViL::math::Matrix<double> const&) (Matrix.h:224)
                  ==3352==    by 0x8051C62: BOViL::algorithms::ExtendedKalmanFilter::setUpEKF(BOViL::math::Matrix<double>, BOViL::math::Matrix<double>, BOViL::math::Matrix<double>) (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<double>::operator*(BOViL::math::Matrix<double> const&) 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<double> const&, 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<double>::operator*(BOViL::math::Matrix<double> const&) 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<double> const&, 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<double>::operator*(BOViL::math::Matrix<double> const&) 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<double> const&, 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<double>::operator*(BOViL::math::Matrix<double> const&) 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<double> const&, 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<double>::operator*(BOViL::math::Matrix<double> const&) 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<double> const&, 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<double>::operator*(BOViL::math::Matrix<double> const&) 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<double> const&, 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<double>::Matrix(double const*, int, int) (Matrix.h:118)
                  ==3352==    by 0x804CCF3: BOViL::math::Matrix<double>::operator*(BOViL::math::Matrix<double> const&) 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<double> const&, double) (ExtendedKalmanFilter.cpp:39)
                  ==3352==    by 0x804B98F: testSegmentation() (TestSegmentation.cpp:53)
                  ==3352==    by 0x805266D: main (main.cpp:16)
                  

                  这里是代码片段:

                  --> 矩阵界面

                      template <typename type_> 
                      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& _mat);     // Copy constructor
                          Matrix(Matrix&& _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<<(const Matrix<type_>& _mat) const;        // Operator for cout 666 TODO:
                          type_& operator[](int _index);
                          Matrix operator=(const Matrix& _mat);               // Assignement operator
                          Matrix operator+(const Matrix& _mat) const;     // Add operator
                          Matrix operator-(const Matrix& _mat) const;     // Sub operator
                          Matrix operator*(const Matrix& _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&(const Matrix& _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& _L, Matrix& _U);
                          bool decompositionCholesky(Matrix& _L, Matrix& _Lt);
                          bool decompositionLDL(Matrix& _L, Matrix& _D, Matrix& _Lt);
                          bool decompositionQR_GR(Matrix& _Q, Matrix& _R);        // QR decomposition using Householder reflexions algorithm.
                  
                          Matrix inverse();       // Using QR algorithm
                  
                  
                      private:    // Private interface
                          int mCols, mRows;
                          type_* mPtr;
                  
                      };
                  

                  -->这里是矩阵崩溃的地方:

                  -->And here is where matrix crash:

                  void ExtendedKalmanFilter::forecastStep(const double _incT){
                      updateJf(_incT);
                  
                      mXfk = mJf * mXak;  <<<----- HERE CRASH, inside operator*
                  
                      mP = mJf * mP * mJf.transpose() + mQ;
                  }
                  

                  准确地说,它在构造函数矩阵内崩溃(type_* ptr, int _cols, int _rows);初始化指针时

                  To be precise, it crash inside the constructor matrix(type_* ptr, int _cols, int _rows); while initializing the pointer

                  template<typename type_> 
                  Matrix<type_> Matrix<type_>::operator* (const Matrix<type_>& _mat) const{
                      if(mCols !=_mat.mRows)
                          assert(false);
                  
                      type_* ptr = new type_[mRows*_mat.mCols];
                  
                      for(int i = 0; i < mRows ; i ++ ){
                          for(int j = 0 ; j < mCols ; j ++){
                              ptr[_mat.mCols * i + j] = 0;
                              for(int k = 0 ; k < _mat.mRows ; k ++){
                                  ptr[_mat.mCols * i + j] += mPtr[mCols * i + k] * _mat.mPtr[_mat.mCols * k + j];
                              }
                          }
                      }
                  
                      Matrix<type_> mat(ptr, mRows, _mat.mCols); <<< ----- HERE
                      delete[] ptr;
                  
                      return mat;
                  }
                  
                  
                  template<typename type_> 
                  Matrix<type_>::Matrix(const type_* _matPtr, int _rows, int _cols):  mPtr(new type_[_cols*_rows]),
                                                                                          mCols(_cols),
                                                                                          mRows(_rows)        
                      { <<<---- CRASH before getting into (So I suppose that crash in the new type_[_cols*_rows]
                          for(int i = 0; i < _cols*_rows ; i ++){
                              mPtr[i] = _matPtr[i];
                          }
                      }
                  

                  最后,类的析构函数是:

                  Finally, the destructor of the class is:

                  template<typename type_> 
                  Matrix<type_>::~Matrix(){
                      if(mPtr)
                          delete[] mPtr;
                  }
                  

                  谁能帮帮我?我在 Windows 中使用 Visual Studio 和 linux 中使用 valgrind 尝试调试时找不到问题.

                  Can anyone help me? I cant find the trouble I tryed debugging with Visual Studio in windows and with valgrind in linux.

                  提前致谢

                  推荐答案

                  你的第一个错误说:

                  ==3352== Invalid read of size 8
                  ==3352==    at 0x804CC8F: BOViL::math::Matrix<double>::operator*(BOViL::math::Matrix<double> const&) 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<double> const&, 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<double>::operator=(BOViL::math::Matrix<double> const&) (Matrix.h:224)
                  ==3352==    by 0x8051C62: BOViL::algorithms::ExtendedKalmanFilter::setUpEKF(BOViL::math::Matrix<double>, BOViL::math::Matrix<double>, BOViL::math::Matrix<double>) (ExtendedKalmanFilter.cpp:23)
                  ==3352==    by 0x804B74F: testSegmentation() (TestSegmentation.cpp:37)
                  ==3352==    by 0x805266D: main (main.cpp:16)
                  

                  简而言之:

                  ==3352== Invalid read of size 8
                  ==3352==    at 0x804CC8F: BOViL::math::Matrix<double>::operator*(BOViL::math::Matrix<double> const&) const (Matrix.h:285)
                  ==3352==  Address 0x6a8b3c0 is 0 bytes after a block of size 48 alloc'd
                  

                  知道你的矩阵是double,这意味着矩阵内部的数组被分配为包含6个元素(48/sizeof double).但是,您正在访问块后的 0 个字节,这意味着您正在访问元素索引 6.

                  Knowing that your matrix is of double, that means the array inside the matrix is allocated to contain 6 elements (48/sizeof double). However, you are accessing 0 bytes after the block, which means you are accessing exactly element index 6.

                  因此,您需要验证两件事:

                  So there are two things that you need to verify:

                  1. 6 正确吗?数组应该包含 6 个元素吗?
                  2. Matrix.h 的第 285 行,它可能在 for 循环内,不是在这里:

                  1. Is 6 correct? Should the array contain 6 elements?
                  2. At line 285 of Matrix.h, which is likely inside the for loops, not here:

                  Matrix<type_> mat(ptr, mRows, _mat.mCols); <<< ----- HERE
                  

                  您需要检查您为数组提供的索引.很可能,您会发现数组的索引为 6,这就是您应该找出为什么的地方.

                  you need to examine what indices you are giving to the array. Likely, you will find the array being indexed at 6 and that's where you should figure out why.

                  这篇关于cpp - valgrind - 读取大小 8 无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Constructor initialization Vs assignment(构造函数初始化 Vs 赋值)
                  Is a `=default` move constructor equivalent to a member-wise move constructor?(`=default` 移动构造函数是否等同于成员移动构造函数?)
                  Has the new C++11 member initialization feature at declaration made initialization lists obsolete?(声明时新的 C++11 成员初始化功能是否使初始化列表过时了?)
                  Order of constructor call in virtual inheritance(虚继承中构造函数调用的顺序)
                  How to use sfinae for selecting constructors?(如何使用 sfinae 选择构造函数?)
                  Initializing a union with a non-trivial constructor(使用非平凡的构造函数初始化联合)
                    <i id='f1r0H'><tr id='f1r0H'><dt id='f1r0H'><q id='f1r0H'><span id='f1r0H'><b id='f1r0H'><form id='f1r0H'><ins id='f1r0H'></ins><ul id='f1r0H'></ul><sub id='f1r0H'></sub></form><legend id='f1r0H'></legend><bdo id='f1r0H'><pre id='f1r0H'><center id='f1r0H'></center></pre></bdo></b><th id='f1r0H'></th></span></q></dt></tr></i><div id='f1r0H'><tfoot id='f1r0H'></tfoot><dl id='f1r0H'><fieldset id='f1r0H'></fieldset></dl></div>
                    <tfoot id='f1r0H'></tfoot>
                    1. <small id='f1r0H'></small><noframes id='f1r0H'>

                          <tbody id='f1r0H'></tbody>
                          <bdo id='f1r0H'></bdo><ul id='f1r0H'></ul>

                          • <legend id='f1r0H'><style id='f1r0H'><dir id='f1r0H'><q id='f1r0H'></q></dir></style></legend>