Valgrind 泄漏检测 returns 段错误
Valgrind leak detection returns segfault error
我正在 Linux 上使用 Valgrind 检查我的代码是否存在内存泄漏。该程序在前一个小时内运行良好,但 returns 某些有向边组合出现以下错误。我想知道在 dijkstra_sp.cpp 执行之前是否需要检查 NULL。我已确定以下代码中可能是此问题核心的行。
==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)
dijkstra_sp.cpp
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);
}
}
linked_list.h
// item T to the list
template<typename T> List<T> *
List<T>::addToList(T newItem, List<T> *list)
{
// Could sizeof(List<T>) being zero cause this issue? <<<<<<<<<<<<<<<<<<<
List<T> *resultList = (List<T> *)malloc(sizeof(List<T>));
FOLLOWING IS LINE 76 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
resultList->item = newItem;
resultList->next = list;
return resultList;
}
您 运行 内存不足。发生这种情况时调用 malloc
returns NULL (0)。当您尝试写入该无效指针时,您会崩溃。
我正在 Linux 上使用 Valgrind 检查我的代码是否存在内存泄漏。该程序在前一个小时内运行良好,但 returns 某些有向边组合出现以下错误。我想知道在 dijkstra_sp.cpp 执行之前是否需要检查 NULL。我已确定以下代码中可能是此问题核心的行。
==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)
dijkstra_sp.cpp
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);
}
}
linked_list.h
// item T to the list
template<typename T> List<T> *
List<T>::addToList(T newItem, List<T> *list)
{
// Could sizeof(List<T>) being zero cause this issue? <<<<<<<<<<<<<<<<<<<
List<T> *resultList = (List<T> *)malloc(sizeof(List<T>));
FOLLOWING IS LINE 76 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
resultList->item = newItem;
resultList->next = list;
return resultList;
}
您 运行 内存不足。发生这种情况时调用 malloc
returns NULL (0)。当您尝试写入该无效指针时,您会崩溃。