简体   繁体   English

尝试解决细分错误

[英]Trying to resolve a segmentation fault

I keep on getting a segmentation fault in my C++ code which I have traced with GDB through the thread of execution and do not see where the issue is coming from. 我一直在我的C​​ ++代码中遇到分段错误,该错误是通过执行线程与GDB一起跟踪的,看不到问题的根源。 My program creates a Graph object from a list of Vertex objects in the primary class AsTheCrowFlies. 我的程序从主类AsTheCrowFlies中的Vertex对象列表创建Graph对象。 The actual creation of the Graph object happens in the makeGraph(const std::vector). Graph对象的实际创建在makeGraph(const std :: vector)中进行。 The line in makeGraph where the problem starts is where I call another function checkEdges(Map map, const Vertex &To, const &From) which looks like this: makeGraph中问题开始的那一行是我调用另一个函数checkEdges(Map map,const Vertex&To,const&From)的样子:

  bool Graph::checkEdges(Map &map, const Vertex &To, const Vertex &From) {

      if(map.find(To)!=map.end()) {

        std::vector<Edge> edges = map[To];

        for (auto itr = edges.begin(); itr!=edges.end(); ++itr) {

               if(itr->getDestination()==From)
                  return true;

          }

     }

   return false;

 }

Based on how the output from GDB it looks like when the if statement in the for loop executes and tries to use the overloaded == function in the Vertex class: 基于GDB的输出结果,当for循环中的if语句执行并尝试使用Vertex类中的重载==函数时,它看起来像:

     bool Vertex::operator == (const Vertex &other) const {

        if (name==other.name && latitude==other.latitude && 
         longitude == other.longitude) {

            return true;

         }

    return false; 

   }

it gives the error message: " Cannot access memory at address 0x4a8>" The full list of the error messages when I use the bt command in GDB is: 它给出了错误消息:“无法访问地址0x4a8>的内存”当我在GDB中使用bt命令时,错误消息的完整列表为:

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7b8f6f3 in std::string::size() const ()
from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
(gdb) bt
#0  0x00007ffff7b8f6f3 in std::string::size() const ()
from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#1  0x000000000040a729 in std::operator==<char> (__lhs=" ",
__rhs=<error reading variable: Cannot access memory at address 0x4a8>)
at /usr/include/c++/4.8/bits/basic_string.h:2495

#2  0x000000000041a30d in Vertex::operator== (this=0x671b58, other=...)
at Vertex.cpp:50

#3  0x0000000000415fb7 in Graph::checkEdges (this=0x7fffffffe880,
map=std::unordered_map with 50 elements = {...}, To=..., From=...)
at Graph.cpp:364

#4  0x00000000004157cf in Graph::makeGraph (this=0x7fffffffe880,
input=std::vector of length 50, capacity 50 = {...}) at Graph.cpp:161

#5  0x0000000000414bea in Graph::setVertices (this=0x7fffffffe880,
v=std::vector of length 50, capacity 64 = {...}) at Graph.cpp:38

#6  0x0000000000402b62 in AsTheCrowFlies::menu (this=0x7fffffffe800,
filename=0x7fffffffec48 "cap_cities.txt") at AsTheCrowFlies.cpp:17

#7  0x000000000041a05c in main (argc=2, argv=0x7fffffffe9c8) at main.cpp:24

Here is the full version of makeGraph(const Vertex input): 这是makeGraph(const Vertex input)的完整版本:

 Map Graph::makeGraph(std::vector<Vertex> input) {

   Map new_graph;

   size_t size, n, mid,index;

   size  = input.size();

   n = size - 1;

   mid = ((size + size%2)/2)-1;

   if (size<1)
return new_graph;


double mean,max,min,median; //double variables for mean, median, maximum, minimum, sum of squares and standard deviation created

    //vector used to hold the distances between adjacent vertices
std::vector<double> dist(size); 


    for (int i = 0; i < size; i++) {

        mean=0.0; //set average/mean to "0"


        if(new_graph.find(input[i])==new_graph.end()) { //if the current Vertex has no map entry, add it here

      std::vector<Edge> edges(size);            
      new_graph[input[i]] = edges;         

        }

       //Create and sort duplicate list with each Vertex moved to 
         the front in turn         

       std:vector<Vertex> list(size);

       list = input;

       sort(list.begin(),list.end(),[&](const Vertex &v1, const Vertex &v2){return findMinDist(input[i].getLatitude(),input[i].getLongitude(),v1.getLatitude(),v1.getLongitude()) < findMinDist(input[i].getLatitude(),input[i].getLongitude(),v2.getLatitude(),v2.getLongitude()) ;}); 


       //Gets mean and fills distance tracking vector


       for (int j = 0; j < size; j++) {

        dist[j]=findMinDist(input[i].getLatitude(),input[i].getLongitude(),input[j].getLatitude(),input[j].getLongitude());     
        mean+=dist[j]/size;//accumulates mean value

            }        


      min = dist[1];



      max = dist[n];


  if(size%2==0) { //calculates median distance value for...

    median = (dist[mid]+dist[mid+1])/2; //...even sized vectors and...

      } else {median = dist[mid];} //...odd sized vectors


     if(mean >= median) { //index value is calucated for...

    index = (mid+1)*(mean-min)/(max-min); //...case where mean is greater than or equal to median and...


      } else {index = (mid+1)*(median-min)/(max-min);}  //...case where mean is less than median and...


  for (int k = 0; k < index; k++) { //

         if(!checkEdges(new_graph,list[0],list[k])){

    new_graph[list[0]].push_back(Edge(list[0],list[k],dist[k]));

         }


         if(k!=0) {

             if(new_graph.find(list[k])==new_graph.end()) {//SEGMENTATION
                                                            //FAULT STARTS 
                                                            //HERE

        std::vector<Edge> edges(size);

                    new_graph[list[k]]=edges;

                    new_graph[list[k]].push_back(Edge(list[k],list[0],dist[k]));

            } else {

        if(!checkEdges(new_graph,list[k],list[0])) {

        new_graph[list[k]].push_back(Edge(list[k],list[0],dist[k]));    

                   }

               }

        }

       }


     dist.clear(); 

   }

return new_graph;       

} }

And the signature of getDestination is 而getDestination的签名是

  Vertex& Edge::getDestination() {return destination;}

I looking at this and not seeing why there would be a segmentation fault here. 我正在查看此内容,但没有看到为什么这里会有分段错误。 Please help! 请帮忙! I have included the pertinent sections of code where the error seems to be happening at but if you need to some more code to help understand what is going on, please let me know. 我已经包含了似乎发生错误的代码的相关部分,但是如果您需要更多代码来帮助您了解发生了什么,请告诉我。

The most likely thing is that getDestination() returns a pointer to an invalid/invalidated object or even null . 最可能的是getDestination()返回一个指向无效/无效对象甚至null的指针。

Anyway, I'd use the debugger. 无论如何,我会使用调试器。 And I would slightly adapt the code such that debugging and inspecting the objects becomes easier. 而且我会稍微修改一下代码,以使调试和检查对象变得更加容易。 Good luck! 祝好运!

       Vertex &edgeFrom = itr->getDestination();
       if(edgeFrom==From)
          return true;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM