setModelData crashes program
-
@VRonin said in setModelData crashes program:
you just need to add
Q_ASSERT(whatComboEditor);to have thatAh, OK. It's just that you have to remember to write that every time after each
qobject_cast<>, it would be nice to have them combined in one statement. I suppose you would write:Q_ASSERT(whatComboEditor = qobject_cast<>)but is
Q_ASSERTcode removed in non-debug code and/or does it evaluate its argument more than once (assuming it's a macro)?I would like a
qobject_assert_cast<>:)EDIT: Yes, I see you cannot use
Q_ASSERT(whatComboEditor = qobject_cast<>)in non-debug as the code gets removed. -
That's not specific to Q_ASSERT, it's the same for the standard C++ assert. Always test the result of a function and not the function itself.
-
If you want to use it in code that should still be present in release mode you can use
Q_ASSUME@VRonin said in setModelData crashes program:
If you want to use it in code that should still be present in release mode you can use
Q_ASSUMEUm,
I don't think soare you sure: http://doc.qt.io/qt-5/qtglobal.html#Q_ASSUME states it's a compile-time hint, and you get "undefined behaviour" if the condition is false. We need a runtime check of the return result ofqobject_cast<>.EDIT: Unless the docs are misleading, and the true implementation is via https://git.merproject.org/faenil/qtbase/commit/3b0ed624351441a2d7be45cf9582fd36955ae860 , which uses
Q_ASSERT_X? -
Q_ASSUMEin debug mode is likeQ_ASSERTin release mode, ifwhatComboEditoris null the program would crash anyway so you can't do much worse.The undefined behaviour comes from when you use the return value:
if(Q_ASSUME(false))is undefined behaviour. if it just wraps an operation you do not check anyway,Q_ASSUMEdoes nothing to your compiled binary in release mode -
Q_ASSUMEin debug mode is likeQ_ASSERTin release mode, ifwhatComboEditoris null the program would crash anyway so you can't do much worse.The undefined behaviour comes from when you use the return value:
if(Q_ASSUME(false))is undefined behaviour. if it just wraps an operation you do not check anyway,Q_ASSUMEdoes nothing to your compiled binary in release mode@VRonin
That wasn't quite the point, though. I was talking about writing the single-line statement (if Qt won't offer aqboject_assert_cast<>):Q_ASSERT(whatComboEditor = qobject_cast<>)versus
Q_ASSUME(whatComboEditor = qobject_cast<>)The point being that it does the assignment. So we do need the expression to evaluate, just we don't need/care to do the "assertion" part of testing the result in release build. This would not work for
Q_ASSERTas (I believe) it's a macro which produces nothing (no code at all) in release build. That was when you suggestedQ_ASSUME. -
@VRonin
That wasn't quite the point, though. I was talking about writing the single-line statement (if Qt won't offer aqboject_assert_cast<>):Q_ASSERT(whatComboEditor = qobject_cast<>)versus
Q_ASSUME(whatComboEditor = qobject_cast<>)The point being that it does the assignment. So we do need the expression to evaluate, just we don't need/care to do the "assertion" part of testing the result in release build. This would not work for
Q_ASSERTas (I believe) it's a macro which produces nothing (no code at all) in release build. That was when you suggestedQ_ASSUME. -
Q_ASSERT(statement);- Debug:
if(!statement) triggerAnAssertion(); - Release:
;
- Debug:
Q_ASSUME(statement);- Debug:
if(!statement) triggerAnAssertion(); - Release:
statement;
- Debug:
Is the difference clearer now?
-
@gabor53 said in setModelData crashes program:
What can I add to prevent the data from disappearing?
Sounds like a model bug. What is
fixModel? -
@VRonin said in setModelData crashes program:
Sounds like a model bug. What is fixModel?
-
Q_ASSERT(statement);- Debug:
if(!statement) triggerAnAssertion(); - Release:
;
- Debug:
Q_ASSUME(statement);- Debug:
if(!statement) triggerAnAssertion(); - Release:
statement;
- Debug:
Is the difference clearer now?
-
@VRonin said in setModelData crashes program:
Sounds like a model bug. What is fixModel?