c++ - 删除双链循环列表时出错

标签 c++ pointers delete-operator

好吧,我需要一个设计良好的双链循环列表,因为没有标准,所以我在下面构建了这个类。问题是当退出程序或完全删除 CycleList 时出现错误:访问冲突读取位置 0xfeeeff22。 (MVS 2010)

有趣的是,当我调试时,一切似乎都被正确删除了。 (析构函数中的那 3 个 cout 完美打印...)。

有什么想法吗?

class CycleList{
public:

struct Node{
    static int s_size;
    static int e_size;
    IwPoint3d P;
    IwVector3d N;
    Node* next;
    Node* prev;

    Node(IwPoint3d _P, IwVector3d _N){
        P = _P;
        N = _N;
        next = this;
        prev = this;
        ++Node::s_size;
    }

    Node(IwPoint3d _P, IwVector3d _N, Node* _prev, Node* _next){
        P = _P;
        N = _N;
        prev = _prev;
        next = _next;
        prev->next = this;
        next->prev = this;
        ++Node::s_size;
    }

    ~Node(){
        prev->next = next;
        next->prev = prev;
        --Node::s_size;
    }
private:
    Node(const Node& rhs){} //copy forbidden
    Node operator=(const Node&){}
};

Node* current;
int size;

CycleList(IwPoint3d P, IwVector3d N){
    current = new Node(P,N);
    size = 1;
}

~CycleList(){
    while(!empty()){
        delete current->next;
    }
    cout << "I'm here" << endl;
    delete current;
    cout << "and here.." << endl;
    size = 0;
    cout << "and even here..." << endl;
}

bool empty() const{
    return current == current->next;
}

void insert_front(IwPoint3d P, IwVector3d N){
    new Node(P,N,current,current->next);
    ++size; 
}

void insert_back(IwPoint3d P, IwVector3d N){
    new Node(P, N,current->prev,current);
    ++size;
}

void delete_front(){
    if(!empty()){
        delete current->next;
        --size;
    }
}
void delete_back(){
    if (!empty()){
        delete current->prev;
        --size;
    }
}

IwPoint3d &front(){
    return current->next->P;
}

IwPoint3d &back(){
    return current->prev->P; 
}

void print(){
    for (int i = 0; i < size; ++i){
        cout << '(' << current->P.x << ", " << current->P.y << ", " << current->P.z << ')' << endl;
        current = current->next;
    }
}
};

好的,我找到了。我有一个 std::List 这个 CycleList 是通过复制得到它们的,我正在删除它们,然后在程序结束时删除这个列表时可能跟随不再指向任何地方的指针......

我有类似的东西在函数结束时崩溃:

CycleList initial_front(hex1_p,hex1_n);
initial_front.print();

list<CycleList> Fronts;

Fronts.push_back(initial_front);

while (Fronts.size() > 0){
    CycleList current_front = Fronts.front();
    Fronts.pop_front();
    current_front.print();
}

但类似的东西正在起作用:

CycleList initial_front(hex1_p,hex1_n);
initial_front.print();

list<CycleList*> Fronts;

Fronts.push_back(&initial_front);

while (Fronts.size() > 0){
    CycleList* current_front = Fronts.front();
    Fronts.pop_front();
    current_front->print();
}

如果您有最好的方法,我会很高兴知道。

最佳答案

乍一看,如果不提高电流,这似乎是双重免费。

while(!empty()){
    delete current->next;
}

关于c++ - 删除双链循环列表时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22036546/

相关文章:

c++ - 使用 Flatbuffer Union 导致双重释放或损坏错误

c++ - delete[] 在 C++ 中真的有效吗?

c++ - 使用与 new 中使用的指针类型不同的指针删除内存是否安全?

c++ - 如何安装 boost::contract?

c++ - 在模板类中调用静态模板方法的问题

c++ - 什么是压缩 2D 曲线的最佳数据结构/算法?

使用三重指针创建矩阵会导致段错误

c++ - 相同大小的 Win32 加密

C: 如何将经过 malloc 处理的指针传递给函数并释放它们?

c - C 读取文件并返回指针