Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 7

Qt-interest Archive, May 2008
Is it safe to delete QObject which have parent?


Message 1 in thread

Dear all...

A short question: Is it safe to delete a QObject which has parent?

suppose:

class Thing : public QObject {
 Q_OBJECT
public:
  Thing(QObject* parent=0);
}

int main(){
  QObject parent;
  Thing* aThing = new Thing(&parent);

  //is this safe?
  delete aThing;
  // at the end of the scope, parent will be destroyed...which
according to docs, will also delete all of it's child (again).
}

I tried it, seems OK...but when dealing with pointers, just no error
doesn't mean that the everything is fine.

-- 
 [ signature omitted ] 

Message 2 in thread

Hi,

Am Samstag, 31. Mai 2008 schrieb Barkah Yusuf Widodo:
> A short question: Is it safe to delete a QObject which has parent?

Why shouldn't it be okay? it just de-registers at the parent...

> I tried it, seems OK...but when dealing with pointers, just no error
> doesn't mean that the everything is fine.

Well, dangling pointers is a completely different story. And if you want to be 
safe, you use QPointer for all the QObject-pointers you use. :-)

Arnold
-- 
 [ signature omitted ] 

Attachment: signature.asc
Description: This is a digitally signed message part.