#include "Graph.h"
Graph::Graph()
: base_state(2)
{
//ctor
}
Graph::~Graph()
{
for(auto p = graph_map.begin(); p != graph_map.end(); p = graph_map.begin())
delete_vertex(p->first);
for(auto p = vertex_holder.begin(); p != vertex_holder.end(); p = vertex_holder.begin())
delete_vertex(*p); //When this function is called, it causes a segmentation fault- deleting the other vertex works fine
}
Graph::Graph(const Graph& other)
: base_state(other.get_base_state())
{
//copy ctor
}
Graph& Graph::operator=(const Graph& rhs)
{
if (this == &rhs) return *this; // handle self assignment
//assignment operator
return *this;
}
Vertex* Graph::create_vertex()
{
Vertex * vert = new Vertex;
vertex_holder.emplace(vert);
auto iter = vertex_holder.find(vert);
return *iter;
}
void Graph::create_connector(Vertex* vert1, Vertex* vert2)
{
Connector * connect = new Connector(vert1,vert2);
graph_map.emplace(vert1, connect);
graph_map.emplace(vert2, connect);
connect->update_pointers(graph_map, vert1, vert2);
if(vertex_holder.count(vert1))
vertex_holder.erase(vert1);
if(vertex_holder.count(vert2))
vertex_holder.erase(vert2);
}
void Graph::delete_connector(Connector* connect)
{
if((connect->vertex_pair.first == nullptr)||(connect->vertex_pair.second == nullptr))
return;
auto * a = connect->vertex_pair.first;
auto * b = connect->vertex_pair.second;
auto p = graph_map.equal_range(a);
for(auto iter = p.first; iter != p.second; iter++)
if(iter->second == connect)
{
graph_map.erase(iter);
break;
}
p = graph_map.equal_range(b);
for(auto iter = p.first; iter != p.second; iter++)
if(iter->second == connect)
{
graph_map.erase(iter);
break;
}
if (graph_map.find(a) == graph_map.end())
vertex_holder.emplace(a);
if (graph_map.find(b) == graph_map.end())
vertex_holder.emplace(b);
delete connect;
}
void Graph::delete_vertex(Vertex* vert)
{
auto test1 = graph_map.find(vert);
if(test1 == graph_map.end())
{
auto test2 = vertex_holder.find(vert);
if (test2 == vertex_holder.end())
return;
vertex_holder.erase(test2);
delete vert; //Segmentation Fault!
return;
}
auto p = graph_map.equal_range(vert);
for(auto iter = p.first; iter != p.second;iter = p.first)
{
delete_connector(iter->second);
p = graph_map.equal_range(vert);
}
delete vert;
}
void Graph::coboundary()
{
for(auto p : graph_map)
p.second->coboundary(base_state);
for(auto p : graph_map)
p.first->set_state(0);
}
void Graph::boundary()
{
for(auto p : graph_map)
p.first->boundary(base_state);
for(auto p : graph_map)
p.second->set_state(0);
}
Vertex* Graph::find_vertex(std::pair<int,int> coordinates)
{
for(auto p : graph_map)
if(p.first->get_coordinates() == coordinates)
return p.first;
elsefor(auto p : vertex_holder)
if(p->get_coordinates() == coordinates)
return p;
returnnullptr;
}
Now, the issue that I've tracked down using the debugger is that a segfault is called within the hashtable for the unordered_set for connection_set, if that helps.
Given that you've got several files it would be nice if you could provide an easy method for us to copy your setup. Like a zip or a github project.
> Now, the issue that I've tracked down using the debugger is that a segfault
> is called within the hashtable for the unordered_set for connection_set, if that helps.
I think that a backtrace would be more helpful.
@norm b: the destructor of a raw pointer would not perform a delete on it.
Ignore them- they're really me just blabbering nonsense to myself. Anyway, the download isn't identical to the code here- I added a bit more to main, and removed the comments where the segmentation fault is. However, that's the only difference.