{"id":132,"date":"2009-11-23T22:49:06","date_gmt":"2009-11-23T21:49:06","guid":{"rendered":"http:\/\/pusling.com\/blog\/?p=132"},"modified":"2016-07-10T15:23:30","modified_gmt":"2016-07-10T14:23:30","slug":"subclassing-qmutex-considered-harmful","status":"publish","type":"post","link":"https:\/\/pusling.com\/blog\/?p=132","title":{"rendered":"Subclassing QMutex considered harmful"},"content":{"rendered":"<p>If you are using <code>QMutex<\/code>, you are probably writing threaded applications, and thus want to use helgrind (part of <a href=\"http:\/\/www.valgrind.org\">valgrind<\/a>).<\/p>\n<p>helgrind has some support for threading and mutexes in Qt, but not fully.<\/p>\n<p>If you try to run a app using a <code>QMutex<\/code> subclass in helgrind, it fails with<\/p>\n<pre class=\"lang:default decode:true \" >&lt;appname&gt;: hg_intercepts.c:2124: _vgwZU_libQtCoreZdsoZa__ZN6QMutexC2ENS_13RecursionModeE: Assertion `0' failed.<\/pre>\n<p>Basically, it is the handler for the <code>_ZN6QMutexC2ENS_13RecursionModeE<\/code> symbol that is implemented as <code>assert(0);<\/code>. The demangled symbol is <code>QMutex::QMutex(QMutex::RecursionMode)<\/code>, which looks very much like the constructor for <code>QMutex<\/code>:<\/p>\n<pre class=\"lang:default decode:true \" >$ objdump -T \/usr\/lib\/libQtCore.so | grep _ZN6QMutexC\r\n00068840 g    DF .text  0000005b  Base        _ZN6QMutexC2ENS_13RecursionModeE\r\n000687e0 g    DF .text  0000005b  Base        _ZN6QMutexC1ENS_13RecursionModeE<\/pre>\n<p>which both demangles into the same <code>QMutex::QMutex(QMutex::RecursionMode)<\/code><\/p>\n<p>The important difference is the C1 vs C2 in the mangled symbol name. The C1 symbol is used for creating <code>QMutex<\/code> objects (complete object constructor), wheras the C2 symbol is used for creating subclasses of <code>QMutex<\/code> (base object constructor).<\/p>\n<p>And the C2 handler in helgrind is as said implemented as <code>assert(0);<\/code>.<br \/>\nSo until helgrind is fixed here, please be careful to not subclass <code>QMutex<\/code>. (the destructor handler for subclasses is also implemented the same way)<\/p>\n<p><code>QMutex<\/code> has no virtual base class, so even in case of subclassing, I think the helgrind handlers should be implemented the same way in the base object constructor as in the base complete object constructor.<\/p>\n<p>I guess I should file a bug against helgrind.<\/p>\n<p>A test app if anyone is curious:<\/p>\n<pre class=\"lang:c++ decode:true \" >#include &lt;QtCore&gt;\r\n\r\nclass mymutex : public QMutex {\r\n        public:\r\n                mymutex(QMutex::RecursionMode=QMutex::NonRecursive);\r\n};\r\nmymutex::mymutex(QMutex::RecursionMode mode) : QMutex(mode) {\r\n}\r\n\r\nint main(int, char**) {\r\n        mymutex m;\r\n        QMutexLocker lock(&amp;m);\r\n        return 0;\r\n}<\/pre>\n<p><strong>Update 1:<\/strong> Bug filed.<br \/>\n<strong>Update 2:<\/strong> Better understanding of virtual base classes. Thanks Thiago<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you are using QMutex, you are probably writing threaded applications, and thus want to use helgrind (part of valgrind). helgrind has some support for threading and mutexes in Qt, but not fully. If you try to run a app using a QMutex subclass in helgrind, it fails with &lt;appname&gt;: hg_intercepts.c:2124: _vgwZU_libQtCoreZdsoZa__ZN6QMutexC2ENS_13RecursionModeE: Assertion `0&#8242; failed. [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,4],"tags":[],"class_list":["post-132","post","type-post","status-publish","format-standard","hentry","category-english","category-kde"],"_links":{"self":[{"href":"https:\/\/pusling.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/132","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pusling.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pusling.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pusling.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/pusling.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=132"}],"version-history":[{"count":8,"href":"https:\/\/pusling.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/132\/revisions"}],"predecessor-version":[{"id":139,"href":"https:\/\/pusling.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/132\/revisions\/139"}],"wp:attachment":[{"href":"https:\/\/pusling.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=132"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pusling.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=132"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pusling.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=132"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}