Qt4 delete this vs deleteLater()

Dario Massarin nekkar at libero.it
Sat Aug 13 09:22:03 BST 2005


Hi all.

While developing the make_kget_cool branch I ran into a problem with the 
current Job class (under kdelibs/kio/kio/job.cpp) that was making my program 
crash.

This was due to the fact that the emitResult() function was deleting itself 
with "delete this;". 
I think this is currently not the right way to delete a QObject, with Qt4, 
especially when the class itself has signals connected to slots belonging to 
other threads. In this case (that is pretty normal using kioslaves) Qt4 makes 
all the signal/slot mechanism asyncronous and we have to replace:
delete this;   --- with ---> deleteLater();

void Job::emitResult()
{
  // If we are displaying a progress dialog, remove it first.
  if ( m_progressId ) // Did we get an ID from the observer ?
    Observer::self()->jobFinished( m_progressId );
  if ( m_error && d->m_interactive && d->m_autoErrorHandling )
    showErrorDialog( d->m_errorParentWidget );
  emit result(this);
  delete this;  ---  REPLACED WITH   --->  deleteLater();
}


I've already committed this fix, but I have also noticed that in the kill 
function we have another "delete this;". 
So my question is: "What do you think about this?". Should we go through 
kdelibs and replace, in every QObject class, "delete this" with deleteLater() 
or there are cases in which this is not the right solution? 


void Job::kill( bool quietly )
{
  kdDebug(7007) << "Job::kill this=" << this << " " << className() << " 
m_progressId=" << m_progressId << " quietly=" << quietly << endl;
  // kill all subjobs, without triggering their result slot
  Q3PtrListIterator<Job> it( subjobs );
  for ( ; it.current() ; ++it )
     (*it)->kill( true );
  subjobs.clear();

  if ( ! quietly ) {
    m_error = ERR_USER_CANCELED;
    emit canceled( this ); // Not very useful (deprecated)
    emitResult();
  } else
  {
    if ( m_progressId ) // in both cases we want to hide the progress window
      Observer::self()->jobFinished( m_progressId );
    delete this;	    -------> SHOULD BE REPLACED??
  }
}


Many thanks,	
	Dario




More information about the kde-core-devel mailing list