PDA

View Full Version : non virtual destructors in few Qt's classes



raszewski
15th January 2013, 09:02
Hi

I was reading Qt's documentation and I noticed that few classes with virtual methods do not have virtual destructors (for example QSettings, QSqlDriver). Could you explain why?

amleto
15th January 2013, 09:14
They do have virtual dtor. Inherited from QObject

raszewski
15th January 2013, 11:15
Yes, but when is it called a derived class destructor?
For example: when I have class that inherits from QSqlDriver and I store pointer QSqlDriver* that points to my class object and in next step I will call delete on that pointer, ~QSqlDriver will be executed, not my class destructor. So that is easy way to get memory leak or maybe I don't get it.

amleto
15th January 2013, 12:29
you don't get it. Your example is not a memory leak. Your class dtor will be called. QObject has virtual dtor, therefore ALL subclasses of QObject WILL have a viritual dtor.

raszewski
15th January 2013, 14:48
Yes, you've got right. I've tested my example and there was no mem leak and everything cleans correctly.