使用 Valgrind 调试程序 - 检测分段错误

Debugging a program using Valgrind - Detecting a segmentation fault

我有一个 class A 和一个派生自 A 的 class B :

class A
{
  public :
  A() { // Some code }
  virtual ~A(){}
  // etc
};

这是我的 class B :

class B : public A
{
  public :
  B():A(){//Some extra code}
  ~B();
  // etc
};

A 和 B 的真实代码很长所以我不会 post 在那里,除非有必要。

我的问题是,在我的主程序中:

只有当我有一个B的实例时才会出现段错误。

因此,为了找到我的问题所在,我尝试使用 Valgrind,但我完全卡住了,因为我不明白 Valgrind 显示的是什么。

Valgrind 的输出很长,但其中有一个方案,结果如下:

==13512== Invalid read of size 4
==13512==    at 0x852EE9D: __tcf_0 (in /usr/local/lib/libeikeotools.so)
==13512==    by 0x6AE15E9: __cxa_finalize (cxa_finalize.c:56)
==13512==    by 0x84C3E72: ??? (in /usr/local/lib/libeikeotools.so)
==13512==    by 0x4010739: _dl_fini (dl-fini.c:252)
==13512==    by 0x6AE1258: __run_exit_handlers (exit.c:82)
==13512==    by 0x6AE12A4: exit (exit.c:104)
==13512==    by 0x6AC6ECB: (below main) (libc-start.c:321)
==13512==  Address 0x131e4d00 is 16 bytes inside a block of size 32 free'd
==13512==    at 0x4C2C2BC: operator delete(void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==13512==    by 0x664536E: std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string() (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
==13512==    by 0x73A3384: __tcf_0 (logwriter.cpp:32)
==13512==    by 0x6AE15E9: __cxa_finalize (cxa_finalize.c:56)
==13512==    by 0x7301A72: ??? (in /usr/local/lib/libeikeotoolsd.so)
==13512==    by 0x4010739: _dl_fini (dl-fini.c:252)
==13512==    by 0x6AE1258: __run_exit_handlers (exit.c:82)
==13512==    by 0x6AE12A4: exit (exit.c:104)
==13512==    by 0x6AC6ECB: (below main) (libc-start.c:321)
==13512== 
==13512== Invalid free() / delete / delete[] / realloc()
==13512==    at 0x4C2C2BC: operator delete(void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==13512==    by 0x852EEB3: __tcf_0 (in /usr/local/lib/libeikeotools.so)
==13512==    by 0x6AE15E9: __cxa_finalize (cxa_finalize.c:56)
==13512==    by 0x84C3E72: ??? (in /usr/local/lib/libeikeotools.so)
==13512==    by 0x4010739: _dl_fini (dl-fini.c:252)
==13512==    by 0x6AE1258: __run_exit_handlers (exit.c:82)
==13512==    by 0x6AE12A4: exit (exit.c:104)
==13512==    by 0x6AC6ECB: (below main) (libc-start.c:321)
==13512==  Address 0x131e4cf0 is 0 bytes inside a block of size 32 free'd
==13512==    at 0x4C2C2BC: operator delete(void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==13512==    by 0x664536E: std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string() (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
==13512==    by 0x73A3384: __tcf_0 (logwriter.cpp:32)
==13512==    by 0x6AE15E9: __cxa_finalize (cxa_finalize.c:56)
==13512==    by 0x7301A72: ??? (in /usr/local/lib/libeikeotoolsd.so)
==13512==    by 0x4010739: _dl_fini (dl-fini.c:252)
==13512==    by 0x6AE1258: __run_exit_handlers (exit.c:82)
==13512==    by 0x6AE12A4: exit (exit.c:104)
==13512==    by 0x6AC6ECB: (below main) (libc-start.c:321)

编辑 1 : 这是我的 'B' class :

HOGpython.hpp :

#ifndef HOGPYTHON_H
#define HOGPYTHON_H
#include "osell/cvaddenda/HOGDescriptor2.h"
#include <opencv2/core/core.hpp>
#include "conversion.hpp"

#define NO_IMPORT_ARRAY

class  HOGpython : public eikeotools::CvAddenda::HOGDescriptor2
{

    public:

        // Constructeurs :
        HOGpython();
        HOGpython(PyObject* _winSize, PyObject* _blockSize, PyObject* _blockStride, PyObject* _cellSize, int _nbins, double _winSigma);

        // Destructeur :
        ~HOGpython(){ std::cout << " Destroying .." << std::endl;}

        // setters :
        PyObject*   get_winSize() const;
        PyObject*   get_blockSize() const;
        PyObject*   get_blockStride() const;
        PyObject*   get_cellSize() const;
        int         get_nbins() const;
        int         get_derivAperture() const;
        double      get_winSigma() const;
        int         get_histogramNormType() const;
        double      get_L2HysThreshold() const;
        bool        get_gammaCorrection() const;
        int         get_nlevels() const;

        // Setters :
        void    set_winSize( PyObject* o);
        void    set_blockSize( PyObject* o);
        void    set_blockStride( PyObject* o);
        void    set_cellSize( PyObject* o);
        void    set_nbins( int o);
        void    set_derivAperture( int o);
        void    set_winSigma( double o);
        void    set_histogramNormType( int o);
        void    set_L2HysThreshold( double o);
        void    set_gammaCorrection( bool o);
        void    set_nlevels( int o);

        // Méthodes d'instance :
        PyObject* compute(PyObject* img, PyObject* locations);
        PyObject* computeSubPix(PyObject* img, PyObject* locations);
        PyObject* computeGradient(PyObject* img);

};

#endif

和HOGpython.cpp:

#include "HOGpython.hpp"





// ==  Constructeur sans paramètres == //
HOGpython :: HOGpython():eikeotools::CvAddenda::HOGDescriptor2()
{}

// ==  Constructeur avec paramètres == //
HOGpython :: HOGpython(PyObject* _winSize, PyObject* _blockSize, PyObject* _blockStride, PyObject* _cellSize, int _nbins, double _winSigma)
    :eikeotools::CvAddenda::HOGDescriptor2( tupleToSize(_winSize),
                                            tupleToSize(_blockSize),
                                            tupleToSize(_blockStride),
                                            tupleToSize(_cellSize),
                                            _nbins,
                                            1,
                                            _winSigma,
                                            eikeotools::CvAddenda::HOGDescriptor2::L2Hys,
                                            0.2,
                                            false,
                                            eikeotools::CvAddenda::HOGDescriptor2::DEFAULT_NLEVELS)
{}

// ==  Getters == //
PyObject*   HOGpython::get_winSize()            const   {return SizeToTuple(winSize);       }
PyObject*   HOGpython::get_blockSize()          const   {return SizeToTuple(blockSize);     }
PyObject*   HOGpython::get_blockStride()        const   {return SizeToTuple(blockStride);   }
PyObject*   HOGpython::get_cellSize()           const   {return SizeToTuple(cellSize);      }
int         HOGpython::get_nbins()              const   {return nbins;                      }
int         HOGpython::get_derivAperture()      const   {return derivAperture;              }
double      HOGpython::get_winSigma()           const   {return winSigma;                   }
int         HOGpython::get_histogramNormType()  const   {return histogramNormType;          }
double      HOGpython::get_L2HysThreshold()     const   {return L2HysThreshold;             }
bool        HOGpython::get_gammaCorrection()    const   {return gammaCorrection;            }
int         HOGpython::get_nlevels()            const   {return nlevels;                    }

// ==  Setters == //
void    HOGpython::set_winSize(PyObject* o)     {winSize            = tupleToSize(o);}
void    HOGpython::set_blockSize(PyObject* o)   {blockSize          = tupleToSize(o);}
void    HOGpython::set_blockStride(PyObject* o) {blockStride        = tupleToSize(o);}
void    HOGpython::set_cellSize(PyObject* o)    {cellSize           = tupleToSize(o);}
void    HOGpython::set_nbins(int o)             {nbins              = o; return;}
void    HOGpython::set_derivAperture(int o)     {derivAperture      = o; return;}
void    HOGpython::set_winSigma(double o)       {winSigma           = o; return;}
void    HOGpython::set_histogramNormType(int o) {histogramNormType  = o; return;}
void    HOGpython::set_L2HysThreshold(double o) {L2HysThreshold     = o; return;}
void    HOGpython::set_gammaCorrection(bool o)  {gammaCorrection    = o; return;}
void    HOGpython::set_nlevels(int o)           {nlevels            = o; return;}



PyObject* HOGpython::compute(PyObject* img, PyObject* locations)
{
    //  Déclaration d'instances :
    cv::Mat im, pt;
    PyObject* ret;

    // Conversion :
    NDArrayConverter cvt;

    im = cvt.toMat(img);
    pt = cvt.toMat(locations);

    // Déclaration de vecteurs :
    std::vector<float> desc;
    std::vector<cv::Point> pts;

    //Py_BEGIN_ALLOW_THREADS;
    PyThreadState * m_thread_state;
    m_thread_state = PyEval_SaveThread();

    for(int i = 0; i < pt.rows; i++)
    {
        pts.push_back(cv::Point(pt.at<double>(i,0), pt.at<double>(i,1)));
    }

    eikeotools::CvAddenda::HOGDescriptor2::compute(im,desc,cv::Size(),cv::Size(),pts, false);

    PyEval_RestoreThread(m_thread_state);
    m_thread_state = NULL;
    //Py_END_ALLOW_THREADS;
    cv::Mat pointsMat = cv::Mat(desc);
    ret = cvt.toNDArray(pointsMat);

    return ret;
}


PyObject* HOGpython::computeSubPix(PyObject* img, PyObject* locations)
{
    //  Déclaration d'instances :
    cv::Mat im, pt;
    PyObject* ret;

    // Conversion :
    NDArrayConverter cvt;

    im = cvt.toMat(img);
    pt = cvt.toMat(locations);

    // Déclaration de vecteurs :
    std::vector<float> desc;
    std::vector<cv::Point2f> pts;

    //Py_BEGIN_ALLOW_THREADS;
    PyThreadState * m_thread_state;
    m_thread_state = PyEval_SaveThread();

    for(int i = 0; i < pt.rows; i++)
    {
        pts.push_back(cv::Point(pt.at<double>(i,0), pt.at<double>(i,1)));
    }

    eikeotools::CvAddenda::HOGDescriptor2::computeSubPix(im,desc,cv::Size(),cv::Size(),pts, false);

    PyEval_RestoreThread(m_thread_state);
    m_thread_state = NULL;
    //Py_END_ALLOW_THREADS;
    cv::Mat pointsMat = cv::Mat(desc);
    ret = cvt.toNDArray(pointsMat);

    return ret;
}

PyObject* HOGpython::computeGradient(PyObject* img)
{
    // Déclaration d'instances :
    cv::Mat im, grad, angleOfs;
    PyObject* listeRes;
    PyObject* grad_;
    PyObject* angleOfs_;

    // Conversion :
    NDArrayConverter cvt;
    im = cvt.toMat(img);

    //Py_BEGIN_ALLOW_THREADS;
    PyThreadState * m_thread_state;
    m_thread_state = PyEval_SaveThread();
    eikeotools::CvAddenda::HOGDescriptor2::computeGradient(im, grad, angleOfs, cv::Size(), cv::Size());
    PyEval_RestoreThread(m_thread_state);
    m_thread_state = NULL;
    //Py_END_ALLOW_THREADS;

    // Conversion :
    grad_ = cvt.toNDArray(grad);
    angleOfs_ = cvt.toNDArray(angleOfs);

    // Création d'une liste Python dans laquelle les deux résultats seront enregistrés :
    listeRes = PyList_New(0);
    PyList_Append(listeRes, grad_);
    PyList_Append(listeRes, angleOfs_);

    // Renvoie des données :
    return listeRes;

    }

不幸的是,我无法显示我的 class A(框架的一部分),但我可以看出 A 不会导致任何分段错误。

只是为了总结我的代码,我想为 python 包装我的 class A,并且由于 A 包含一些 OpenCV 对象,Boost.Python 不能自动转换它们,所以我创建了从 A 派生的 B,但是从 opencv 对象到 Python 对象进行了一些转换。

为了进行转换,我有一个工作正常的转换模块。 我想添加一些内容:这是给我一个段错误的主要代码:

int main()
{
  HOGpython h;
  return 0;
}

这看起来像是一个全局对象的析构函数(可能是 std::string 或与 std::string 有某种关系的东西)被调用,但要么该对象被手动删除,要么有人拿走了该对象的所有权并将其删除。

请与我们分享更多代码,logwriter class 的源不应包含非常敏感的数据,它的存在可能有助于实际识别错误。

顺便说一句,请确保 ~B(); 确实有一个实现。

好的,我解决了问题。这与我的代码无关。

问题来自共享库冲突,我猜我使用的框架没有安装好。 我删除它,完全重新安装,一切正常。

问题已解决!