Valgrind泄漏检测返回段错误

时间:2017-07-19 00:19:10

标签: c++ memory-leaks valgrind

我在Linux上使用Valgrind检查我的代码是否存在内存泄漏。该程序在前一个小时运行正常,但对于某些有向边的组合返回以下错误。我想知道在dijkstra_sp.cpp执行之前是否需要检查NULL。我已经在以下代码中找到了可能是此问题中心的行。

root
 |-- id: integer (nullable = true)
 |-- lang: string (nullable = true)
 |-- type: string (nullable = true)

dijkstra_sp.cpp

==25051== Process terminating with default action of signal 11 (SIGSEGV)
==25051==  Access not within mapped region at address 0x0
==25051==    at 0x410D79: List<DirectedEdge*>::addToList(DirectedEdge*, List<DirectedEdge*>*) (linked_list.h:76)
==25051==    by 0x410AD5: pathTo(DijkstraSPTree*, ShortestPath*, int) (dijkstra_sp.cpp:77)
==25051==    by 0x423C54: getShortestPath(EdgeWeightedDigraph*, int, int) (vehicle_searching.cpp:45)
==25051==    by 0x4187E5: netPathWeight(EdgeWeightedDigraph*, int, int, int) (vehicle_Linux.cpp:1099)
==25051==    by 0x41B8E0: Schedule(int, int, VehicleState*) (vehicle_Linux.cpp:781)
==25051==    by 0x415719: updateAndRender(VehicleState*, int) (vehicle_Linux.cpp:237)

linked_list.h

struct DirectedEdge {
  int32 from;
  int32 to;
  real32 weight;
};

void
pathTo(DijkstraSPTree *spTree, ShortestPath *shortestPath, int32 dest)
{
  // should I assert input not null? <<<<<<<<<<<<<<<<<<<<<<<<<<<
  List<DirectedEdge *>::traverseList(freeDirectedEdge, shortestPath->edgeList);
  List<DirectedEdge *>::emptyList(&shortestPath->edgeList);
  shortestPath->totalWeight = spTree->distTo[dest];

  // check if there IS a path to dest from the root of spTree
  if (spTree->distTo[dest] < INFINITY) {
        DirectedEdge *nextEdge = spTree->edgeTo[dest];
        if(nextEdge != 0)
        nextEdge = spTree->edgeTo[nextEdge->from];
    for (DirectedEdge *nextEdge = spTree->edgeTo[dest];
        nextEdge != 0;
        nextEdge = spTree->edgeTo[nextEdge->from]) {
// FOLLOWING IS LINE 77 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
      shortestPath->edgeList =
        List<DirectedEdge *>::addToList(nextEdge, shortestPath->edgeList);
    }
  }

1 个答案:

答案 0 :(得分:0)

你的内存耗尽了。当发生这种情况时,对malloc的调用返回NULL(0)。当您尝试写入该无效指针时,您将崩溃。