C++图形函数内存泄漏(valgrind在线上没有具体说明)
Memory Leak In C++ Graph Function (valgrind not specific on line)
我正在用C++中的边列表实现一个Graph。代码按预期执行,但根据valgrind的说法,AddEdge函数导致了内存泄漏。我很难弄清楚它是什么,因为AddEdge和AddVertex很相似,但AddVertex没有泄漏。
void Graph::AddVertex(string v)
{
bool full = false;
try
{
VertexNode* location = new VertexNode;
delete location;
full = false;
}
catch(std::bad_alloc exception)
{
full = true;
}
if(full == true)
throw GraphFull();
else
{
VertexNode* temp = new VertexNode;
temp->vname = v;
if (vertices == NULL)
{
vertices = temp;
vertices->nextVertex = NULL;
edges = NULL;
}
else
{
temp->nextVertex = vertices;
vertices = temp;
}
}
}
void Graph::AddEdge(string s, string d, int w)
{
bool full = false;
try
{
EdgeNode* location = new EdgeNode;
delete location;
full = false;
}
catch(std::bad_alloc exception)
{
full = true;
}
if(full == true)
throw GraphFull();
else
{
EdgeNode* temp = new EdgeNode;
temp->weight = w;
VertexNode* search = vertices;
while(search->vname != s)
search = search->nextVertex;
temp->source = search;
search = vertices;
while(search->vname != d)
search = search->nextVertex;
temp->destination = search;
if (edges == NULL)
{
edges = temp;
edges->nextEdge = NULL;
}
else
{
temp->nextEdge = edges;
edges = temp;
}
}
}
这是valgrind的输出:
==17435== at 0x4A075BC: operator new(unsigned long) (vg_replace_malloc.c:298)
==17435== by 0x401952: Graph::AddEdge(std::string,std::string,int)(in /home/graph)
==17435== by 0x402255: main (in /home/graph)
AddEdge和AddVertex都没有释放它们分配的任何内存,因此从设计上讲,问题根本不在这些例程中。很明显,内存泄漏是指在程序关闭之前未能释放内存。仅据报告,当程序退出时,特定例程分配的内存仍保持分配状态。
问题可能不在AddEdge
中分配时,而是在程序退出时,AddEdge
分配的内存没有释放(释放)。
是否释放了顶点而没有释放边?
相关文章:
- 在线编译器中的分段C++没有打印消息
- 当使用通配符和null指针调用函数时,对输出的说明
- 关于C++中具有多重继承"this"指针的说明
- 16 位到 10 位转换代码说明
- 在线程之间拆分任务总是值得的吗?
- join() 失败,如果在线程内部调用 io_context.run()
- C++ 基本 CTOR 说明 - 为什么不调用赋值/复制构造函数
- 编译器指出结构和 int 说明的错误 2059
- 如何在C++中在线组装?
- 随机数生成函数说明
- 在线程中读取无符号整数时,c++ 位是否以原子方式切换?
- 赋值到类型和空大括号. 语法说明
- C++ switch 语句:对案例进行分组,并针对每个案例给出特定的说明
- 标准对此指向成员函数类型模板参数有何说明?是我的代码有误,还是 MSVS 16.6 有问题?
- std::extent 实现详细信息说明
- 在线程函数中处理数据向量时进行线程竞速
- 我应该在线程上调用 processEvents() 吗?
- 如何创建始终在线的过程?
- pthread只有在线程数量较少时才可以正常工作
- C++图形函数内存泄漏(valgrind在线上没有具体说明)