Skip to content

Commit

Permalink
Fix QtDesigner crash in debug mode
Browse files Browse the repository at this point in the history
Debug-mode Qt Designer crashed when tried to instantiate a ctkMatrixWidget because of an assert failure.
It may be useful to know that some operations fail, but making the Qt Designer crash is a too strong way of notifying developers (especially when developers only use CTK and not intend to fix its potential internal errors).

Fixed by adding a new CTK_VALIDATE macro that just logs a warning instead of crashing in debug mode and does nothing in release mode. It should be possible to use this macro instead of Q_ASSSERT where crashing the application in unexpected situations should be avoided.
  • Loading branch information
lassoan committed Jan 25, 2019
1 parent 6cd82aa commit c342461
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
17 changes: 17 additions & 0 deletions Libs/Core/ctkUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,23 @@

#include "ctkCoreExport.h"

/// This macro can be used instead of Q_ASSERT to log a warning when
/// a condition fails. It logs a warning message in debug mode and
/// has no effect in release mode.
#ifndef NDEBUG
# define CTK_VALIDATE(condition) \
{ \
if (! (condition) ) \
{ \
qWarning() << "Check `" #condition "` failed in " << __FILE__ \
<< " line " << __LINE__; \
} \
}
#else
# define CTK_VALIDATE(condition)
#endif


namespace ctk {
///
/// \ingroup Core
Expand Down
5 changes: 3 additions & 2 deletions Libs/Widgets/ctkMatrixWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
// CTK includes
#include "ctkMatrixWidget.h"
#include "ctkDoubleSpinBox.h"
#include "ctkUtils.h"

// Qt includes
#include <QDebug>
Expand Down Expand Up @@ -238,7 +239,7 @@ void ctkMatrixWidgetPrivate::updateGeometries()
bool lastColumn = (j==(ccount-1));
int newWidth = lastColumn ? lastColWidth : colWidth;
this->Table->setColumnWidth(j, newWidth);
Q_ASSERT(this->Table->columnWidth(j) == newWidth);
CTK_VALIDATE(this->Table->columnWidth(j) == newWidth);
}
// rows
const int rcount = q->rowCount();
Expand All @@ -249,7 +250,7 @@ void ctkMatrixWidgetPrivate::updateGeometries()
bool lastRow = (i==(rcount-1));
int newHeight = lastRow ? lastRowHeight : rowHeight;
this->Table->setRowHeight(i, newHeight);
Q_ASSERT(this->Table->rowHeight(i) == newHeight);
CTK_VALIDATE(this->Table->rowHeight(i) == newHeight);
}
this->Table->updateGeometry();
}
Expand Down

0 comments on commit c342461

Please sign in to comment.