<div dir="ltr">Hi,<div><br></div><div>If this is the wrong mail-list could someone point me to the correct one please?</div><div><br></div><div>Mac</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Dec 1, 2016 at 5:52 PM, Tiago Macarios <span dir="ltr"><<a href="mailto:tiagomacarios@gmail.com" target="_blank">tiagomacarios@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hi,<div><br></div><div><span style="font-size:12.8px">First time poster so I hope I get the etiquette right.</span><br></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">I am trying to use clang-tidy (3.9.1) with a Qt (5.7) project and I am getting a false positive memory leak.</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">Here is the CMake file:</span></div><div><span style="font-size:12.8px"><br></span></div><div><div><span style="font-size:12.8px">cmake_minimum_required(VERSION 3.2)</span></div><div><span style="font-size:12.8px">project(main)</span></div><div><span style="font-size:12.8px">add_executable(main main.cpp)</span><br></div><div><span style="font-size:12.8px">set_target_properties(main</span><br></div><div><span style="font-size:12.8px"> PROPERTIES</span></div><div><span style="font-size:12.8px"> CXX_STANDARD 14</span></div><div><span style="font-size:12.8px"> CXX_EXTENSIONS OFF</span></div><div><span style="font-size:12.8px"> AUTOMOC ON</span></div><div><span style="font-size:12.8px"> AUTOUIC ON</span></div><div><span style="font-size:12.8px"> CXX_CLANG_TIDY</span></div><div><span style="font-size:12.8px"> "clang-tidy"</span></div><div><span style="font-size:12.8px"> "-checks=modernize-*,<wbr>readability-*,performance-*"</span></div><div><span style="font-size:12.8px"> "-fix"</span></div><div><span style="font-size:12.8px">)</span></div><div><span style="font-size:12.8px">find_package(Qt5Core)</span><br></div><div><span style="font-size:12.8px">target_link_libraries(main Qt5::Core)</span><br></div><div style="font-size:12.8px"><br></div></div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">Here is the main.cpp (it is the minimum amount of code to reproduce the issue, the code itself is brain-dead):</div><div style="font-size:12.8px"><br></div><div><div><span style="font-size:12.8px">#include <QObject></span><br></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">int main(int argc, char *argv[])</span></div><div><span style="font-size:12.8px">{</span></div><div><span style="font-size:12.8px"> QObject a;</span></div><div><span style="font-size:12.8px"> QObject::connect(&a, &QObject::destroyed, []() {});</span><br></div><div><span style="font-size:12.8px"> return 0;</span><br></div><div><span style="font-size:12.8px">}</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px"><br></span></div><div style="font-size:12.8px">clang-tidy will display the following warning:</div></div><div style="font-size:12.8px"><br></div><div><div style="font-size:12.8px">/opt/Qt5.7.0/5.7/gcc_64/<wbr>include/QtCore/qobject.h:343:<wbr>16: warning: Potential memory leak [clang-analyzer-cplusplus.<wbr>NewDeleteLeaks]</div><div style="font-size:12.8px"> return connectImpl(sender, reinterpret_cast<void **>(&signal), context, Q_NULLPTR,</div><div style="font-size:12.8px"> ^</div><div style="font-size:12.8px">/mnt/e/_Working/tidy/main.cpp:<wbr>8:5: note: Calling 'QObject::connect'</div><div style="font-size:12.8px"> QObject::connect(&a, &QObject::destroyed, []() {});</div><div style="font-size:12.8px"> ^</div><div style="font-size:12.8px">/opt/Qt5.7.0/5.7/gcc_64/<wbr>include/QtCore/qobject.h:293:<wbr>16: note: Calling 'QObject::connect'</div><div style="font-size:12.8px"> return connect(sender, signal, sender, slot, Qt::DirectConnection);</div><div style="font-size:12.8px"> ^</div><div style="font-size:12.8px">/opt/Qt5.7.0/5.7/gcc_64/<wbr>include/QtCore/qobject.h:308:<wbr>39: note: '?' condition is true</div><div style="font-size:12.8px"> const int SlotArgumentCount = (FunctorArgumentCount >= 0) ? FunctorArgumentCount : 0;</div><div style="font-size:12.8px"> ^</div><div style="font-size:12.8px">/opt/Qt5.7.0/5.7/gcc_64/<wbr>include/QtCore/qobject.h:340:<wbr>13: note: Left side of '||' is false</div><div style="font-size:12.8px"> if (type == Qt::QueuedConnection || type == Qt::BlockingQueuedConnection)</div><div style="font-size:12.8px"> ^</div><div style="font-size:12.8px">/opt/Qt5.7.0/5.7/gcc_64/<wbr>include/QtCore/qobject.h:340:<wbr>9: note: Taking false branch</div><div style="font-size:12.8px"> if (type == Qt::QueuedConnection || type == Qt::BlockingQueuedConnection)</div><div style="font-size:12.8px"> ^</div><div style="font-size:12.8px">/opt/Qt5.7.0/5.7/gcc_64/<wbr>include/QtCore/qobject.h:344:<wbr>28: note: Memory is allocated</div><div style="font-size:12.8px"> new QtPrivate::QFunctorSlotObject<<wbr>Func2, SlotArgumentCount,</div><div style="font-size:12.8px"> ^</div><div style="font-size:12.8px">/opt/Qt5.7.0/5.7/gcc_64/<wbr>include/QtCore/qobject.h:343:<wbr>16: note: Potential memory leak</div><div style="font-size:12.8px"> return connectImpl(sender, reinterpret_cast<void **>(&signal), context, Q_NULLPTR,</div><div style="font-size:12.8px"> ^</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">Here is the code it refers to QtCore/qobject.h:343:</div><div style="font-size:12.8px"><br></div><div><div style="font-size:12.8px">return connectImpl(sender, reinterpret_cast<void **>(&signal), context, Q_NULLPTR,</div><div style="font-size:12.8px"> new QtPrivate::QFunctorSlotObject<<wbr>Func2, SlotArgumentCount,</div><div style="font-size:12.8px"> typename QtPrivate::List_Left<typename SignalType::Arguments, SlotArgumentCount>::Value,</div><div style="font-size:12.8px"> typename SignalType::ReturnType>(slot),</div><div style="font-size:12.8px"> type, types, &SignalType::Object::<wbr>staticMetaObject);</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">The object created by that new, does get properly destroyed. This seems to happen at <span style="font-size:12.8px">tCore\qobject_impl.h:168:</span></div></div></div><div style="font-size:12.8px"><span style="font-size:12.8px"><br></span></div><div style="font-size:12.8px"><span style="font-size:12.8px"><div style="font-size:12.8px"> static void impl(int which, QSlotObjectBase *this_, QObject *r, void **a, bool *ret)</div><div style="font-size:12.8px"> {</div><div style="font-size:12.8px"> switch (which) {</div><div style="font-size:12.8px"> case Destroy:</div><div style="font-size:12.8px"> delete static_cast<<wbr>QFunctorSlotObject*>(this_); //HERE</div><div style="font-size:12.8px"> break;</div><div style="font-size:12.8px"> case Call:</div><div style="font-size:12.8px"> FuncType::template call<Args, R>(static_cast<<wbr>QFunctorSlotObject*>(this_)-><wbr>function, r, a);</div><div style="font-size:12.8px"> break;</div><div style="font-size:12.8px"> case Compare: // not implemented</div><div style="font-size:12.8px"> case NumOperations:</div><div style="font-size:12.8px"> Q_UNUSED(ret);</div><div style="font-size:12.8px"> }</div><div style="font-size:12.8px"> }</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px"><br></div></span></div><div style="font-size:12.8px"><span style="font-size:12.8px"><br></span></div><div style="font-size:12.8px">From a naive perspective it seems like that false positive leak would be pretty hard to catch. So I guess the best solution (at the moment) would be for me to silence the error. (Please someone correct me if I am wrong).</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">Googling around I found two different ways to silence this false positives:</div><div style="font-size:12.8px"><span style="font-size:12.8px">// NOLINT</span></div><div style="font-size:12.8px"><span style="font-size:12.8px">#ifndef __clang_analyzer__</span></div><div style="font-size:12.8px"><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">I tried to use both in multiple ways, see this SO post: <a href="http://stackoverflow.com/questions/40642307/silencing-clang-tidy" target="_blank">http://stackoverflow.<wbr>com/questions/40642307/<wbr>silencing-clang-tidy</a></span></div><div>But I still haven't figured out a way to silence this error. Maybe someone can help me figure this one out?</div><div><br></div><div><span style="font-size:12.8px">FYI: QObject::connect is used all over Qt code. So I would rather patch the Qt source files, then have to annotate every call to that function.</span><br></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">Mac.</span></div><div style="font-size:12.8px"><span style="font-size:12.8px"><br></span></div><div style="font-size:12.8px"><span style="font-size:12.8px"><br></span></div><div style="font-size:12.8px"><span style="font-size:12.8px"><br></span></div><div style="font-size:12.8px"><span style="font-size:12.8px"><br></span></div><div style="font-size:12.8px"><span style="font-size:12.8px"><br></span></div><div style="font-size:12.8px"><span style="font-size:12.8px"><br></span></div><div style="font-size:12.8px"><span style="font-size:12.8px"><br></span></div><div style="font-size:12.8px"><span style="font-size:12.8px"><br></span></div><div style="font-size:12.8px"><span style="font-size:12.8px"><br></span></div><div style="font-size:12.8px"><span style="font-size:12.8px"><br></span></div><div style="font-size:12.8px"><span style="font-size:12.8px"><br></span></div><div style="font-size:12.8px"><span style="font-size:12.8px"><br></span></div></div>
</blockquote></div><br></div>