Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Web example of Visitor Design Pattern -- suspected memory leak

Reply
Thread Tools

Web example of Visitor Design Pattern -- suspected memory leak

 
 
pauldepstein@att.net
Guest
Posts: n/a
 
      05-10-2009
Illustration of Visitor pattern below is copy-pasted from the web: My
opinion is that the statement delete set[i]; 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]; i++)
{
set[i]->accept(&count_operation);
set[i]->accept(&call_operation);
// I would now say delete set[i]; but the author didn't.
}
count_operation.report_num();
}
 
Reply With Quote
 
 
 
 
Andrew Tomazos
Guest
Posts: n/a
 
      05-10-2009
On May 10, 10:28*pm, pauldepst...@att.net wrote:
> Illustration of Visitor pattern below is copy-pasted from the web: *My
> opinion is that the statement *delete set[i]; * 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
visitor design pattern, loki and more aaragon C++ 9 08-14-2008 04:28 PM
May I have a example of design pattern of "composite", I still feel fuzzy after reading book of Addison-Wesley's"design pattern " jones9413@yahoo.com C++ 1 08-31-2007 04:09 AM
Suspected Memory Leak in Multithread queue implmenetation tikcireviva C++ 2 01-15-2007 01:58 PM
Visitor design pattern - breaking dependence on the target hierarchy Siphiuel C++ 0 03-07-2006 11:47 AM
suspected cPickle memory leak Al Franz Python 0 05-13-2005 12:10 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57