簡體   English   中英

c ++程序中的段錯誤; 無法理解的valgrind輸出

[英]Segfault in c++ program; Incomprehensible valgrind output

我目前正在編寫我的第一個大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);
}

我在函數中放入了一些消息,以跟蹤錯誤發生的位置。 終端中的輸出為:

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

之前沒有任何問題地執行過對“ this-> GetBodyId()”的函數調用,然后突然產生了段錯誤,我不知道為什么這樣做。 使用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== 

(這持續了一段時間。如果有幫助,我可以發布完整的輸出。)

但是,這完全沒有幫助,因為我不知道它試圖告訴我什么。 搜索“ valgrind無效的大小讀取”並沒有給我任何從何處開始的提示。

那么,關於如何解決此錯誤的任何想法?

更新:

在進入該板之前,我嘗試使用gdb調試錯誤。 因為這是我第一次遇到這樣的錯誤,所以我更喜歡GUI版本(KDevelop 4)。 我設置一個斷點,然后跨過線路。 但是,根據調試器,此指針所指向的地址一直都是恆定的(而且是正確的)。 一切看起來都很不錯,直到程序崩潰為止。 而且崩潰報告並沒有告訴我任何有價值的信息(根據我通過此指針調用的函數的不同,它向我表明錯誤的出處在其中一個標准庫中,例如“ libstdc ++。so.6”,我認為這不太可能。)。 然后,我嘗試了valgrind,這是專門針對這種內存訪問錯誤的調試器(根據其他論壇條目)。

閱讀完答案后,我給了調試新手的機會,然后插入了行'std :: cout <<“ this指針指向” << << this << std :: endl;' 在各個位置。 事實證明,在調用函數“ SetRotation”之后,指針實際上指向空。 在此函數內部,我寫入了具有十二個元素的數組的第[12]個元素(但從零開始-因此在元素[11]結束)。 對於大多數時間與Matlab合作的人來說,這是一個經典錯誤。 我已解決此問題,程序運行無任何錯誤。 然后我再次插入錯誤,並使用gdb進行了第二次查看。 但是它仍然說this指針指向正確的位置。 我不知道調試器有什么問題。 它不會更新還是執行任何操作,但是它不會給我任何提示我做錯了什么。

因此,無論gdb還是gdb的KDevelop GUI都有一個錯誤掩蓋了我自己的錯誤。

非常感謝您幫助我修復錯誤。

大小為8的讀取無效

表示程序試圖從無效的內存位置讀取8個字節。

地址0x0未堆棧,未分配或(最近)釋放

表示地址為零-因此,您嘗試讀取空指針。

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

表示嘗試執行此操作的代碼位於RigidBody.cpp 353行中,該函數稱為RotateAroundAxis 我建議你看看那條線。 如果沒有明顯的問題,那么也許您可以在代碼示例中對其進行標記,以便我們來看一下。 也可能值得在調試器中運行代碼。 它應該在細分錯誤時停止,並允許您檢查相關變量。

在我看來,您正在通過設置為0RigidBody*調用RigidBody函數。

或者,您在SetRotation中有一個錯誤,使您的內存無法識別。

您需要使用調試器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM