Tag Archives: qt

QObject auto-disconnect in destructor

It is pretty nice that QObject::~QObject() automatically disconnects everything connected to the object's slots. However, it would be pretty unwise to always rely on this useful feature. Here's one good example how it can screw up a seemingly harmless code.

Suppose you have some kind of networking class, say, MyProtocol. This class is a descendant of QObject that incorporates several fields, including a custom MyStream object. MyProtocol is also connected to the disconnected() signal of the socket used for network communication. Now MyStream has a destructor that sends some sort of "goodbye message" to the other end using the same socket. When this happens, the socket could realize that the connection is broken and emit disconnected(). Which would call the appropriate slot in the half-destructed MyProtocol:

This could be a source of rather subtle bugs. Since MyStream destructor is called from the MyProtocol destructor, it can’t be guaranteed that the MyProtocol object is still usable. And ~MyStream() doesn’t use it directly! Instead, it works with the socket, which in turn can emit some signals, and some slots will get called, some of them possibly in the half-destructed MyProtocol!

How to deal with this? Easy. Disconnect all the signals as soon as you don’t need them any more. Writing a custom destructor for MyProtocol that does it would work too. Too bad Qt doesn’t provide a way to disconnect every signal connected to a particular object, only the other way around (all slots connected to the object signals). The hardest part of this kind of bugs is to figure out what happens, as the program could break at any point later due to memory corruption.

Qt Jambi

People often say that I like Java not. It is not strictly true. Java would be a wonderful language, if… if what? I usually say it not, because people would not listen anyway ^_^ There are a lot of ifs actually, and all of them combined make java almost useless. Not in sense that it is not suited to any task, but in sense that there is always a better way. Graphics in Java is too slow, so everything that hath GUI should be developed with something like Qt (that means, C++ or Python). Long-running applications are vulnerable to memory leaks in Java VM, so it is also a bad task for Java. Various text-processing tasks would be wonderful job for Java, if there was no Perl nor Bison and Flex. High-level network applications require an elegant API, so Java may rest, Qt will do a job here. Low-level require high performance which also meaneth Qt or C for lower-level tasks.

Now there are a lot of Java applications but they just make no sense. Azureus, for example, which I had to replace with poor analog in Python because Azureus was overloading my X server (a network application, not a video player or 3D game!). Azureus would be better implemented in Qt/C++ or Qt/Python (GUI part) and Qt/C++ or plain C (network part). There are much more examples of improper uses of Java. I know of no proper one.

But now something happened what really made me feel slightly more better for Java. Trolltech released Technology Preview of Qt Jambi – a Qt variant for Java. Now a lot of things became better.

First of all, it is finally possible to create efficient GUI applications in Java. Unfortunately, I still know Python not, so I am unable to say whether there are any cases when Qt Jambi would be somehow better than PyQt, but the very fact that it is now possible to write something in Java that will have a GUI but will not make everything slow as hell is rather nice.

Another thing, Qt seemeth to become not only cross-platform, but also cross-language toolkit. On top of all, it is now possible to create applications consisting of parts in C++ and Java. Of course, there is JNI, but it is something like black magic compared to Qt Jambi in terms of clarity ^_^

What I like not is that they released it in binary form. And they are telling us something about feedback from open source community! How the hell can they hope for it without releasing source code?! Hope they will dual-license Qt Jambi with its official release.

Nonetheless, it is something unbelievably wonderful. Just the feeling of using Qt Designer for Java application is something.