gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 05:43:05 24 4
gpt4 key购买 nike

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

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

这是我的 B 类:

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

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

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

  • 我实例化A的时候没有问题
  • 当我实例化 B 时,我可以“玩”它(访问它的成员等),但是当程序完成时,我遇到了段错误。

只有当我有 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”类:

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;

}

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

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

为了进行转换,我有一个工作正常的转换模块。我想补充一点:这是我的主要代码,它给我一个段错误:

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

最佳答案

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

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

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

关于c++ - 使用 Valgrind 调试程序 - 检测段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36737694/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com