jeudi 3 septembre 2020

What caused the memory leak in this code?

I am inspecting the code that may cause memory leak. I know something is wrong with std::set.erase(this) and the destructor of SomeObject. So how to fix it?

class SomeObject;
////....
std::set<SomeObject*> managedObjects;
///.....
class SomeObject{
public:
    SomeObject(){  managedObjects.insert(this); }
    SomeObject(SomeObject&& S)/*move cter*/{ managedObjects.insert(this); }
    virtual ~SomeObject() { managedObjects.erase(this); }
    ////....
};
////....
void clearAllObjects() {
    for(auto p : managedObjects){
        if(p){
            delete p;
        }
    }
    managedObjects.clear();
}
////....

Aucun commentaire:

Enregistrer un commentaire