Web example of Visitor Design Pattern -- suspected memory leak

Discussion in 'C++' started by pauldepstein@att.net, May 10, 2009.

  1. Guest

    Illustration of Visitor pattern below is copy-pasted from the web: My
    opinion is that the statement delete set; needs to be inserted
    at the end of the for loop in main to prevent a memory leak.
    Am I correct or am I missing something?

    Thanks,

    Paul Epstein

    class Color
    {
    public:
    virtual void accept(class Visitor*) = 0;
    };

    class Red: public Color
    {
    public:
    /*virtual*/void accept(Visitor*);
    void eye()
    {
    cout << "Red::eye\n";
    }
    };
    class Blu: public Color
    {
    public:
    /*virtual*/void accept(Visitor*);
    void sky()
    {
    cout << "Blu::sky\n";
    }
    };

    class Visitor
    {
    public:
    virtual void visit(Red*) = 0;
    virtual void visit(Blu*) = 0;
    };

    class CountVisitor: public Visitor
    {
    public:
    CountVisitor()
    {
    m_num_red = m_num_blu = 0;
    }
    /*virtual*/void visit(Red*)
    {
    ++m_num_red;
    }
    /*virtual*/void visit(Blu*)
    {
    ++m_num_blu;
    }
    void report_num()
    {
    cout << "Reds " << m_num_red << ", Blus " << m_num_blu <<
    '\n';
    }
    private:
    int m_num_red, m_num_blu;
    };

    class CallVisitor: public Visitor
    {
    public:
    /*virtual*/void visit(Red *r)
    {
    r->eye();
    }
    /*virtual*/void visit(Blu *b)
    {
    b->sky();
    }
    };

    void Red::accept(Visitor *v)
    {
    v->visit(this);
    }

    void Blu::accept(Visitor *v)
    {
    v->visit(this);
    }

    int main()
    {
    Color *set[] =
    {
    new Red, new Blu, new Blu, new Red, new Red, 0
    };
    CountVisitor count_operation;
    CallVisitor call_operation;
    for (int i = 0; set; i++)
    {
    set->accept(&count_operation);
    set->accept(&call_operation);
    // I would now say delete set; but the author didn't.
    }
    count_operation.report_num();
    }
    , May 10, 2009
    #1
    1. Advertising

  2. On May 10, 10:28 pm, wrote:
    > Illustration of Visitor pattern below is copy-pasted from the web:  My
    > opinion is that the statement  delete set;   needs to be inserted
    > at the end of the for loop in main to prevent a memory leak.
    > Am I correct or am I missing something?


    All memory is freed anyway when the process is terminated after main
    returns, and none of those Color subclasses have a destructor, so the
    deletes will have no impact. But yes, it is generally a good idea to
    call delete on any new'd objects before main returns, just in case
    someone adds a destructor later, or moves the code into a non-main
    function.
    -Andrew.
    Andrew Tomazos, May 10, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Al Franz

    suspected cPickle memory leak

    Al Franz, May 13, 2005, in forum: Python
    Replies:
    0
    Views:
    423
    Al Franz
    May 13, 2005
  2. Siphiuel
    Replies:
    0
    Views:
    323
    Siphiuel
    Mar 7, 2006
  3. tikcireviva
    Replies:
    2
    Views:
    460
    tikcireviva
    Jan 15, 2007
  4. aaragon
    Replies:
    9
    Views:
    677
    aaragon
    Aug 14, 2008
  5. Pallav singh
    Replies:
    0
    Views:
    345
    Pallav singh
    Jan 22, 2012
Loading...

Share This Page