Virtual method and this pointer

I'm just starting to learn C ++, and I'm trying to create a Thread class that has the basic functionality of the Java Thread class. What I'm trying to do is make a class that you subclass, write a Run method (which is pure virtual in the base class), create a subclass object, call the start method on it, and you have a stream.

The problem is that the way I use C ++ does not send correctly - it, as the Run function, is not virtual, the Run method of the base class is called.

Here is the code for the header

#ifndef _THREAD_H_ #define _THREAD_H_ #include <pthread.h> class Thread { public: Thread(); void Start(); ~Thread(); protected: virtual void Run() = 0; private: static void *RunWrapper(void *); pthread_t thread; }; #endif 

Implementation

 #include "thread.h" #include <pthread.h> Thread::Thread() { } void Thread::Start() { pthread_create(&thread, NULL, Thread::RunWrapper, (void *) this); } void *Thread::RunWrapper(void *arg) { Thread *t = (Thread *) arg; t->Run(); return arg; } Thread::~Thread() { pthread_join(thread, NULL); } 

And a file that is actually trying to do something

 #include <iostream> #include "thread.h" class MyThread : public Thread { protected: void Run() { std::cout << "The thread is runned" << std::endl; } }; int main(void) { MyThread thread; thread.Start(); return 0; } 

The error I get the last 10 hours:

 pure virtual method called terminate called without an active exception 
+1
c ++ linux pthreads
Oct 01 '10 at 0:12
source share
3 answers

The problem is that your MyThread object is basically destroyed as soon as the main function returns, which is likely to happen before the new thread really starts calling its Start method.

As part of the destruction process, the vtable will reset for the vtable of the base class before calling the base class destructor, so when the RunWrapper call is launched, it finishes running the pure virtual method error. Calling a method on a destroyed object leads to undefined behavior, so anything can happen; this behavior is a coincidence of how your C ++ compiler implements destructors and stack allocation.

+5
01 Oct 2018-10-10T00:
source share

I donโ€™t know what causes the error directly, but what you are doing is bad: your MyThread object may go out of scope before your thread accesses it. The region may have disappeared and the pointer is invalid at the time the stream begins.

Try allocating the object on the heap and see if it works (then, assuming that it is so, figure out how to free the object when the thread executes).

Oh, and you will need to exit your application (returning from the main one) until your thread is executed ...

0
01 Oct '10 at 0:18
source share

The problem is that you are invoking Start, which belongs to the Thread class. This method then calls Run, which is called by the Run method of the Thread class. Unfortunately, you cannot call the overrided method from the base class.

-2
Oct 01 '10 at 0:20
source share



All Articles