C++ - 使用引用计数的基本垃圾收集器

标签 c++ garbage-collection smart-pointers reference-counting

好的,我正在尝试使用引用计数的概念在 C++ 中实现一个垃圾收集器(一个非常基本的收集器),它可以工作,但有些东西我不明白。
我有两个类(class):

  • 一个名为 GC 的类,基本上它所做的只是递增和递减引用计数器
  • > 一个名为 TObject 的类,它扮演着智能指针的角色(我重载了 * 和 -> 运算符,还有 = 运算符)
  • 这是下面的代码: GC.cpp

    #include <iostream>
    
    using namespace std;
    
    class GC {
    public:
        GC(){
            this->refCount = 0;//Initialisation du compteur à zero
        }
    
        void incrementRef(){
            this->refCount++;//Incrémentation du compteur de references
        }
    
        int decrementRef(){
            return this->refCount--;//Décrementation du compteur de references
        }
    
        int getCounter(){//Getter du compteur de references
            return refCount;
        }
        ~GC(){}
    
    private:
        int refCount; //Compteur de references
    };
    

    TObject.cpp:

    #include <iostream>
    #include "GC.cpp"
    
    using namespace std;
    
    template <class T>
    class TObject {
    
    T *p;
    GC *gc;
    
    public:
        TObject(T *p){
            cout<<"refobject"<<endl;
            this->p = p;
            gc = new GC();
            this->gc->incrementRef();
        }
    
        virtual ~TObject(){//Destructeur
            cout<<"delete TObject"<<endl;
            if(this->gc->decrementRef() == 0){
                delete p;
                delete gc;
            }
        }
    
        T* operator->(){//Surcharge de l'opérateur d'indirection
            return p;
        }
    
        T& operator*() const {//Surchage de l'opérateur
            return *p;
        }
    
        TObject<T>& operator=(const TObject<T> &t){
            if(this->gc->decrementRef() == 0){
                delete p;
                delete gc;
            }
            this->p = t.p;
            this->gc = t.gc;
            this->gc->incrementRef();
            return *this;
        }
    
        GC getGC(){
            return *gc;
        }
    };
    

    下面是我在 main 中测试它的方式:

    TObject<int> t(new int(2));
    cout<<"t1 counter: "<<t.getGC().getCounter()<<endl;//Displays 1
    TObject<int> t2(NULL);
    cout<<"t2 counter: "<<t2.getGC().getCounter()<<endl;//Displays 1
    t2 = t;
    cout<<"t1 counter: "<<t.getGC().getCounter()<<endl;//Displays 2, why?
    cout<<"t2 counter: "<<t2.getGC().getCounter()<<endl;//Displays 2
    

    我不明白,我在 t2 中复制了 t 而我没有更新 t1!为什么它的引用计数器也更新了?

    最佳答案

    这是因为,t 和 t2 共享同一个 gc 实例。查看您的重载 = 运算符方法:-

    TObject<T>& operator=(const TObject<T> &t)
    {
       if(this->gc->decrementRef() == 0)
       {
            delete p;
            delete gc;
       }
       this->p = t.p;
       this->gc = t.gc;  // you are using same gc. Instead, you must be using
                         //  this->gc = new GC(); 
       this->gc->incrementRef();
       return *this;
    }
    

    关于C++ - 使用引用计数的基本垃圾收集器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40443081/

    相关文章:

    c++ - 这段代码是如何运行的?

    c++ - C++ 中的 SQL 数据库

    c++ - 它是干净的代码吗?

    jakarta-ee - Java,如何在对象销毁时终止进程

    java - 垃圾收集器对象的内存泄漏

    c++ - 引用计数智能指针引用计数如何工作?

    rust - 如何在实现 Deref 的类型(例如 Box)内对值进行模式匹配,而不复制内容?

    c++ - 错误 : Display duplicated results via pointer

    c++ - Qt5 VS 2010 - QtSerialPort 库失败

    javascript - javascript垃圾收集器是否处理全局变量?