Re: Pure virtual function call in Winamp?
Paavo Helde wrote:
"Robbie Hatley" <lonewolf@well.com> wrote in
news:0cGdnaaabt2ruHzXnZ2dnUVZ_j-dnZ2d@giganews.com:
You cannot test for everything, especially if the problem appears in a
"very particular set of circumstances".
Yes, you can. Search for TDD
I have definitely seen that bug myself, mostly in my own code during
development. In my code, the bugs were always caused by too complex data
structures, where child objects had backlink pointers or references to
their parents, and the object lifetimes were not entirely clear. The
following example ought to exhibit the bug, about once per each 10 runs.
<CUT THE BUGGY CODE>
I modified your example (the destructors should be virtual, and I added
some debug messages).
This example explains better why it happens:
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <string>
bool BadMoonPhase() {
return true;
//return rand()%10 == 0;
}
class X {
public:
X()
{
std::cout<<"X()"<<std::endl;
}
virtual ~X()
{
std::cout<<"~X()"<<std::endl;
}
};
class A {
public:
A(): x_(NULL)
{
std::cout<<"A()"<<std::endl;
}
void SetX(X* x) {x_ = x;}
virtual std::string Description() const=0;
virtual ~A()
{
std::cout<<"~A()"<<std::endl;
delete x_;
}
private:
X* x_;
};
class Y: public X {
public:
Y(A& parent): parent_(parent)
{
std::cout<<"Y()"<<std::endl;
}
std::string Description() const {
return "Y, inside of a " + parent_.Description();
}
virtual ~Y()
{
std::cout<<"~Y()"<<std::endl;
if (BadMoonPhase())
{
std::cerr << "Bad moon phase discovered by: ";
std::cerr << Description() << "\n";
}
}
private:
A& parent_;
};
class B: public A {
public:
B()
{
std::cout<<"B()"<<std::endl;
SetX(new Y(*this));
}
virtual ~B()
{
std::cout<<"~B()"<<std::endl;
}
virtual std::string Description() const { return "B object";}
};
int main() {
srand(time(NULL));
B b;
}
Therefore, this has nothing to do with bad moon phase, but with the bad
design, and trying to be clever.
--
Bolje je ziveti sto godina kao bogatun, nego jedan dan kao siromah!