[Interest] Double destruction of Qt objects in Qt example code?

Thiago Macieira thiago.macieira at intel.com
Sun Aug 19 19:22:51 CEST 2012


On domingo, 19 de agosto de 2012 17.18.34, Till Oliver Knoll wrote:
> Wow - I did not even know that it was legal C++ to explicitly call a
> d'tor, as in:
> 
>   SomeObject someObject;
> 
>   someObject.~SomeObject();
> 
> (at least my gcc on Mac compiles this without complaint. And as long as
> you don't release any resources twice inside the d'tor - or simply have
> a qDebug("D'tor called.") or something in the d'tor - nothing bad
> happens: the d'tor is indeed simply called twice (once explicitly, once
> the automatic variable goes out of scope).

Yes, it's perfectly valid. The standard will tell you that the destructor 
running twice is undefined behaviour though.

> However it never occured to me to actually try such a thing. I mean,
> what would be a practical scenario where one wanted to explicitly call a
> d'tor? So clean up resources before the actual object gets destroyed
> (assuming that the code inside the d'tor would take care not to delete
> resources twice, such that it would be okay to call a d'tor several times)?

QVector, QList.

And here's the question: without looking at the sources for those classes, how 
do you call the constructor? :-)

Hint: it's not
	someObject.SomeObject()

because that doesn't compile. This is perfectly valid:
	someObject.SomeObject::SomeObject::SomeObject::SomeObject::someFunc()

Once you figure out how to call the constructor, you'll also learn another way 
to call the destructor.

-- 
Thiago Macieira - thiago.macieira (AT) intel.com
  Software Architect - Intel Open Source Technology Center
     Intel Sweden AB - Registration Number: 556189-6027
     Knarrarnäsgatan 15, 164 40 Kista, Stockholm, Sweden
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 190 bytes
Desc: This is a digitally signed message part.
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20120819/5743fa18/attachment.sig>


More information about the Interest mailing list