认识指针分配当我尝试调用指针上删除对一个struct Vertex
(与Vertex * v = new Vertex
分配,然后成功地使用并存储在std::list
在我的类的析构,我得到这个运行时错误:删除不从列表
graphtake3(12325,0x100082000) malloc: *** error for object 0x100200340: pointer being freed was not allocated
***
指针肯定被分配,因为应用程序运行良好,并且所有内容都显示为它应该在堆栈跟踪中,但由于某种原因,delete
似乎无法解除其分配。这里发生了什么,以及为什么不删除工作?
这里是相关的缩写代码:
#include <vector>
#include <list>
#include <iostream>
#include <string>
enum Color {BLACK, GREY, WHITE};
struct Vertex {
int id;
std::string name;
Color color;
Vertex();
Vertex(std::string name);
~Vertex();
};
class Graph {
std::vector<std::list<Vertex *>> adjList;
public:
Graph();
Graph (int nodeCount);
~Graph();
int newVertex();
int newVertex(std::string name);
void newUnDirectedEdge(int v1, int v2);
void newDirectedEdge(int v1, int v2);
std::list<Vertex*> getConnections(int v);
friend std::ostream& operator<<(std::ostream& os, const Graph& g);
};
和
#include "Graph.hpp"
Vertex::Vertex() {
color = WHITE;
}
Vertex::Vertex(std::string name) {
this->name = name;
color = WHITE;
}
Vertex::~Vertex() {
}
Graph::Graph() {
}
Graph::Graph(int nodeCount) {
adjList.reserve(nodeCount);
}
Graph::~Graph(){
for (int i = 0; i<adjList.size(); i++) {
for (std::list<Vertex*>::iterator iterator = adjList[i].begin(), end = adjList[i].end(); iterator !=end; iterator++) {
delete (*iterator); //fails
}
}
}
int Graph::newVertex() {
Vertex * v = new Vertex();
adjList.push_back(std::list<Vertex *>(1, v));
v->id= (int)adjList.size()-1;
return v->id;
}
int Graph::newVertex(std::string name) {
Vertex * v = new Vertex();
adjList.push_back(std::list<Vertex *>(1, v));
v->id= (int)adjList.size()-1;
v->name= name;
return v->id;
}
void Graph::newUnDirectedEdge(int v1, int v2) {
newDirectedEdge(v1, v2);
newDirectedEdge(v2, v1);
}
void Graph::newDirectedEdge(int v1, int v2) {
Vertex * vertex2 = adjList[v2].front();
adjList[v1].push_back(vertex2);
}
std::list<Vertex*> Graph::getConnections(int v) {
return adjList[v];
}
std::ostream& operator<<(std::ostream& os, const Graph& g) {
for (int i = 0; i<g.adjList.size(); i++) {
for (std::list<Vertex*>::const_iterator iterator = g.adjList[i].begin(), end = g.adjList[i].end(); iterator !=end; iterator++) {
os << (*iterator)->id << " (" << (*iterator)->name << ") ";
}
os << '\n';
}
return os;
}
与主:
#include <iostream>
#include "Graph.hpp"
int main(int argc, const char * argv[]) {
Graph graph(5);
int v1 = graph.newVertex("Paris");
int v2 = graph.newVertex("London");
int v3 = graph.newVertex("Lyon");
int v4 = graph.newVertex("Nice");
int v5 = graph.newVertex("Marseille");
int v6 = graph.newVertex("La Rochelle");
int v7 = graph.newVertex("Toulon");
graph.newUnDirectedEdge(v2, v1);
graph.newUnDirectedEdge(v1, v3);
graph.newUnDirectedEdge(v1, v4);
graph.newUnDirectedEdge(v3, v4);
graph.newUnDirectedEdge(v5, v4);
graph.newUnDirectedEdge(v7, v5);
std::cout << graph;
return 0;
}
请发布一个最小的但* *完整的**演示,读者可以尝试。这听起来像是一个3规则问题。但如果没有代码,就不可能说出来。 –
@ Cheersandhth.-Alf Ok,我添加了更完整的实现 – TheInnerParty
感谢您的更新,Btw。将'Graph(const Graph&)= delete;'(假设你正在使用C++ 11)添加到'Graph'类decl中。如果你的代码编译在各个地方开始呕吐,你肯定会违反[三规则](https://en.wikipedia.org/wiki/Rule_of_three_(C%2B%2B_programming))。 – WhozCraig