Why A non-void setter function in a property results in CTD (Segmentation fault) in 'Release' build mode only?
-
Hello, I've ran into a curious phenomena that I don't understand. I made a property for my class and when implementing the setter, I copied the getter with it's return type and forgot to fix it. The getter had a
bool
return type.Everything work fine in
DEBUG
mode but when I complied inRELEASE
every time I set the property value, the program would crush with aSEGFAULT
without any meaningful way to debug the error or a helpful error message.After more time than I care to admit I found that I forgot to fix the return type, and I fixed it. The crushes stopped.
What I want to know is why. Why did it work in DEBUG but crushed in RELEASE and why a setter having
void
as a return type in a property is a must?Here is an example:
Consider this class:
class StubClass : public QObject { Q_OBJECT StubClass(){ _isTrue = false; } public: Q_PROPERTY(bool isTrue READ isTrueGetter WRITE isTrueSetter NOTIFY isTrueChanged); bool isTrueGetter(){ return this->_isTrue; } void isTrueSetter(bool newIsTrue){ this->isTrue = newIsTrue; emit this->isTrueChanged(); } signals: void isTrueChanged(); private: bool _isTrue; }
Like this it works in both release and debug but if I change:
void isTrueSetter(bool newIsTrue){ this->isTrue = newIsTrue; emit this->isTrueChanged(); }
To:
bool isTrueSetter(bool newIsTrue){ this->isTrue = newIsTrue; emit this->isTrueChanged(); }
It crushes in
Release
only. -
Simplify your program until it no longer crashes.
Also you can compile your program in release with debug information to get a useful backtrace. You code above can not crash but you don't show us how you call this call. I would guess it a dangling pointer. -
Also you can compile your program in release with debug information to get a useful backtrace.
How do you do that?
-
@Curtwagner1984 said in Why A non-void setter function in a property results in CTD (Segmentation fault) in 'Release' build mode only?:
How do you do that?
What build system do you use?
-
@Curtwagner1984 said in Why A non-void setter function in a property results in CTD (Segmentation fault) in 'Release' build mode only?:
Like this it works in both release and debug but if I change:
void isTrueSetter(bool newIsTrue){ this->isTrue = newIsTrue; emit this->isTrueChanged(); }
To:
bool isTrueSetter(bool newIsTrue){ this->isTrue = newIsTrue; emit this->isTrueChanged(); }
It crushes in
Release
only.Hi,
You do not return anything in the latter hence it's undefined behaviour.
You likely have a warning about it when compiling, don't leave warning in your code.