I saw strange behavior in a piece of code that I wrote on Linux, and would like to share it to see if anyone knows about this. I had a base class and a derived class. In the base class, I defined a virtual method, and in the derived class I redefined this method with the same signature. Then I used boost bind to start the stream. Here is an example code:
Class Base { public: virtual void DoSomething(); virtual void Init() = 0; ... } Class Derived : public Base { public: void DoSomething(); void Init(); ... }
In the Derived method's Init method, I did the following:
boost::thread *t = new boost::thread(boost::bind(&Base::DoSomething, this));
The DoSomething method of the base class did what it was supposed to do, while the same method of the derived class was an empty method, left there by mistake. Now, during the execution of the above code, most of the time when DoSomething from the base class was executed in a thread, the application worked fine, and sometimes it did not work. After some debugging, I noticed the error above, and removing DoSomething from the derived class solved the problem. Using Eclipse in debug mode it seems that the DoSomething method of the derived class has always been called, while the application from the console has been working most of the time, but not always. Is there a reason for this behavior? I mean, why sometimes the binding function used the base class method, and sometimes the same derived class method?
Thank you in advance
Edit in response to @pmr
It would be difficult to show a complete working example, I will try to show a little how the classes are used.
First I create a Derived object, then in the init function I start the thread with the initialization code shown above. DoSomething has a while loop that iterates over a vector, but that is not what I think.
void Derived::Init() { ... boost::thread *t = new boost::thread(boost::bind(&Base::DoSomething, this)); } void Base::DoSomething() { while(true) { ... } } void Derived::DoSomething() { }
As you can see in this code, the Derived DoSomething method was empty, so sometimes I didnβt see any processing that was performed in the Base DoSomething function instead.