RFC: How to fix a tricky leak in QFormLayout?

We have a nasty leak in QFormLayout::setWidget(). In setWidget(), we create the QWidgetItemV2 corresponding to the passed QWidget, and pass that on to Private::setItem, which has a bunch of error returns (guard clauses) that do not delete the item, among them negative row index and cell already occupied.

We could easily fix that missing delete, but this function is also used by setLayout(), say, where the item is the layout.

Conceptually deleting that item (= the nested layout) is completely OK, because the user should be able to rely on the form layout to take ownership of the nested layout, without ifs and buts.

But then we have code in tst_qformlayout that breaks. Simplified, it reads:

   QFormLayout layout;
   QHBoxLayout l4;
   layout.addLayout(-1, QFormLayout::FieldRole, &l4);

I guess you spot the problem? If l4 had been added, everything would’ve been peachy: The QHBoxLayout destructor unregistered itself from layout, which does not attempt to delete l4 when it itself is deleted.

But if l4 is not added for some reason, like in the test code above, the fixed code will attempt to delete l4, which is undefined behaviour, of course (double-delete).

I think such broken code deserves to be broken, for the greater good of fixing a resource leak. Esp. since a double-delete should complain much louder than a leak, and the API user can do something about the double-delete while she can’t do anything about the leak (the pointer is not reachable from outside QFormLayout).

I feel uneasy about adding this to 5.6 LTS, though, so I’ll make a minimal fix there, just for setWidget().

What do you think about the larger fix?

Effective Qt @ Meeting C++ 2015: talk is now online

Effective Qt continues as a conference-driven series!

After the guys at Qt World Summit last year managed to mess up the recording of exactly one of the many, many talks there (my Effective Qt one, of course), this is to let you know that Jens Weller of Meeting C++ has uploaded the recording of my Effective Qt talk to his conference channel on YouTube.

I talk about Q_FOREACH vs. C++11 range-for, QLatin1String vs. QStringLiteral, as well as, of course, how bad QList is.


Heise Developer: GoingNative 2012 — Gegenwart und Zukunft von C++

For you German speakers out there, my sixth Heise Developer article, “GoingNative 2012 — Gegenwart und Zukunft von C++” just went live on Heise’s site.

It’s a sum-up of the GoingNative 2012 conference that happened on Feb 2nd-3rd. There won’t be an English translation later, since it’d be outdated by the time it appeared. The talks are in English, and you can download them all from Microsoft’s Channel9 page, though.

From the article:

Die Qualität der Vorträge war durchweg hoch, wenn auch Nichtmuttersprachler einigen Vortragenden nur schwer folgen konnten. Die Konferenz macht Lust auf ein Jahr mit mehr C++-Konferenzen als seit 1999 üblich. Besonders hängen blieben das Versprechen auf schnelle Adoption des neuen Standards, auch von Clang und Microsoft, und der Versuch, die C++-Standardbibliothek in den nächsten Jahren massiv zu erweitern.

GoingNative12 @ KDAB, Berlin

For everyone interested in C++11, and as suggested by Herb Sutter, KDAB will show the live stream of the GoingNative12 C++ conference in it’s training facilities at Tempelhofer Ufer 11 in Berlin this Thursday and Friday (Feb 2nd and 3rd). The show is scheduled to run from 18:30 to 02:15 (CET) each day. We invite everyone to join us, but please drop us a note at berlin(at)kdab.com so we can plan ahead.

Wild Beasts

In “Private Practice: Taming Templates”, I had (deliberately, of course :)) hidden a bug in the implementation of KDGenericFactory. I’ve now added a discussion about the bug and its fix.

Private Practice: What’s in a Proxy-Style?

A new session in the private practice, “What’s in a Proxy Style?”, examines QProxyStyle and derives some important caveat when applying a classical Design Pattern.

This is the original-language version of my recent Mythos Proxy-Style article.

Private Practice: Taming Templates

I’ve started a new column, Private Practice, that looks at existing implementations and examines them.

The first Private Practice article, Taming Templates, examines techniques to control template code bloat. There’s nothing particularly earth-shattering in there, but I noticed that a lot of people don’t seem to be aware of the fact that templates can cause a lot of duplicated code, so I thought I’d write about it.

Even if they haven’t been released under the Private Practice banner, the following articles also fit that category well: