2011-04-17 44 views
0

我有一個錯誤與分配,而我得到了賽格故障,在讀核心轉儲時給出了這樣的:賽格故障,需要幫助閱讀核心轉儲

#0 0xb781eb27 in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::size() const() from /usr/lib/libstdc++.so.6 

這是代碼片段我相信它是指。

bool flightmap::GetNextCity(int& index, int& nextCity){ 
    bool success = false; 
    flightRec tmp, tmp2; 
    for (int i = nextCity; (!success) && (i < size); i++) 
    {//if Citytmp goes over size, we never found it 
     tmp.DestinationCity = GetCityName(i); 
     tmp2 = map[index].Retrieve(tmp,success); 
    }//end for loop 
    if (success) 
    { 
     nextCity = GetCityNumber(tmp2.DestinationCity); 
    }//end if 
    return success; 
} 

下面是檢索功能:

flightRec sortedListClass::Retrieve(flightRec& input, bool& success) const{ 
    nodeptr curr; 
    flightRec tmp; 
    curr = head; 
    if (head == NULL) 
    {//If the list is empty 
     std::cout << "List empty, operation not preformed" << std::endl; 
     success = false; 
    } 
    /*LINE 167*/ while ((curr!=NULL)&&(!(curr->DestinationCity==input.DestinationCity))) //<- THIS IS LINE 167 
    {//Here we first check if curr points to NULL, then we see if DesinationCity and Origin are not yet found. 
     curr=curr->ptr; 
    } 
    if ((curr->DestinationCity==input.DestinationCity)) 
    {//If we found it, then let's return it... 
     tmp.DestinationCity=curr->DestinationCity; 
     //tmp.Origin=curr->Origin; 
     tmp.flightnumber=curr->flightnumber; 
     tmp.cost=curr->cost; 
     success = true; 
     return tmp; 
    } 
    else //We didn't and then...damn. 
    { 
     //std::cout << "Can't find flight to " << input.DestinationCity << std::endl; 
     success = false; 
    } 
} 

flightRec:

struct flightRec{ 
    std::string Origin; 
    int flightnumber; 
    float cost; 
    std::string DestinationCity; 
    bool operator <(const flightRec& rhs) const; 
    bool operator ==(const flightRec& rhs) const; 
    flightRec* ptr; 
}; 
typedef flightRec* nodeptr; 

flightMap.h

#ifndef FLIGHTMAP_H 
#define FLIGHTMAP_H 
#include "sortedListClass.h" 
#include "stackClass.h" 
#include <fstream> 
#include <cstdio> 

class flightmap { 
public: 
    flightmap(); 
    flightmap(const flightmap& orig); 
    virtual ~flightmap(); 
    void readcities (std::ifstream& in); 
    void readflights (std::ifstream& in); 
    void display(); 
    void isPath (int& in, int& out); 
    void MarkVisited (int& index); 
    bool IsVisited (int& index); 
    void UnvisitAll(); 
    bool GetNextCity (int& index, int& nextCity); 
    int GetCityNumber(std::string& city); 
    std::string GetCityName(int& index); 
private: 
    int size; 
    sortedListClass* map; 
    std::string* origin; 
    bool* visited; 
}; 

#endif 

打字BT到GDB在這之後是輸出是什麼:

#0 0xb7f4eb27 in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::size() const() from /usr/lib/libstdc++.so.6 
#1 0x0804a407 in std::operator==<char> (__lhs=..., __rhs=...) at /usr/lib/gcc/i686-pc-linux-gnu/4.5.2/../../../../include/c++/4.5.2/bits/basic_string.h:2345 
#2 0x0804ab32 in sortedListClass::Retrieve (this=0x8053468, input=..., [email protected]) at sortedListClass.cpp:167 
#3 0x0804a1ff in flightmap::GetNextCity (this=0xbffff4dc, [email protected], [email protected]) at flightMap.cpp:197 
#4 0x08049ba7 in flightmap::isPath (this=0xbffff4dc, [email protected], [email protected]) at flightMap.cpp:110 
#5 0x08049314 in FindPath (datafile=..., map=...) at ola6.cpp:51 
#6 0x08049190 in main (argc=2, argv=0xbffff5b4) at ola6.cpp:35 
+0

您必須發佈完整的回溯。 #0只會告訴你問題被觸發的功能,但它不會告訴你它是如何到達那裏的。你需要關注它,直到你到達源代碼文件中的一行,這會告訴你代碼中的哪一行會導致問題。我不認爲這可能是您發佈的代碼段。 – Malvineous 2011-04-17 04:49:30

+0

我不知道如何在gdb中做到這一點。 – Athetius 2011-04-17 04:50:40

回答

1

bt對於回溯是正確的。您可以看到錯誤在sortedListClass::Retrieve行167開始。回溯中的下一個條目(回到#0)適用於operator ==,這意味着167行上的==是導致崩潰的原因。

您發佈的代碼中有幾個==調用,所以如果您可以編輯您的帖子並指出哪一行是167行,我們可以進一步診斷。

更新添加行號後面:

這只是一個猜測,但很有可能你已忘了給ptr成員在早些時候在節目中的一些點設置爲NULL,這樣在周圍線167環-168 curr最終指向隨機存儲位置,導致崩潰。也有可能已經釋放了該內存位置的對象,但ptr尚未更新,並且正指向現在已刪除的對象。在您分配新物體flightRec的地方密切檢查您的代碼,並確保始終正確設置ptr

+0

編輯但這裏是行if((curr!= NULL)&&(curr-> DestinationCity == input.DestinationCity)) – Athetius 2011-04-17 05:04:01

+0

謝謝,我會進一步調查。 – Athetius 2011-04-17 05:18:39

0

sizesize您的代碼在哪裏申明/定義?

它看起來像你不知怎的結束了被稱爲std :: basic_string size方法。

+0

尺寸在私有數據部分 – Athetius 2011-04-17 04:31:11

+0

下的flightmap.h中聲明爲int,請發佈flightmap.h。另外,如果'Retrieve'中的'head'爲NULL,您將會遇到問題,因爲您將其分配給'curr',然後訪問'curr-> DestinationCity',無論您是否檢查null失敗。如果您發佈了整個堆棧跟蹤,那麼也會有所幫助。 – 2011-04-17 04:40:56

+0

我不確定如何在gdb中檢索堆棧。我想我明白了。 Nope – Athetius 2011-04-17 04:47:05