Note that there are some explanatory texts on larger screens.

plurals
  1. POIs it okay to use "delete this;" on an object that inherits from a Thread class?
    primarykey
    data
    text
    <p>In general, if you have a class that inherits from a <code>Thread</code> class, and you want instances of that class to automatically deallocate after they are finished running, is it okay to <code>delete this</code>?</p> <p>Specific Example:</p> <p>In my application I have a <code>Timer</code> class with one <code>static</code> method called <code>schedule</code>. Users call it like so:</p> <pre><code>Timer::schedule((void*)obj, &amp;callbackFunction, 15); // call callbackFunction(obj) in 15 seconds </code></pre> <p>The <code>schedule</code> method creates a <code>Task</code> object (which is similar in purpose to a Java TimerTask object). The <code>Task</code> class is <code>private</code> to the <code>Timer</code> class and inherits from the <code>Thread</code> class (which is implemented with pthreads). So the <code>schedule</code> method does this:</p> <pre><code>Task *task = new Task(obj, callback, seconds); task-&gt;start(); // fork a thread, and call the task's run method </code></pre> <p>The <code>Task</code> constructor saves the arguments for use in the new thread. In the new thread, the task's <code>run</code> method is called, which looks like this:</p> <pre><code>void Timer::Task::run() { Thread::sleep(this-&gt;seconds); this-&gt;callback(this-&gt;obj); delete this; } </code></pre> <p>Note that I can't make the <code>task</code> object a stack allocated object because the new thread needs it. Also, I've made the <code>Task</code> class <code>private</code> to the <code>Timer</code> class to prevent others from using it.</p> <p>I am particularly worried because deleting the <code>Task</code> object means deleting the underlying <code>Thread</code> object. The only state in the <code>Thread</code> object is a <code>pthread_t</code> variable. Is there any way this could come back to bite me? Keep in mind that I do not use the <code>pthread_t</code> variable after the <code>run</code> method finishes.</p> <p>I could bypass calling <code>delete this</code> by introducing some sort of state (either through an argument to the <code>Thread::start</code> method or something in the <code>Thread</code> constructor) signifying that the method that is forked to should <code>delete</code> the object that it is calling the <code>run</code> method on. However, the code seems to work as is.</p> <p>Any thoughts?</p>
    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.
    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