Missing something?
-
Here's another "oh, duh" bug, in the same vein as Rob's:
class IWorkItem
{
public:
virtual bool DoWork()=0;
};class CSpecificTask : public IWorkItem
{
public:
CSpecificTask(LPCTSTR szName) : m_strName(szName) {}
~CSpecificTask()
{
//...
}
virtual bool DoWork()
{
//...
}
private:
CString m_strName;
}//...
void AddNewWorkItem()
{
EnqueueWorkItem(new CSpecificTask(_T("Blah")));
}void ProcessWorkItem()
{
IWorkItem* pItem = PopWorkItem();
// ...
delete pItem;
}Musta stared at memory leak reports for a good two hours last night, before giving up and going to take a shower. As i was washing my face, i realized what i'd missed... :doh: :-O
every night, i kneel at the foot of my bed and thank the Great Overseeing Politicians for protecting my freedoms by reducing their number, as if they were deer in a state park. -- Chris Losinger, Online Poker Players?
-
Here's another "oh, duh" bug, in the same vein as Rob's:
class IWorkItem
{
public:
virtual bool DoWork()=0;
};class CSpecificTask : public IWorkItem
{
public:
CSpecificTask(LPCTSTR szName) : m_strName(szName) {}
~CSpecificTask()
{
//...
}
virtual bool DoWork()
{
//...
}
private:
CString m_strName;
}//...
void AddNewWorkItem()
{
EnqueueWorkItem(new CSpecificTask(_T("Blah")));
}void ProcessWorkItem()
{
IWorkItem* pItem = PopWorkItem();
// ...
delete pItem;
}Musta stared at memory leak reports for a good two hours last night, before giving up and going to take a shower. As i was washing my face, i realized what i'd missed... :doh: :-O
every night, i kneel at the foot of my bed and thank the Great Overseeing Politicians for protecting my freedoms by reducing their number, as if they were deer in a state park. -- Chris Losinger, Online Poker Players?
I ain't done any C++ for a couple of years. Is it lack of virtual destructor?
Kevin
-
Here's another "oh, duh" bug, in the same vein as Rob's:
class IWorkItem
{
public:
virtual bool DoWork()=0;
};class CSpecificTask : public IWorkItem
{
public:
CSpecificTask(LPCTSTR szName) : m_strName(szName) {}
~CSpecificTask()
{
//...
}
virtual bool DoWork()
{
//...
}
private:
CString m_strName;
}//...
void AddNewWorkItem()
{
EnqueueWorkItem(new CSpecificTask(_T("Blah")));
}void ProcessWorkItem()
{
IWorkItem* pItem = PopWorkItem();
// ...
delete pItem;
}Musta stared at memory leak reports for a good two hours last night, before giving up and going to take a shower. As i was washing my face, i realized what i'd missed... :doh: :-O
every night, i kneel at the foot of my bed and thank the Great Overseeing Politicians for protecting my freedoms by reducing their number, as if they were deer in a state park. -- Chris Losinger, Online Poker Players?
I got myself a copy of PC Lint (and VisualLint) last week and this would of been picked up - lack of a virtual destructor in the base class is such a common mistake, I'm surprised compiler vendors don't explicitly check for it.
Kicking squealing Gucci little piggy.
-
I ain't done any C++ for a couple of years. Is it lack of virtual destructor?
Kevin
Yup.
every night, i kneel at the foot of my bed and thank the Great Overseeing Politicians for protecting my freedoms by reducing their number, as if they were deer in a state park. -- Chris Losinger, Online Poker Players?
-
That's the one. :)
every night, i kneel at the foot of my bed and thank the Great Overseeing Politicians for protecting my freedoms by reducing their number, as if they were deer in a state park. -- Chris Losinger, Online Poker Players?
-
I got myself a copy of PC Lint (and VisualLint) last week and this would of been picked up - lack of a virtual destructor in the base class is such a common mistake, I'm surprised compiler vendors don't explicitly check for it.
Kicking squealing Gucci little piggy.
Rob Caldecott wrote:
lack of a virtual destructor in the base class is such a common mistake, I'm surprised compiler vendors don't explicitly check for it.
Well, it'd get pretty annoying in some places. I have a few sets of classes that are always instantiated and destroyed by various owners, but used by other code that requires only that they implement a pure virtual base class.
every night, i kneel at the foot of my bed and thank the Great Overseeing Politicians for protecting my freedoms by reducing their number, as if they were deer in a state park. -- Chris Losinger, Online Poker Players?
-
Yup.
every night, i kneel at the foot of my bed and thank the Great Overseeing Politicians for protecting my freedoms by reducing their number, as if they were deer in a state park. -- Chris Losinger, Online Poker Players?
It's one of those standard C++ interview questions isn't it? However, I hate questions where they ask you to spot what's wrong with the code and you miss something like this because you tend to see what you want to see. Then if the interviewer pulls you up they can't tell whether you were unaware of the principle or whether you just missed it. I had a question where I was asked what was wrong with a piece of XML and one of the errors was that a closing tag was in the wrong case (one letter of the tag, in fact). I didn't spot it but of course I was perfectly aware that XML is case sensitive!
Kevin
-
I got myself a copy of PC Lint (and VisualLint) last week and this would of been picked up - lack of a virtual destructor in the base class is such a common mistake, I'm surprised compiler vendors don't explicitly check for it.
Kicking squealing Gucci little piggy.
Rob Caldecott wrote:
lack of a virtual destructor in the base class is such a common mistake, I'm surprised compiler vendors don't explicitly check for it.
gcc issues a warning in that case.
-
Here's another "oh, duh" bug, in the same vein as Rob's:
class IWorkItem
{
public:
virtual bool DoWork()=0;
};class CSpecificTask : public IWorkItem
{
public:
CSpecificTask(LPCTSTR szName) : m_strName(szName) {}
~CSpecificTask()
{
//...
}
virtual bool DoWork()
{
//...
}
private:
CString m_strName;
}//...
void AddNewWorkItem()
{
EnqueueWorkItem(new CSpecificTask(_T("Blah")));
}void ProcessWorkItem()
{
IWorkItem* pItem = PopWorkItem();
// ...
delete pItem;
}Musta stared at memory leak reports for a good two hours last night, before giving up and going to take a shower. As i was washing my face, i realized what i'd missed... :doh: :-O
every night, i kneel at the foot of my bed and thank the Great Overseeing Politicians for protecting my freedoms by reducing their number, as if they were deer in a state park. -- Chris Losinger, Online Poker Players?
Gotta ya, because you haven't explicitly declared a virtual destructor in the base class, the compiler creates a concrete one for you implicitly. So where you call delete against the base class rather than the child class, the child classes destructor never gets called and the memory never gets freed. Nasty nasty nasty ...
Regards Ray "Je Suis Mort De Rire" Blogging @ Keratoconus Watch
-
Yup.
every night, i kneel at the foot of my bed and thank the Great Overseeing Politicians for protecting my freedoms by reducing their number, as if they were deer in a state park. -- Chris Losinger, Online Poker Players?
Please forgive my ignorance but why does the destructor have to be Virtual?
--- :beer: Hakuna-Matada :beer: It means no worries for the rest of your days... It's our problem free, Philosophy :jig: It’s rather simple to write an unmanaged C++ application that crashes when it performs a callback. It’s impossible to write a managed application that does the same, thanks to delegates. - Jeff Prosise