title: An issue with arguments for detached thread
body: <p>I am working on a school project (simulation of virtual memory), where we are supposed to use detached threads. There are also other limitations to what we can use but I will mention that later. The problem is that when I give the pthread_create function the last argument as (void*)something and the created thread is detached, the function called by pthread_create will recieve the argument, but since the thread is detached, the original arguments are deleted as soon as the function from where I called pthread_create finishes - that means the argument in the called function is no longer valid and therefore I get segmentation faults etc.. Here is a part of the code (cant post it whole, its very big):</p>
  
  <pre><code>bool CProcManager::NewProcess(void* processArg, void(*entryPoint)(CCPU*, void*), bool copyMem) {
  uint32_t free_page;
  pthread_attr_t attr;
  pthread_t thread;
  
  if (proc_cnt &gt;= 63)
      return 0;
  
  pthread_attr_init(&amp;attr);
  pthread_attr_setdetachstate(&amp;attr, PTHREAD_CREATE_DETACHED);
  
  if (!FindFreePage(free_page)) return 0;
  
  free_page = (free_page &lt;&lt; 12) | 7;
  CProcManager ccpu(m_MemStart, free_page);
  ThrArgs args(entryPoint, (void *) processArg, ccpu);
  
  proc_cnt++;
  if (pthread_create(&amp;thread, NULL, StartWork, (void *) &amp;args))
      return 0;
  
  //pthread_mutex_lock(&amp;start_mtx);
  //pthread_cond_wait(&amp;start_cond, &amp;start_mtx);
  //pthread_mutex_unlock(&amp;start_mtx);
  
  return 1;
  }
  </code></pre>
  
  <p>Here you can see that this function gets a poitner to void and a pointer to function as parameters. pthread_creates tells the thread to call the "StartWork" function with argument "args", which is a structure containing a pointer to function, a pointer to void and an object of type CProcManager.</p>
  
  <pre><code>void* CProcManager::StartWork(void* arguments) {
  //pthread_mutex_lock(&amp;start_mtx);
  ThrArgs *args = (ThrArgs*) arguments;
  CProcManager ccpu = args-&gt;ccpu;
  
  //pthread_cond_signal(&amp;start_cond);
  //pthread_mutex_unlock(&amp;start_mtx);
  
  args-&gt;entryPoint(&amp;ccpu, args-&gt;processArg);
  
  ccpu.RemovePage((ccpu.m_PageTableRoot &gt;&gt; 12) * PAGE_SIZE, false);
  
  pthread_mutex_lock(&amp;end_mtx);
  proc_cnt--;
  pthread_cond_signal(&amp;end_cond);
  pthread_mutex_unlock(&amp;end_mtx);
  return (NULL);
  }
  </code></pre>
  
  <p>In this function, I finally call the function I had a pointer to. As you can guess, the arguments become invalid when the "NewProcess" function ends, so I tried to overcome this by adding a condition variable and somehow make a copy of the arguments I have (havent come to a solution yet) and then let the "NewProcess" function end. But there are certain limitations in this project. The function "NewProcess" is being called many times and the threads should run simultaneously (I tried signaling the condition variable after calling entryPoint and that worked), so I cant finish one thread and then do the next. The parameter "void* processArg" of the first function is originally a certain object type and this type cannot be accessed from the two functions above.</p>
  
  <p>So can anyone please give me a suggestion how copy the arguments so I dont have a segmantation fault?</p>