I have a slightly modified version of the thread class copied off the Linux Self Help site that I have used to create a threading base class:
class Thread
{
public:
static void *entry (void *pvArg) { Thread *pobjThread = static_cast<Thread *> (pvArg); pobjThread->run (); }
virtual void run (void) = 0;
};
I have 2 thread classes:
class Item : public Thread
and
class Product : public Thread
class Item starts the thread from the constructor of the function, which class into pthread library to create the thread calling entry with this as the pvArg while class Product creates it’s thread later during programme execution.
Now the thing is, class Item works fine. The runfunction is called and processes correctly. However, when class Product calls the same function later, I get:
pure virtual method called
Both class have the same implementation with overloading the run method, but one is called and the other is not.
Why would I suddenly get a pure virtual method called exception?
Thanks.
Update:
class Item is different the class Product because Item is declared as a static Item item; in the cpp file and there is only one. class Product is used like a normal object. If I do the same thing to class Product it works fine.
Thanks to what modelnine pointed out, we found the offending code was creating an object, starting the thread and then destroying the object, before the thread got a chance to run. This, as modelnine indicated, delete’s the vtable, and this caused the problem.
Thanks to modelnine in the comments of the question.