Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix QtDesigner crash in debug mode #847

Merged
merged 1 commit into from
Jul 25, 2019

Conversation

lassoan
Copy link
Member

@lassoan lassoan commented Jan 25, 2019

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 converting assert to a debug log.

See user problem report here: https://discourse.slicer.org/t/designer-will-not-load-with-slicer-widgets/5496/4

@pieper
Copy link
Member

pieper commented Jan 25, 2019

+1

I would prefer it if we could get rid of all asserts.

Libs/Widgets/ctkMatrixWidget.cpp Outdated Show resolved Hide resolved
Libs/Widgets/ctkMatrixWidget.cpp Outdated Show resolved Hide resolved
@lassoan
Copy link
Member Author

lassoan commented Jan 25, 2019

@jcfr What do you think about the new CTK_VALIDATE macro that only logs a warning but does not terminate the application on failure? (I've forced push updated code that uses this macro)

Libs/Core/ctkUtils.h Outdated Show resolved Hide resolved
@lassoan lassoan force-pushed the fix-qt-designer-crash branch 2 times, most recently from a9e327b to d77e1cd Compare January 25, 2019 18:30
@lassoan
Copy link
Member Author

lassoan commented Jan 25, 2019

Done. Renamed macro to CTK_SOFT_ASSERT.

@jcfr
Copy link
Member

jcfr commented Jan 25, 2019

One last nitpick, i suggest to use the do {...} while (0) idiom.

For reference, see https://stackoverflow.com/questions/923822/whats-the-use-of-do-while0-when-we-define-a-macro

@pieper
Copy link
Member

pieper commented Jan 25, 2019

I have never understood why people assume it's okay to ignore these conditions in release mode. I prefer either exceptions or the vtkErrorMacro followed by graceful cleanup approach for anything that really "shouldn't happen". When people use asserts they allow release mode code to silently ignore that the program has gotten into a bad state. If you are going to allow a user to go into a bad state then at least leave a log message.

@lassoan
Copy link
Member Author

lassoan commented Jan 26, 2019

Fully agree.

Do you think we should log in release mode builds, too?

@jcfr
Copy link
Member

jcfr commented Jan 26, 2019

should log in release mode builds, too

it depends ...

if we decide "yes", then in the case of this PR, the check in ctkMatrixWidgetPrivate::updateGeometries being not a critical error, I still recommend to do:

#ifndef QT_NO_DEBUG
CTK_SOFT_ASSERT(this->Table->columnWidth(j) == newWidth);
#endif

It basically started to be asserted with the transition to Qt5, it is something we should be fixed/investigated and having the debug version reporting a message is valuable. I looked into it not long ago but got side tracked.

@pieper
Copy link
Member

pieper commented Jan 26, 2019

For our typical use cases I think it's best if debug and release builds behave identically to the extent we can. That makes it easier to replicate user issues and compare log files.

@nolden
Copy link
Member

nolden commented Jan 26, 2019

For our typical use cases I think it's best if debug and release builds behave identically to the extent we can. That makes it easier to replicate user issues and compare log files.

I fully agree. The "assert" macro and its optimization in release mode doesn't make much sense unless it is in super time-critical loop which UI code probably isn't ;)

@lassoan
Copy link
Member Author

lassoan commented Jan 26, 2019

Thanks for all the feedbacks. It is good that we all agree on this.

I've updated the macro to log warning regardless of build mode and it uses do{}while(0) idiom.

/// in two key aspectsit only logs a warning (instead of
/// terminating the application) and the message is logged
/// in both release and debug mode builds (instead of ignoring
/// the condition in release mode).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[...]
/// in two key aspects: (1) it only logs a warning (instead of
/// terminating the application) and (2) the message is logged
/// in both release and debug mode builds (instead of ignoring
/// the condition in release mode).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, fixed.

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_SOFT_ASSERT macro that just logs a warning instead of terminating the application (and does it both in debug and release mode, to make application behavior consistent). It should be possible to use this macro instead of Q_ASSSERT where crashing the application in unexpected situations in debug mode should be avoided.
@lassoan lassoan merged commit 0e1d945 into commontk:master Jul 25, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants