c++ - 析构函数好像叫 'early'

标签 c++ memory dynamic destructor

原来这是一个简单的构造函数误用问题。 请参阅“编辑”部分以获取更新信息。

抱歉又问了一个 C++ dtor 问题... 然而,我似乎无法找到与我的完全相同的一个,因为所有其他人都分配给 STL 容器(这将删除对象本身),而我的分配给一个指针数组。

所以我有下面的代码片段

#include<iostream>

class Block{
public:
    int x, y, z;
    int type;
    Block(){
        x=1;
        y=2;
        z=3;
        type=-1;
    }
};

template <class T> class Octree{
    T* children[8];
public:
    ~Octree(){
        for( int i=0; i<8; i++){
            std::cout << "del:" << i << std::endl;
            delete children[i];
        }
    }    
    Octree(){
        for( int i=0; i<8; i++ )
            children[i] = new T;
    }
    // place newchild in array at [i]
    void set_child(int i, T* newchild){
        children[i] = newchild;
    }
    // return child at [i]
    T* get_child(int i){
        return children[i];
    }
    // place newchild at [i] and return the old [i]
    T* swap_child(int i, T* newchild){
        T* p = children[i];
        children[i] = newchild;
        return p;
    }
};

int main(){
    Octree< Octree<Block> > here;
    std::cout << "nothing seems to have broken" << std::endl;
}

查看输出我注意到析构函数在我认为它应该被调用之前被调用了很多次(因为 Octree 仍在范围内),输出的末尾还显示:

del:0
del:0
del:1
del:2
del:3

Process returned -1073741819 (0xC0000005)   execution time : 1.685 s
Press any key to continue.

由于某种原因,析构函数在循环中经过同一点两次 (0),然后死亡。

所有这些都发生在“似乎没有出错”行之前,这是我在调用任何 dtor 之前所期望的。

提前致谢:)

编辑 我发布的代码删除了一些我认为不必要的东西,但在复制和编译我粘贴的代码后,我不再收到错误。 我删除的是代码的其他整数属性。 原文如下:

#include<iostream>

class Block{
public:
    int x, y, z;
    int type;
    Block(){
        x=1;
        y=2;
        z=3;
        type=-1;
    }
    Block(int xx, int yy, int zz, int ty){
        x=xx;
        y=yy;
        z=zz;
        type=ty;
    }
    Block(int xx, int yy, int zz){
        x=xx;
        y=yy;
        z=zz;
        type=0;
    }
};

template <class T> class Octree{
    int x, y, z;
    int size;
    T* children[8];
public:
    ~Octree(){
        for( int i=0; i<8; i++){
            std::cout << "del:" << i << std::endl;
            delete children[i];
        }
    }

    Octree(int xx, int yy, int zz, int size){
        x=xx;
        y=yy;
        z=zz;
        size=size;
        for( int i=0; i<8; i++ )
            children[i] = new T;
    }
    Octree(){
        Octree(0, 0, 0, 10);
    }
    // place newchild in array at [i]
    void set_child(int i, T* newchild){
        children[i] = newchild;
    }
    // return child at [i]
    T* get_child(int i){
        return children[i];
    }
    // place newchild at [i] and return the old [i]
    T* swap_child(int i, T* newchild){
        T* p = children[i];
        children[i] = newchild;
        return p;
    }
};

int main(){
    Octree< Octree<Block> > here;
    std::cout << "nothing seems to have broken" << std::endl;
}

此外,关于 set_child、get_child 和 swap_child 导致可能的内存泄漏的问题,这将得到解决,因为包装类将在设置之前使用 get 或使用 swap 获取旧子项并在释放之前将其写入磁盘内存本身。

我很高兴这不是我的内存管理失败,而是另一个错误。 我还没有制作复制和/或赋值运算符,因为我只是在测试 block 树,我几乎肯定会很快将它们全部设为私有(private)。

这个版本吐出 -1073741819。

谢谢大家的建议,对于劫持我自己的帖子我深表歉意:$

已解决 一个构造函数调用另一个构造函数的问题。

感谢大家的帮助,对于浪费时间表示歉意:)

最佳答案

有人定义了构造函数和析构函数但没有复制构造函数。是被销毁的拷贝弄乱了计数。关注rule of three .

关于c++ - 析构函数好像叫 'early',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4549978/

相关文章:

.net - 新 .Net 流程的成本

jquery - JQuery 中的动态高度

c++ - 未限定范围的多行 MACROS 的危险

c++ - 如何在 C++ 中伪造虚拟可变参数函数模板?

c++ - 我自己的 C++ 框架(类似 MFC),我可以创建子窗口,但在 WM_CREATE 时无法创建编辑框

java - Java 中的可变长度(动态)数组

javascript - Knockout.js 动态绑定(bind)可观察值

c++ - 为什么 C 程序员使用 typedef 来重命名基本类型?

java - 在Windows中使用Eclipse增加Java堆大小

c++ - 使用 malloc 分配比现有内存更多的内存