Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/151.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ c+中的Segfault+;程序;不可理解的valgrind输出_C++_Gdb_Segmentation Fault_Valgrind_Kdevelop - Fatal编程技术网

C++ c+中的Segfault+;程序;不可理解的valgrind输出

C++ c+中的Segfault+;程序;不可理解的valgrind输出,c++,gdb,segmentation-fault,valgrind,kdevelop,C++,Gdb,Segmentation Fault,Valgrind,Kdevelop,我目前正在编写我的第一个大型c++项目,但在运行程序时遇到了一个分段错误。我已经尝试过使用valgrind调试它,但到目前为止没有成功。 由于程序相当大,我将仅显示发生错误的相关函数: void RigidBody::RotateAroundAxis( dReal Angle, dRealVector3 Axis, dRealVector3 Anch

我目前正在编写我的第一个大型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;// 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);
}
对'this->GetBodyId()'的函数调用以前执行过,没有任何问题,然后它突然产生segfault,我不知道它为什么会这样做。 使用valgrind,我得到了以下输出:

==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== 
==10910==大小为8的无效读取
==10910==0x409AF5:RigidBody::RotateAroundAxis(双精度,boost::array,boost::array)(RigidBody.cpp:353)
==10910==by 0x40FFF7:Joints::铰链::铰链(dxWorld*,std::string,boost::array,boost::shared_ptr,boost::shared_ptr,double)(Joints.cpp:55)
==10910==by 0x410563:BioFlexRotatory::BioFlexRotatory(dxWorld*,无符号字符,boost::array,boost::array,boost::shared\u ptr,boost::shared\u ptr,double,double,double)(BioFlexRotatory.cpp:30)
==10910==by 0x40BE9B:Universe::AddBioFlexRotatory(无符号字符,boost::数组,boost::数组,boost::shared_ptr,boost::shared_ptr,double,double,double)(Universe.cpp:186)
==10910==0x406CFE:main(main.cpp:75)
==10910==地址0x0不是堆栈、malloc或(最近)空闲
==10910== 
==10910== 
==10910==进程终止,信号11(SIGSEGV)的默认动作:转储堆芯
==10910==访问不在地址0x0的映射区域内
==10910==0x409AF5:RigidBody::RotateAroundAxis(双精度,boost::array,boost::array)(RigidBody.cpp:353)
==10910==by 0x40FFF7:Joints::铰链::铰链(dxWorld*,std::string,boost::array,boost::shared_ptr,boost::shared_ptr,double)(Joints.cpp:55)
==10910==by 0x410563:BioFlexRotatory::BioFlexRotatory(dxWorld*,无符号字符,boost::array,boost::array,boost::shared\u ptr,boost::shared\u ptr,double,double,double)(BioFlexRotatory.cpp:30)
==10910==by 0x40BE9B:Universe::AddBioFlexRotatory(无符号字符,boost::数组,boost::数组,boost::shared_ptr,boost::shared_ptr,double,double,double)(Universe.cpp:186)
==10910==0x406CFE:main(main.cpp:75)
==10910==如果您认为这是由于堆栈导致的
==10910==程序主线程溢出(不太可能,但
==10910==可能),您可以尝试增大
==10910==使用--main stacksize=标志的主线程堆栈。
==10910==此运行中使用的主线程堆栈大小为8388608。
==10910== 
==10910==文件描述符:3在退出时打开。
==10910==打开文件描述符2:/dev/pts/6
==10910==    
==10910== 
==10910==打开文件描述符1:/dev/pts/6
==10910==    
==10910== 
==10910==打开文件描述符0:/dev/pts/6
==10910==    
==10910== 
==10910== 
==10910==堆摘要:
==10910==在出口处使用:36个块中的5521字节
==10910==总堆使用率:39个allocs,3个free,分配了5803个字节
==10910== 
==10910==1个块中的24个字节仍然可以在36个块中的丢失记录1中访问
==10910==at 0x4C286E7:运算符新(无符号长)(vg_替换_malloc.c:287)
==10910==by 0x40C990:Universe::AddRigidBody(std::string,dMass,boost::array,boost::numeric::ublas::c_matrix,boost::array,std::string)(共享计数。hpp:91)
==10910==0x4068AE:main(main.cpp:52)
==10910== 
==10910==1个块中的24个字节仍然可以在36个块中的丢失记录2中访问
==10910==at 0x4C286E7:运算符新(无符号长)(vg_替换_malloc.c:287)
==10910==by 0x40C990:Universe::AddRigidBody(std::string,dMass,boost::array,boost::numeric::ublas::c_matrix,boost::array,std::string)(共享计数。hpp:91)
==10910==0x406A0E:main(main.cpp:56)
==10910== 
(这种情况持续了很长一段时间。如果有帮助,我可以发布完整的输出。)

然而,这对我毫无帮助,因为我不知道它想告诉我什么。搜索“valgrind无效读取大小”也没有给我任何关于从何处开始的提示

那么,关于如何修复这个错误有什么想法吗

更新:

在此之前,我尝试使用gdb调试错误。因为这是我第一次遇到这样的错误,所以我更喜欢GUI版本(KDevelop4)。我设置了一个断点,然后穿过这些线。但是,根据调试器,该指针指向的地址始终是常量(并且是正确的)。在节目崩溃之前,一切看起来都很好。崩溃报告没有告诉我任何有价值的东西(取决于我通过this指针调用的函数,它告诉我错误的来源是在一个std库中,比如“libstdc++.so.6”,我认为这不太可能)。然后我尝试了valgrind,一个专门处理这种内存访问错误的调试器(根据其他论坛条目)

在我阅读了答案之后,我给了noob调试的另一个机会,我插入了行'std::cout' 大小为8的读取无效

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

地址0x0不是堆栈、malloc或(最近)空闲

意味着地址为零-因此您试图读取空指针

在0x409AF5:RigidBody::RotateAroundAxis(双精度,boost::array,boost::array)(RigidBody.cpp:353)


表示尝试执行此操作的代码位于名为
RotateAroundAxis
的函数中的
RigidBody.cpp
的第353行。我建议你看看那条线;如果没有明显的问题,那么您可以在代码示例中标记它,以便我们可以查看。也可能值得在调试器中运行代码;它应该在分段错误处停止,并允许您检查相关变量。

在我看来,您正在通过设置为
0
RigidBody*
调用
RigidBody
函数

或者,您在
SetRotation
中有一个bug
==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==