Search code examples
c++pointersnull-pointerdelete-operator

C++ delete operator on pointer, pointer not nulling


I'm trying to implement a directed graph in C++. However, I'm having trouble with my RemoveEdge function, after I call the function and it uses the delete operator on the pointer and set the pointer to nullptr, it is not nulling outside the scope of the function.

I'm not sure if I've stated my problem clearly enough, but maybe some code will help.

Graph.h

template<class TVertex, class TEdge, class TWeight>
class Graph
{
protected:
    std::list<Vertex<TVertex, TEdge, TWeight>*>* _Vertices;
    std::list<Edge<TVertex, TEdge, TWeight>*>* _Edges;
public:
    Graph();
    int TotalVertices();
    int TotalEdges();
    std::list<Vertex<TVertex, TEdge, TWeight>*>* Vertices();
    std::list<Edge<TVertex, TEdge, TWeight>*>* Edges();

    Vertex<TVertex, TEdge, TWeight>* FindVertex(const TVertex&);
    Vertex<TVertex, TEdge, TWeight>* InsertVertex(const TVertex&);
    void RemoveVertex(const TVertex&);

    Edge<TVertex, TEdge, TWeight>* FindEdge(const TEdge&);
    Edge<TVertex, TEdge, TWeight>* InsertEdge(const TVertex&, const TVertex&, const TEdge&, const TWeight&);
    void RemoveEdge(const TEdge&);
};

Graph.FindEdge()

template<class TVertex, class TEdge, class TWeight>
Edge<TVertex, TEdge, TWeight>* Graph<TVertex, TEdge, TWeight>::FindEdge(const TEdge& label)
{
    Edge<TVertex, TEdge, TWeight>* edge = nullptr;
    std::list<Edge<TVertex, TEdge, TWeight>*>::iterator it;

    for(it = this->_Edges->begin(); it != this->_Edges->end(); ++it)
    {
        if(label == (*it)->Label())
        {
            edge = *it;
            break;
        }
    }

    return edge;
}

Graph.RemoveEdge()

template<class TVertex, class TEdge, class TWeight>
void Graph<TVertex, TEdge, TWeight>::RemoveEdge(const TEdge& label)
{
    Edge<TVertex, TEdge, TWeight>* edge = this->FindEdge(label);
    if(edge == nullptr)
        return;

    this->_Edges->remove(edge);
    edge->Source()->RemoveEdge(edge);
    edge->Destination()->RemoveEdge(edge);

            // Problem is here, why isn't this working like I think it should?
    delete edge;
    edge = nullptr;
}

Main.cpp

// created graph
// added vertices
// added edges
Edge<string, string, int>* e5 = graph->InsertEdge("Oshawa", "Toronto", "E5", 5);
graph->RemoveEdge("E5");
cout << ((e5 == nullptr) ? "null" : "not null") << endl; // this outputs not null

So as you can see my program crashes after I remove the edge from the graph, for some reason it is outputting not null after the RemoveEdge function is executed. I'm not sure why this is happening, I've used the delete operator and I've also nulled the pointer explicitly after. What am I doing wrong here?

And yes, I'm sure the edge is being found, the FindEdge function finds the correct edge object and removes it from the appropriate lists but the delete operator is not doing what I want it to do.

Appreciate any help. Thanks in advance.


Solution

  • To solve updating of e5 outside the function you can use shared_ptr and weak_ptr:

    template<class TVertex, class TEdge, class TWeight>
    class Graph
    {
      typedef Vertex<TVertex,TEdge,TWeight> VextexType;
      typedef Edge<TVertex,TEdge,TWeight> EdgeType;
    
      typedef shared_ptr<VertexType> VertexPtr;
      typedef shared_ptr<EdgeType> EdgePtr;
    
    protected:
        std::list< VertexPtr >* _Vertices;
        std::list< EdgePtr >* _Edges;
    public:
        Graph();
        int TotalVertices();
        int TotalEdges();
        std::list<VertexPtr>* Vertices();
        std::list<EdgePtr>* Edges();
    
        VertexPtr FindVertex(const TVertex&);
        VertexPtr InsertVertex(const TVertex&);
        void RemoveVertex(const TVertex&);
    
        EdgePtr FindEdge(const TEdge&)
        {
            for( std::list<EdgePtr>::iterator it = this->_Edges->begin(); it != this->_Edges->end(); ++it)
            {
                if( label == (*it)->Label())
                {
                    return *it;
                }
            }
            return EdgePtr();
        }
    
        EdgePtr InsertEdge(const TVertex&, const TVertex&, const TEdge&, const TWeight&);
        void RemoveEdge(const TEdge& label)
        {
            EdgePtr edge = this->FindEdge(label);
            if(!edge)
               return;
    
           this->_Edges->remove(edge);
           edge->Source()->RemoveEdge( edge.get() );
           edge->Destination()->RemoveEdge( edge.get() );
        }
    };
    

    Now you can write your section in main like this:

    weak_ptr<Edge<string, string, int> > e5( graph->InsertEdge("Oshawa", "Toronto", "E5", 5) );
    graph->RemoveEdge("E5");
    cout << (e5 ? "null" : "not null") << endl;
    

    Note that we use a weak_ptr to store the return value, rather than a shared_ptr.