vendredi 26 février 2016

unique_ptr : Is my memory getting deleted twice?

I have code like this:

class B(){
    A* a;
};

// Is the memory of A still valid after func1() returns?
void func1(const A& a){

    B b = makeB(a);
    std::unique_ptr<B> up(&b);  // I only do this so I can satisfy the func2() interface
    func2(std::move(up));       
}

B makeB(const A& a){
    B b;
    b.a = &a;

    return b;
}

// unique_ptr<> is the interface here, I cannot change it easily
void func2(std::unique_ptr<B> up){

}

Is my argument to func1() valid after the method has returned? I am unsure whether unique_ptr destroys the memory from the way I have assigned it to B::a in makeB().

Aucun commentaire:

Enregistrer un commentaire