The problem is here:
vertices[i]->~Vertex();
delete vertices[i];
The destructor is called automatically when you delete
it, so essentially you're calling it twice. This is how you find out about that when you don't know it: (relevant lines marked as "HERE:"
)
$ valgrind --tool=memcheck ./program341
==3954== Memcheck, a memory error detector
==3954== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==3954== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==3954== Command: ./program341
==3954==
5
2 3 3 4 6
3 1 2 3 7 5 6
1 4 5
0
1 4 7
2 4
Case 1: Path = 2 1 4; 8 second delay
==3954== Invalid read of size 4
==3954== at 0x8048F00: Vertex::~Vertex() (341.cc:48)
HERE: ==3954== by 0x8049155: Graph::~Graph() (341.cc:103)
==3954== by 0x8048D7C: main (341.cc:254)
==3954== Address 0x4336198 is 0 bytes inside a block of size 8 free'd
==3954== at 0x402AC1D: operator delete(void*) (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==3954== by 0x804B1BA: __gnu_cxx::new_allocator<Arc*>::deallocate(Arc**, unsigned int) (new_allocator.h:100)
==3954== by 0x804A3DA: std::_Vector_base<Arc*, std::allocator<Arc*> >::_M_deallocate(Arc**, unsigned int) (stl_vector.h:175)
==3954== by 0x804A276: std::_Vector_base<Arc*, std::allocator<Arc*> >::~_Vector_base() (stl_vector.h:161)
==3954== by 0x8049779: std::vector<Arc*, std::allocator<Arc*> >::~vector() (stl_vector.h:404)
==3954== by 0x8048F3B: Vertex::~Vertex() (341.cc:45)
HERE: ==3954== by 0x8049135: Graph::~Graph() (341.cc:102)
==3954== by 0x8048D7C: main (341.cc:254)
...
...