Tuesday, 27 July 2010

Tracking QSharedPointer leaks

Smart pointers are a great thing. When used properly, they can really help make life easier, and simpler. But things can, and do, occasionally go wrong - and that is when the hurt comes in. Qt provides a number of smart pointer classes, some might say too many, but that's a topic for a whole different discussion, one of which is QSharedPointer.

From the documentation:
"The QSharedPointer class holds a strong reference to a shared pointer
The QSharedPointer is an automatic, shared pointer in C++. It behaves
exactly like a normal pointer for normal purposes, including respect
for constness.

QSharedPointer will delete the pointer it is holding when it goes out
of scope, provided no other QSharedPointer objects are referencing it.

A QSharedPointer object can be created from a normal pointer, another
QSharedPointer object or by promoting a QWeakPointer object to a strong
reference.

Essentially, QSharedPointer works through reference counting, which means if you somehow make a mistake with cleaning up your references, the object your shared pointer refers to won't be deleted, and you've got a hard to track memory leak on your hands. This is precisely what happened to me recently at work, amongst a jungle of a few different libraries, so tracing the problem by hand was really not going to happen, so I needed a miracle, or short of that, a reliable way to track reference count changes on a QSharedPointer instance.

Reading up on QSharedPointer's internals, it became obvious that the reference counting was stored in the dpointer of each QSharedPointer instance. The dpointer is shared amongst QSharedPointer instances referring to the same pointer. So, we should be able to set a watch in gdb to break whenever the refcount changes.

First, we need to find out the address of a QSharedPointer instance, so set a breakpoint just after we first create it:


 (gdb) break main.cpp:22
 Breakpoint 1 at 0x8048806: file main.cpp, line 22.
 (gdb) r
 Starting program: /home/burchr/qsharedpointer/qsharedpointer.
 [Thread debugging using libthread_db enabled]

 Breakpoint 1, main (argc=1, argv=0xbffff444) at main.cpp:22
 22>-    QSharedPointer<MyClass> copy(initial);
 (gdb) p initial
 $1 = {<QtSharedPointer::ExternalRefCount<MyClass>> = {<QtSharedPointer::Basic<MyClass>> = { value = 0x804c438}, d = 0x804c448}, <No data fields>}

Now we have the address, we can set a watch on the QBasicAtomicInt in the dpointer, we can watch the refcount for changes:
 (gdb) watch $1.d->weakref
 Hardware watchpoint 2: $1.d->weakref


Continue debugging, and gdb will break whenever the refcount changes, telling us the old and new values, like so:
 (gdb) c
 Continuing.
 Hardware watchpoint 2: $1.d->weakref

 Old value = {_q_value = 1}
 New value = {_q_value = 2}
 0x08048953 in QBasicAtomicInt::ref (this=0x804c44c)
     at /usr/include/QtCore/qatomic_i386.h:120
 120>                 : "memory");



Much thanks to:

Labels: , , , , , ,

Wednesday, 21 July 2010

Qt: Bootstrapping openness

I witnessed (and was pleased to take part in) some interesting discussions on #qt-labs this afternoon, all stemming from a contribution to Qt3Support being rejected.

A long story short, the contribution - despite looking reasonably valid - was rejected because Qt3Support is effectively unmaintained, and as a result, any changes to it could have negative impacts on users of the support API.

I understand that argument, yet at the same time - I can't help but think it's a bit of a backwards approach to be taking. Typically, a contributor will wander along, find a bitrotting module/project that interests them, throw patches at the previous maintainer - and shortly after doing so, find themselves a de-facto (or indeed official) maintainer through their efforts.

This is a natural progression of things and should really be encouraged, it allows what would otherwise be dead code to live on. However, in Qt3Support's case, the central point was that they'd love someone else to take responsibility for it, but don't want to expend the effort themselves to triage, maintain, and otherwise support it right now.

To me, it's a false saving.

Time you save not maintaining it now (and encouraging potential contributors/future maintainers along the way) is spent being forced to put the code on life support maintenance if you decide you want that code, for whatever reason, later on.

Not to mention that those people who might be happily contributing you ideas or patches (to code old and new) are instead going to be spending their spare time doing something more rewarding than having their hard work rejected in the future.

Openness doesn't just happen overnight. It isn't just in the licensing. It requires real persistent effort and culture change. This is something I plan to keep revisiting over my next few posts.

{Note}: Before commenting, please note that this issue isn't *just* based around Qt3Support. That is one example of the problem, yes, but the patch was actually to QWorkspace which actually wasn't part of Qt3Support, and QtSql is in a similar boat - and there is nothing to port code using QtSql *to*.

Labels: , , , , , ,

Monday, 12 July 2010

Devices of the Undead

Just a short note to point n8x0 owners to the unofficial adaptation thread, where Stskeeps has made a preview release which demonstrates the MeeGo handset UX running on n8x0.

As he notes, it is pretty unusably slow right now, but hopefully that is an improvable slow. I've been spending some time with him on this to try improve the libmeegotouch end and it is good to see that progress is happening on getting it actually working on the devices.

It's a little intriguing as to what the slowness actually is. Software rendering on my desktop at least is pretty snappy (even under heavy load). I don't own an n8x0, so I think I'm going to have to dig around on my n900 and see whether I can find anything of interest out.

Labels: , , , ,

Tuesday, 6 July 2010

Dear Blogger

Please stop doing things like randomly adding meta tags to my blog posts that I don't add, and then refuse to save my posts.

Please don't excessively convert htmlentities (< is already converted, you don't need to convert it *yet again* when I switch from HTML back to compose mode)

And a feature request: let me use more horizontal width of my blog while using the awesome themes you provide.

Thanks,
A user

*EDIT* and another wtf. I typed & l t ; in compose mode, and you converted it to >.

STOP SECOND GUESSING ME. If I'm writing HTML in plain text, LEAVE IT IN PLAIN TEXT.

Labels: ,

Friday, 2 July 2010

Oslo!

If you know me better, you'll know that I don't like to travel all that much. Unfortunately for me, this is a cruel irony given both that my family lives in Australia and I live in Europe nowdays - and more relevantly - that I like Norway a lot, amongst many reasons thanks to my significant other, who is Norwegian.



We tend to visit a few times a year, in particular, spending a few months there over summer - and this year is no exception.

This time, though, we decided to do something new. Believe it or not, despite having visited Norway maybe ten times or so in the past few years, I've never been to Oslo. Kamilla hasn't been there much either. So with that in mind, and the possibility to say hi to a few great people at the Oslo Qt office, we headed south for a few days.

It wasn't a pleasant trip down there, unfortunately, as NSB (the train operator) managed to screw things up pretty badly. We were delayed for around 1.5-2 hours total after the train broke down, eventually they gave up on fixing it and sent for some busses - 3 hours nonstop on a bus when you haven't eaten since breakfast is a little rough, but, we made it in the end.
(never, have I stared so long at railways as this day. :))


(off for a wander..)

We weren't there for too long, just a few days - but we got to see some nice sights wandering around on foot, I got to experience trams for the first time (and I must say, I love them), and of course, got to say hi to some great and friendly people from Qt along the way.

(In particular, I'd like to say a huge thanks to Benjamin - for both putting us up (and putting up with us ;) during our time in Oslo, to Andreas - for being a cool guy, and every inch as insane in real life as on IRC - to Thiago and Thomas for an afternoon (and night) of interesting chatter - to Bradley for finally saying hi in person after occasional chats on IRC for far too long - and to everyone else I waved at, said hi to, and otherwise distracted)

(awesome walk is awesome!)


All in all, I'm quite impressed with Oslo. It didn't really strike me as being just a city, it felt more like a town which just happened to house a few hundred thousand more people than usual. I'm not sure whether this is just great design on the part of the city or due to the geography, but the greenery everywhere, water (see above) and relative lack of noise and good public transport all helped.

(last day, shortly before heading to the airport)


I'm not a fan of cities, but I actually enjoyed my time there. :)

(myself, and our gracious host, Benjamin, after a bit of a wander
 up to the dam supplying Oslo with something like 80% of their water..)

Labels: , ,

Qt and Open Governance - it begins!

Just a short post to note that the promised infrastructure for Qt's open governance project is now live - go read Thiago's blogpost announcing this for more information.

This is something that concerns me quite a lot, and I'm extremely happy to see it move forward.

I would like to urge anyone interested to subscribe to the mailing list and help take part in the discussions over the coming weeks.

Labels: , , , , ,