I have 2 sets of pointers where I want to delete a certain pointer from. But it's giving me a double free error at the last line in the delete edge function.
I initially suspected that maybe it was because set::erase()
was implicitly calling delete
but I read some documents and found out that the destructor won't be called because edge_ins
and edge_outs
are both sets of pointers to Edges.
Now I'm not sure what is causing the double free error at the second call to set::erase()
so I've come here for help.
I have a suspicion that it might be because pointers are treated like objects and when I erase the same pointer from root->edge_outs.erase(e)
it's already gone so it crashes because of the second call at root->edge_ins.erase(e)
. But I was not able to find anything helpful so far.
Edge Deletion:
std::set<Edge*>::iterator
SDG::delete_edge(std::set<Edge*>::iterator e)
{
delete *e;
(*e)->head->edge_ins.erase(e);
return (*e)->root->edge_outs.erase(e);
}
Might be relevant so I will also add how I allocate the memory for the Edge.
Edge creation:
Edge&
SDG::edge(Vertex* out, Vertex* in, Edge::Type type)
{
// 新しいエッジを生成
Edge* edge = new Edge(type, *out, *in);
// 頂点からエッジへの参照
out->add_out(edge);
in->add_in(edge);
return *edge;
}
UPDATE: I have changed the code so that I don't dereference a deleted object but it still gives a double free error.
New code:
SDG::delete_edge(std::set<Edge*>::iterator e)
{
(*e)->head->rm_in(e);
(*e)->root->rm_out(e);
delete *e;
}