Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Why A non-void setter function in a property results in CTD (Segmentation fault) in 'Release' build mode only?
QtWS25 Last Chance

Why A non-void setter function in a property results in CTD (Segmentation fault) in 'Release' build mode only?

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 288 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Curtwagner1984
    wrote on last edited by
    #1

    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 in RELEASE every time I set the property value, the program would crush with a SEGFAULT 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.

    SGaistS 1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      1
      • C Offline
        C Offline
        Curtwagner1984
        wrote on last edited by
        #3

        Also you can compile your program in release with debug information to get a useful backtrace.

        How do you do that?

        Christian EhrlicherC 1 Reply Last reply
        0
        • C Curtwagner1984

          Also you can compile your program in release with debug information to get a useful backtrace.

          How do you do that?

          Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @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?

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          0
          • C Curtwagner1984

            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 in RELEASE every time I set the property value, the program would crush with a SEGFAULT 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.

            SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @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.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            3

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved