I have created a graph datastructure using linked lists. With this code:
typedef struct vertexNode *vertexPointer; typedef struct edgeNode *edgePointer; void freeGraph(vertexPointer); /* announce function */ struct edgeNode{ vertexPointer connectsTo; edgePointer next; }; struct vertexNode{ int vertex; edgePointer next; }; Then I create a graph in which I have 4 nodes, lets say A, B, C and D, where: A connects to D via B and A connects to D via C. With linked lists I imagine it looks like this:

Finally, I try to free the graph with freeGraph(graph).
void freeEdge(edgePointer e){ if (e != NULL) { freeEdge(e->next); freeGraph(e->connectsTo); free(e); e = NULL; } } void freeGraph(vertexPointer v){ if (v != NULL) { freeEdge(v->next); free(v); v = NULL; } } That's where valgrind starts complaining with "Invalid read of size 4", "Address 0x41fb0d4 is 4 bytes inside a block of size 8 free'd" and "Invalid free()". Also it says it did 8 mallocs and 9 frees.
I think the problem is that the memory for node D is already freed and then I'm trying to free it again. But I don't see any way to do this right without having to alter the datastructure.
What is the best way to prevent these errors and free the graph correctly, if possible without having to alter the datastructure? Also if there are any other problems with this code, please tell. Thanks!
greets, semicolon