Skip to content

Need help to understand lifetime management #28

@FlyinCow

Description

@FlyinCow

godbolt case

#include <print>
#include <vector>

#include "proxy/proxy.h"

PRO_DEF_MEM_DISPATCH(MemEval, eval);
struct Curve : pro::facade_builder                                //
               ::add_convention<MemEval, double(double)>          //
               ::support_copy<pro::constraint_level::nontrivial>  //
               ::build {};

using CurveP = pro::proxy<Curve>;

class SomeCurve {
   public:
    double eval(double t) { return t; }
    ~SomeCurve() { std::println("destruct C"); }
};

PRO_DEF_MEM_DISPATCH(MemDrawOn, draw_on);
struct Drawable : pro::facade_builder                                //
                  ::add_convention<MemDrawOn, void()>                //
                  ::support_copy<pro::constraint_level::nontrivial>  //
                  ::build {};

using DrawableP = pro::proxy<Drawable>;

class Drawer {
   public:
    void draw_on() {}
    Drawer(CurveP c) : c(c) {};
    ~Drawer() { std::println("destruct D"); }

   private:
    CurveP c;
};

class Canvas {
   public:
    void add_drawable(DrawableP d) { drawers.push_back(d); }

   private:
    std::vector<DrawableP> drawers;
};

int main() {
    Canvas cv;
    SomeCurve c;
    cv.add_drawable(pro::make_proxy<Drawable, Drawer>(&c));
}

There are one call of desctruction of SomeCurve which is the SomeCurve c; in int main(), but two calls of destruction of Drawer, and I wonder why, which ones are the Drawer that is destructed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions