Seg故障,需要帮助读取核心转储

时间:2011-04-17 04:22:50

标签: c++ adt coredump

我有一个错误的分配,我得到一个seg错误,当读取核心转储给出这个:

#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

在gdb中键入bt之后,这就是输出:

#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=..., success=@0xbffff0a3) at sortedListClass.cpp:167
#3  0x0804a1ff in flightmap::GetNextCity (this=0xbffff4dc, index=@0xbffff108, nextCity=@0xbffff104) at flightMap.cpp:197
#4  0x08049ba7 in flightmap::isPath (this=0xbffff4dc, in=@0xbffff164, out=@0xbffff160) 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

2 个答案:

答案 0 :(得分:1)

bt对于回溯是正确的。您可以在sortedListClass::Retrieve行167中看到错误开始。回溯中的下一个条目(返回到#0)是针对operator ==,这意味着第167行上的==是导致崩溃的原因

您发布的代码中有几个==调用,因此如果您可以编辑帖子并指出哪一行是第167行,我们可以进一步诊断。

添加行号后更新:

这只是一个猜测,但你可能忘记在程序早期的某个时刻将ptr成员设置为NULL,以便最终在第167-168行的curr周围指向随机内存位置,导致崩溃。此外,该内存位置的对象也可能已被释放,但ptr尚未更新,并指向现在已删除的对象。仔细检查您的代码,分配新flightRec个对象的位置,并确保始终正确设置ptr

答案 1 :(得分:0)

代码中声明/定义的size在哪里?

看起来你最终会被称为std :: basic_string size方法。