gpt4 book ai didi

c++ - Valgrind 泄漏检测返回段错误

转载 作者:太空宇宙 更新时间:2023-11-04 12:58:53 24 4
gpt4 key购买 nike

我正在使用 Linux 上的 Valgrind 检查我的代码是否存在内存泄漏。该程序在前一个小时内运行良好,但对于某些有向边的组合返回以下错误。我想知道在 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);
}
}

链表.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 的调用将返回 NULL (0)。当您尝试写入该无效指针时,您会遇到崩溃。

关于c++ - Valgrind 泄漏检测返回段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45179057/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com