Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does this cause a Seg Fault?
    primarykey
    data
    text
    <p>I'm working on an assignment dealing with threading and synchronization with pthreads. In the example code, the main thread creates two other threads which execute fine. The main thread is blocked until both these "child" threads terminate. At least, this is the way I understand it. When the main thread resumes execution, it seems to be getting a segmentation fault when it calls the destructor for <code>AvionicsTask</code>. Honestly, I have no idea why, except that I may not be initializing something correctly. Anyway, the code is as follows:</p> <p>Task.h:</p> <pre><code>class Task { protected: /* -- NAME */ static const int MAX_NAME_LEN = 15; char name[MAX_NAME_LEN]; /* -- IMPLEMENTATION */ pthread_t thread_id; public: /* -- CONSTRUCTOR/DESTRUCTOR */ Task(const char _name[]) { std::strncpy(name, _name, MAX_NAME_LEN); } ~Task(){} /* -- ACCESSORS */ char * Name(); virtual void Start(); virtual void Run()= 0; static void GracefullyExitMainThread(); }; </code></pre> <p>Task.cpp:</p> <pre><code>#include "task.h" std::vector&lt;pthread_t&gt; tasklist; //keep track of tasks created void * thunkfunc(void * args) { Task * task_instance = (Task *)args; task_instance-&gt;Run(); return NULL; } void Task::Start(){ pthread_t threadmachine; void * start_arg = NULL; pthread_create(&amp;threadmachine, NULL, thunkfunc, this); tasklist.push_back(threadmachine); } void Task::GracefullyExitMainThread() { void** return_value; //unused for(int i = 0; i &lt; tasklist.size(); i++){ pthread_join(tasklist[i], return_value); } } char * Task::Name(){ return name; } </code></pre> <p>Task_Test_step1.cpp:</p> <pre><code>#include &lt;iostream&gt; using namespace std; #include "task.h" class RudderController : public Task { public: RudderController(char _name[]) : Task(_name) {} void Run() { cout &lt;&lt; "Rudder Controller [" &lt;&lt; name &lt;&lt; "] running\n" &lt;&lt; flush; for (int i = 0; i &lt; 10; i++) { cout &lt;&lt; name &lt;&lt; " waiting for next sensor input\n" &lt;&lt; flush; usleep(1000000); cout &lt;&lt; name &lt;&lt; " issueing rudder control command" &lt;&lt; i &lt;&lt; "\n" &lt;&lt; flush; usleep(10000); } } }; class AvionicsTask : public Task { public: AvionicsTask(char _name[]) : Task(_name) {} void Run() { cout &lt;&lt; "Avionics System [" &lt;&lt; name &lt;&lt; "] running\n" &lt;&lt; flush; for (int i = 0; i &lt; 10; i++) { cout &lt;&lt; name &lt;&lt; " waiting for next refresh interval\n" &lt;&lt; flush; usleep(700000); cout &lt;&lt; name &lt;&lt; " refreshing avionics screen " &lt;&lt; i &lt;&lt; "\n" &lt;&lt; flush; usleep(12000); } } }; int main(int argc, char * argv[]) { /* -- CREATE TASKS */ RudderController task1("rudder control"); AvionicsTask task2("avionics task"); /* -- LAUNCH TASKS */ task1.Start(); task2.Start(); Task::GracefullyExitMainThread(); } </code></pre> <p>the output of the program with gdb:</p> <pre><code>Starting program: /home/ben/Desktop/Part 1/test [Thread debugging using libthread_db enabled] [New Thread 0x7ffff707e700 (LWP 6797)] Rudder Controller [rudder control] running rudder control waiting for next sensor input [New Thread 0x7ffff687d700 (LWP 6798)] Avionics System [avionics task] running avionics task waiting for next refresh interval avionics task refreshing avionics screen 0 ... //more messages from the threads ... avionics task refreshing avionics screen 9 [Thread 0x7ffff687d700 (LWP 6798) exited] rudder control issueing rudder control command7 rudder control waiting for next sensor input rudder control issueing rudder control command8 rudder control waiting for next sensor input rudder control issueing rudder control command9 [Thread 0x7ffff707e700 (LWP 6797) exited] Program received signal SIGSEGV, Segmentation fault. 0x0000000000401ffa in AvionicsTask::~AvionicsTask (this=0xffffffffffffffc0, __in_chrg=&lt;optimized out&gt;) at task_test_step1.cpp:21 21 class AvionicsTask : public Task { </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload