Segfault在c ++程序中;不可理解的valgrind输出 [英] Segfault in c++ program; Incomprehensible valgrind output

查看:315
本文介绍了Segfault在c ++程序中;不可理解的valgrind输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在写我的第一个大c ++项目,但在运行程序时遇到分段错误。我已经尝试使用valgrind调试它,但到目前为止没有成功。
由于程序相当大,我将只显示发生错误的相关函数:

  void RigidBody :: RotateAroundAxis(dReal Angle,
dRealVector3 Axis,
dRealVector3 Anchor)
{
std :: cout<<RotateAroundAxis BodyID:< this-> GetBodyId()<< std :: endl;
dRealMatrix3 RotationMatrix = HelperFunctions :: RotateAroundAxis(Angle,Axis);
dRealMatrix3 CurrentRotation = this-> GetRotation();
dRealVector3 Position = this-> GetPosition();
dRealMatrix3 newRotationMatrix = boost :: numeric :: ublas :: prod(RotationMatrix,CurrentRotation);
std :: cout<<RotateAroundAxis(Debug0)BodyID:< this-> GetBodyId()<< std :: endl;
SetRotation(newRotationMatrix);
std :: cout<<RotateAroundAxis Debug<< std :: endl; //这是处理没有错误的最后一行。
std :: cout<<RotateAroundAxis(Debug1)BodyID:< this-> GetBodyId()<< std :: endl; //这行可能会导致错误。
//对象现在旋转,但需要翻译
boost :: numeric :: ublas :: bounded_vector< dReal,3> PosRelToAnchor;
std :: cout<<RotateAroundAxis(Debug2)BodyID:< this-> GetBodyId()<< std :: endl;
for(unsigned int i = 0; i <3; i ++){PosRelToAnchor [i] = Position [i] -Anchor [i];};
PosRelToAnchor = boost :: numeric :: ublas :: prod(RotationMatrix,PosRelToAnchor);
for(unsigned int i = 0; i <3; i ++){Position [i] = PosRelToAnchor(i)+ Anchor [i];};
SetPosition(Position);
}

我把一些消息放到函数中,以跟踪错误发生的地方。在终端中的输出是:

  SetRotation BodyID:0x854de60 
SetPosition0
SetPositionBodyID0x854de60
SetPosition1
SetPositionEnd
SetRotation(Debug1)BodyID:0x854de60
SetRotationEnd
RotateAroundAxis Debug
***程序接收信号SIGSEGV(分段故障)***

对'this-> GetBodyId()'的函数调用在没有任何问题之前执行,然后突然产生segfault我没有线索为什么这样做。
使用valgrind,我得到以下输出:

  == 10910 ==无效读取大小8 
== 10910 == at 0x409AF5:RigidBody :: RotateAroundAxis(double,boost :: array< double,3ul> ;, boost :: array< double,3ul>)(RigidBody.cpp:353)
== 10910 = = by 0x40FFF7:Joints :: Hinge :: Hinge(dxWorld *,std :: string,boost :: array :shared_ptr< RigidBody> double)(Joints.cpp:55)
== 10910 == by 0x410563:BioFlexRotatory :: BioFlexRotatory(dxWorld *,unsigned char,boost :: array< double,3ul& :array< double,3ul> ;, boost :: shared_ptr< RigidBody> ;, boost :: shared_ptr< RigidBody> double,double,double)(BioFlexRotatory.cpp:30)
== 10910 == by 0x40BE9B:Universe :: AddBioFlexRotatory(unsigned char,boost :: array< double,3ul> ;, boost :: array< double,3ul> ;, boost :: shared_ptr< RigidBody> ;, boost :: shared_ptr< RigidBody> double,double,double) Universe.cpp:186)
== 10910 == by 0x406CFE:main(main.cpp:75)
== 10910 ==地址0x0不是stack'd,malloc'd或free'd
== 10910 ==
== 10910 ==
== 10910 ==进程以信号11(SIGSEGV)的默认动作结束:dumping core
== 10910 ==访问不在地址0x0的映射区域内
== 10910 ==在0x409AF5:RigidBody :: RotateAroundAxis(double,boost :: array == 10910 == by 0x40FFF7:Joints :: Hinge :: Hinge(dxWorld *,std :: string,boost :: array< double,3ul>,boost :: array< double,3ul> ;, boost :: shared_ptr< RigidBody>,boost :: shared_ptr< RigidBody> ;, double)(bs.bpp:55)
== 10910 == by 0x410563:BioFlexRotatory :: BioFlexRotatory(dxWorld * unsigned char,boost :: array< double,3ul> ;, boost :: array< double,3ul> ;, boost :: shared_ptr< RigidBody> ;, boost :: shared_ptr< RigidBody> double,double,double)(BioFlexRotatory.cpp: 30)
== 10910 == by 0x40BE9B:Universe :: AddBioFlexRotatory(unsigned char,boost :: array< double,3ul> ;, boost :: array< double,3ul> ;, boost :: shared_ptr< RigidBody& boost :: shared_ptr< RigidBody>,double,double,double)(universe.cpp:186)
== 10910 == by 0x406CFE:main(main.cpp:75)
== 10910 ==如果你相信这是发生堆栈的结果
== 10910 ==溢出在你的程序的主线程(不太可能,但
== 10910 ==可能),你可以尝试增加
== 10910 ==主线程栈使用--main-stacksize =标志。
== 10910 ==运行中使用的主线程堆栈大小为8388608.
== 10910 ==
== 10910 == FILE DESCRIPTORS:3在退出时打开。
== 10910 ==打开文件描述符2:/ dev / pts / 6
== 10910 ==< inherited from parent>
== 10910 ==
== 10910 ==打开文件描述符1:/ dev / pts / 6
== 10910 ==< inherited from parent>
== 10910 ==
== 10910 ==打开文件描述符0:/ dev / pts / 6
== 10910 ==< inherited from parent&
== 10910 ==
== 10910 ==
== 10910 == HEAP SUMMARY:
== 10910 == in in exit:5,521 bytes in 36 blocks
== 10910 ==总堆使用:39分配,3释放,5,803字节分配
== 10910 ==
== 10910 == 1个块中的24个字节在丢失记录中仍可到达1 of 36
== 10910 == at 0x4C286E7:operator new(unsigned long)(vg_replace_malloc.c:287)
== 10910 == by 0x40C990:Universe :: AddRigidBody(std :: string, dMass,boost :: array ,std :: string)(shared_count.hpp: 91)
== 10910 == by 0x4068AE:main(main.cpp:52)
== 10910 ==
== 10910 == 1个块中的24个字节仍然可以丢失record 2 of 36
== 10910 == at 0x4C286E7:operator new(unsigned long)(vg_replace_malloc.c:287)
== 10910 == by 0x40C990:Universe :: AddRigidBody(std :: string ,dMass,boost :: array< double,3ul> ;, boost :: numeric :: ubutas :: c_matrix< double,3ul,3ul,boost :: array< double,3ul> ;, std :: string)(shared_count.hpp :91)
== 10910 == by 0x406A0E:main(main.cpp:56)
== 10910 ==

(这会持续一段时间。 )



然而,这根本不能帮助我,因为我不知道它试图告诉我什么。搜索valgrind无效读取大小没有给我任何提示,从哪里开始。



那么,任何想法如何解决这个错误? >

UPDATE:



在此板入口之前,我试图使用gdb调试错误。由于这是我第一次遇到这样的错误,我更喜欢GUI版本(KDevelop 4)。我设置了一个断点,然后步行槽线。然而,根据调试器的地址,这个指针指向,是恒定的(和正确的)所有的时间。一切看起来不错,直到程序崩溃的行。而崩溃报告没有告诉我什么有价值的(根据我通过this指针调用的函数,它告诉我错误的源是在一个std库,如libstdc ++。so.6,我相信是不可能的。然后我试图valgrind,专门针对这种内存访问错误(根据其他论坛条目)的调试器。



在我阅读答案后,我给另一个机会调试另一个机会,我插入了'std :: cout<<这个指针指向<这种<< std :: endl;'在各个位置。事实证明,指针在调用函数SetRotation后真正指向null。在这个函数内部,我写入一个数组的第12个元素,它有十二个元素(但从零开始,因此结束于元素[11])。大多数时候使用Matlab工作的人的经典错误。我固定这和程序运行没有任何错误。然后我再次保留错误,并再次使用gdb查看。但它仍然说这个指针指向正确的地方。我不知道调试器的问题是什么。无论它不更新或任何,但它不给我任何提示,我做错了。



因此,gdb或gdb的KDevelop GUI都有一个伪装我自己的错误的错误。



非常感谢您帮助我修复错误。

解决方案


无效读取大小为8


表示程序尝试从无效的内存位置读取8个字节


地址0x0不是stack'd,malloc'd或(最近)free'd


表示地址为零 - 因此您尝试读取空指针。


位于0x409AF5:RigidBody :: RotateAroundAxis(double,boost :: array,boost :: array)(RigidBody.cpp:353)


意味着试图在 RigidBody.cpp 的第353行,在 RotateAroundAxis 。我建议你看看那条线;如果没有明显的问题,那么也许你可以在你的代码示例中标记它,所以我们可以看看。它也可能值得在调试器中运行代码;它应该在分段错误处停止,并允许您检查相关变量。


I'm currently writing my first big c++-project but encountered a segmentation fault when running the program. I already tried to debug it using valgrind, but so far without success. Since the program is quite big, I'll show only the relevant function in which the error occured:

void RigidBody::RotateAroundAxis(   dReal Angle,
                                    dRealVector3 Axis,
                                    dRealVector3 Anchor)
{   
    std::cout<<"RotateAroundAxis BodyID: " << this->GetBodyId()<<std::endl;
    dRealMatrix3 RotationMatrix=HelperFunctions::RotateAroundAxis(Angle, Axis);
    dRealMatrix3 CurrentRotation=this->GetRotation();
    dRealVector3 Position=this->GetPosition();
    dRealMatrix3 newRotationMatrix=boost::numeric::ublas::prod(RotationMatrix,CurrentRotation);
    std::cout<<"RotateAroundAxis (Debug0) BodyID: " << this->GetBodyId()<<std::endl;    
    SetRotation(newRotationMatrix);
    std::cout<<"RotateAroundAxis Debug"<<std::endl;// This is the last line that is processed without an error.
    std::cout<<"RotateAroundAxis (Debug1) BodyID: " << this->GetBodyId()<<std::endl; // This line probably causes the error.
    //  object is now rotated but needs to be translated
    boost::numeric::ublas::bounded_vector<dReal,3> PosRelToAnchor;
    std::cout<<"RotateAroundAxis (Debug2) BodyID: " << this->GetBodyId()<<std::endl;
    for(unsigned int i=0;i<3;i++){PosRelToAnchor[i]=Position[i]-Anchor[i];};
    PosRelToAnchor=boost::numeric::ublas::prod(RotationMatrix,PosRelToAnchor);
    for(unsigned int i=0;i<3;i++){Position[i]=PosRelToAnchor(i)+Anchor[i];};
    SetPosition(Position);
}

I put some messages into the function in order to track down where the error occurs. The output in the terminal is:

SetRotation BodyID: 0x854de60
SetPosition0
SetPositionBodyID0x854de60
SetPosition1
SetPositionEnd
SetRotation (Debug1) BodyID: 0x854de60
SetRotationEnd
RotateAroundAxis Debug
*** Program received signal SIGSEGV (Segmentation fault) ***

The function call to 'this->GetBodyId()' is executed before without any problem and then it suddenly produces the segfault and I have no clue why it does so. Using valgrind, I got following output:

==10910== Invalid read of size 8
==10910==    at 0x409AF5: RigidBody::RotateAroundAxis(double, boost::array<double, 3ul>, boost::array<double, 3ul>) (RigidBody.cpp:353)
==10910==    by 0x40FFF7: Joints::Hinge::Hinge(dxWorld*, std::string, boost::array<double, 3ul>, boost::array<double, 3ul>, boost::shared_ptr<RigidBody>, boost::shared_ptr<RigidBody>, double) (Joints.cpp:55)
==10910==    by 0x410563: BioFlexRotatory::BioFlexRotatory(dxWorld*, unsigned char, boost::array<double, 3ul>, boost::array<double, 3ul>, boost::shared_ptr<RigidBody>, boost::shared_ptr<RigidBody>, double, double, double) (BioFlexRotatory.cpp:30)
==10910==    by 0x40BE9B: Universe::AddBioFlexRotatory(unsigned char, boost::array<double, 3ul>, boost::array<double, 3ul>, boost::shared_ptr<RigidBody>, boost::shared_ptr<RigidBody>, double, double, double) (Universe.cpp:186)
==10910==    by 0x406CFE: main (main.cpp:75)
==10910==  Address 0x0 is not stack'd, malloc'd or (recently) free'd
==10910== 
==10910== 
==10910== Process terminating with default action of signal 11 (SIGSEGV): dumping core
==10910==  Access not within mapped region at address 0x0
==10910==    at 0x409AF5: RigidBody::RotateAroundAxis(double, boost::array<double, 3ul>, boost::array<double, 3ul>) (RigidBody.cpp:353)
==10910==    by 0x40FFF7: Joints::Hinge::Hinge(dxWorld*, std::string, boost::array<double, 3ul>, boost::array<double, 3ul>, boost::shared_ptr<RigidBody>, boost::shared_ptr<RigidBody>, double) (Joints.cpp:55)
==10910==    by 0x410563: BioFlexRotatory::BioFlexRotatory(dxWorld*, unsigned char, boost::array<double, 3ul>, boost::array<double, 3ul>, boost::shared_ptr<RigidBody>, boost::shared_ptr<RigidBody>, double, double, double) (BioFlexRotatory.cpp:30)
==10910==    by 0x40BE9B: Universe::AddBioFlexRotatory(unsigned char, boost::array<double, 3ul>, boost::array<double, 3ul>, boost::shared_ptr<RigidBody>, boost::shared_ptr<RigidBody>, double, double, double) (Universe.cpp:186)
==10910==    by 0x406CFE: main (main.cpp:75)
==10910==  If you believe this happened as a result of a stack
==10910==  overflow in your program's main thread (unlikely but
==10910==  possible), you can try to increase the size of the
==10910==  main thread stack using the --main-stacksize= flag.
==10910==  The main thread stack size used in this run was 8388608.
==10910== 
==10910== FILE DESCRIPTORS: 3 open at exit.
==10910== Open file descriptor 2: /dev/pts/6
==10910==    <inherited from parent>
==10910== 
==10910== Open file descriptor 1: /dev/pts/6
==10910==    <inherited from parent>
==10910== 
==10910== Open file descriptor 0: /dev/pts/6
==10910==    <inherited from parent>
==10910== 
==10910== 
==10910== HEAP SUMMARY:
==10910==     in use at exit: 5,521 bytes in 36 blocks
==10910==   total heap usage: 39 allocs, 3 frees, 5,803 bytes allocated
==10910== 
==10910== 24 bytes in 1 blocks are still reachable in loss record 1 of 36
==10910==    at 0x4C286E7: operator new(unsigned long) (vg_replace_malloc.c:287)
==10910==    by 0x40C990: Universe::AddRigidBody(std::string, dMass, boost::array<double, 3ul>, boost::numeric::ublas::c_matrix<double, 3ul, 3ul>, boost::array<double, 3ul>, std::string) (shared_count.hpp:91)
==10910==    by 0x4068AE: main (main.cpp:52)
==10910== 
==10910== 24 bytes in 1 blocks are still reachable in loss record 2 of 36
==10910==    at 0x4C286E7: operator new(unsigned long) (vg_replace_malloc.c:287)
==10910==    by 0x40C990: Universe::AddRigidBody(std::string, dMass, boost::array<double, 3ul>, boost::numeric::ublas::c_matrix<double, 3ul, 3ul>, boost::array<double, 3ul>, std::string) (shared_count.hpp:91)
==10910==    by 0x406A0E: main (main.cpp:56)
==10910== 

(This goes on for quite a while. If it helps, I can post the full output.)

However, this doesn't help me at all because I have no clue what it tries to tell me. Searching for "valgrind Invalid read of size" did not give me any hints on where to start either.

So, any ideas on how to fix this error?

UPDATE:

Prior to this board entry, I tried to debug the error using gdb. Since this was the first time I encountered such an error, I prefered the GUI version (KDevelop 4). I set a breakpoint and then stepped trough the lines. However, according to the debugger the address, the this-pointer pointed to, was constant (and correct) all the time. Everything looked good till the line where the program crashed. And the crash report did not tell me anything valuable (Depending on which function I called through the this-pointer, it showed me that the source of the error was in one of the std libraries such as the "libstdc++.so.6", which I believed was unlikely.). Then I tried valgrind, a debugger that is specialised on this kind of memory access error (according to other forum entries).

After I reading the answers, I gave the noob-way of debugging another chance and I inserted the line 'std::cout<<"The this pointer points to" << this << std:: endl;' at various locations. And as it turns out, the pointer really points to null after calling the function 'SetRotation'. Inside of this function I wrote into the [12]th element of an array that has twelve elements (but starts at zero - therefore end at element [11]). A classical error for someone who works with Matlab most of the time. I fixed this and the program runs without any error. Then I insered the error again and had a second look using gdb. But it still says that the this pointer points to the correct place. I don't know what's the problem withe the debugger. Whether it does not update or whatever, but it does not give me any hints on what I did wrong.

So either the gdb or the KDevelop GUI for gdb has a bug that disguised my own error.

Thank you very much for helping me fix the error.

解决方案

Invalid read of size 8

means that the program tried to read 8 bytes from an invalid memory location.

Address 0x0 is not stack'd, malloc'd or (recently) free'd

means that the address was zero - so you're trying to read through a null pointer.

at 0x409AF5: RigidBody::RotateAroundAxis(double, boost::array, boost::array) (RigidBody.cpp:353)

means that the code that tried to do that was at line 353 of RigidBody.cpp, in a function called RotateAroundAxis. I suggest you look at that line; if there's no obvious problem, then perhaps you could mark it in your code sample so we can take a look. It might also be worth running the code in a debugger; it should stop at the segmentation fault and allow you to inspect the relevant variables.

这篇关于Segfault在c ++程序中;不可理解的valgrind输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆