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. How to get rid of segmentation fault in the following code ?
Forum Updated to NodeBB v4.3 + New Features

How to get rid of segmentation fault in the following code ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
qt 5.7
10 Posts 3 Posters 3.6k Views 1 Watching
  • 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.
  • AhtiA Offline
    AhtiA Offline
    Ahti
    wrote on last edited by Ahti
    #1

    When I call 'on_smartModeExitButton_triggered()' function first time ( that is in the if statement of 'on_startcaringButton_clicked' function through 'showSmartModeEnabled()' function) it runs perfectly fine but when I try to call it again from the else part I get segmentation fault. why ?

    My Header File:

    QMessageBox *smartMode ;
    QTimer *notClosed ;
    
    

    My Code:

    void EyeCare::on_smartModeExitButton_triggered(){
    
       smartMode->hide() ;
       notClosed->stop() ;
    }
    
    void EyeCare::messageDisplayTime(){
    
        notClosed = new QTimer(this) ;
        connect(notClosed,SIGNAL(timeout()),this,SLOT(on_smartModeExitButton_triggered(smartMode))) ;
        notClosed->start(2000) ;
    }
    
    void EyeCare::showSmartModeEnabled(){
    
        smartMode = new QMessageBox(this) ;
        smartMode->setWindowFlags(Qt::FramelessWindowHint);
        smartMode->setMaximumSize(100,100);
        smartMode->setMinimumSize(100,100);
        smartMode->setText("Smart Mode Enabled");
        smartMode->setStyleSheet("font: 75 bold 12pt Arial; background-color:#D2D2D2;");
        smartMode->setGeometry(370,300,100,100);
        smartMode->setStandardButtons(QMessageBox::NoButton);
        smartMode->show();
        messageDisplayTime() ;
    }
    
    void EyeCare::on_startcaringButton_clicked()
    {
    
        if (startcaringButtonClickedFlag){
    
             startcaringButtonClickedFlag = 0 ;          
             showSmartModeEnabled() ;
    }else{
    
            QString style = smartMode->text() ;
            style.replace(11,7,"Disabled") ;
            smartMode->setText(style);
            smartMode->show();
            messageDisplayTime() ; // here in this function i  get segmentation fault at the first line of code ( that is at 'smartMode->hide()' )
        }
    }
    

    how can i fix it ?

    what is a signature ?? Lol

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      connect(notClosed,SIGNAL(timeout()),this,SLOT(on_smartModeExitButton_triggered(smartMode))) ; this won't work, it should be connect(notClosed,SIGNAL(timeout()),this,SLOT(on_smartModeExitButton_triggered())) ;

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      AhtiA 1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        To add to @VRonin, you're not doing any checks for smareMode's validity. It will only be valid once you called showSmartModeEnabled. So you are likely accessing a dangling pointer.

        You are also leaking memory since each time you call showSmartModeEnabled it will create a new QMessageBox and the same for messageDisplayTime and QTimer.

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

        AhtiA 3 Replies Last reply
        0
        • VRoninV VRonin

          connect(notClosed,SIGNAL(timeout()),this,SLOT(on_smartModeExitButton_triggered(smartMode))) ; this won't work, it should be connect(notClosed,SIGNAL(timeout()),this,SLOT(on_smartModeExitButton_triggered())) ;

          AhtiA Offline
          AhtiA Offline
          Ahti
          wrote on last edited by
          #4

          @VRonin
          i know that was a mistake i removed that argument
          and it still doesn't work i am getting segmentation fault

          what is a signature ?? Lol

          1 Reply Last reply
          0
          • SGaistS SGaist

            Hi,

            To add to @VRonin, you're not doing any checks for smareMode's validity. It will only be valid once you called showSmartModeEnabled. So you are likely accessing a dangling pointer.

            You are also leaking memory since each time you call showSmartModeEnabled it will create a new QMessageBox and the same for messageDisplayTime and QTimer.

            AhtiA Offline
            AhtiA Offline
            Ahti
            wrote on last edited by Ahti
            #5

            @SGaist

            okay by adding these two lines will i be able to get rid of memory leak ?

            in the ' on_smartModeExitButton_triggered() ' function i added this at end:

            notClosed = NULL ;

            and in the else part ( in 'on_startcaringButton_clicked()' function ) i added this in the end:

            smartMode = NULL ;

            would adding those two lines stop memory leak ?? and how could i show the same messagebox in the else part without creating another one ( if you say its a dangling pointer how could i fix it ) ?

            what is a signature ?? Lol

            1 Reply Last reply
            0
            • SGaistS SGaist

              Hi,

              To add to @VRonin, you're not doing any checks for smareMode's validity. It will only be valid once you called showSmartModeEnabled. So you are likely accessing a dangling pointer.

              You are also leaking memory since each time you call showSmartModeEnabled it will create a new QMessageBox and the same for messageDisplayTime and QTimer.

              AhtiA Offline
              AhtiA Offline
              Ahti
              wrote on last edited by Ahti
              #6

              @SGaist

              i did this:

              void EyeCare::on_smartModeExitButton_triggered(int value){
              
                 if (value){
                     if (test != smartMode)
                         smartMode = test ;
                     smartMode->setVisible(false); ;
                 }else
                     smartMode->setVisible(false); ;
              
                 notClosed->stop() ;
                 notClosed = NULL ;
              }
              

              the value is set to zero if you call it from if part otherwise its one and i created another variable of type QMessageBox namely test that stores the address of smartMode before calling messageDisplayTime() function in the else part ( because in else part it does successfully show the messagebox with smartMode pointer which means its not corrupted until then ) okay now after all this set i don't get any segmentation problem but the smartMode->hide() doesn't work now it doesn't hide the messagebox after every 2 seconds why ?

              This is in the else part:

              ...
              test = smartMode ;  // test is declared as QMessageBox 
              messageDisplayTime(1) ;
              ...
              

              And this is in the if part:

              ...
               messageDisplayTime(0) ;
              ...
              

              what is a signature ?? Lol

              1 Reply Last reply
              0
              • SGaistS SGaist

                Hi,

                To add to @VRonin, you're not doing any checks for smareMode's validity. It will only be valid once you called showSmartModeEnabled. So you are likely accessing a dangling pointer.

                You are also leaking memory since each time you call showSmartModeEnabled it will create a new QMessageBox and the same for messageDisplayTime and QTimer.

                AhtiA Offline
                AhtiA Offline
                Ahti
                wrote on last edited by Ahti
                #7

                @SGaist

                I fixed the problem and here is my new code :

                ...
                }else{
                       QString style = smartMode->text() ;
                        style.replace(11,7,"Disabled") ;
                        smartMode->setText(style);
                        smartMode->show();
                // I replaced messageDisplayTime() function with this
                        notClosed->start(1500);  
                }
                

                I fixed the problem but there will still be memory leak right ?
                because when user again clicks on the startcaringButton my code will create a new messagebox ( in if part ) and will also create a new timer ??????
                i tried to destroy and delete the messagebox in the else part after 'notClosed' timer but i can't as the parent of messagebox is my app itself. So how can i just delete it ? in the else part ? i know i can set no parent to messagebox but that will add extra code right ? so i there any way that i can get rid of memory leak ?

                what is a signature ?? Lol

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  You can build both the timer and dialog only once and re-use them for example.

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

                  AhtiA 1 Reply Last reply
                  1
                  • SGaistS SGaist

                    You can build both the timer and dialog only once and re-use them for example.

                    AhtiA Offline
                    AhtiA Offline
                    Ahti
                    wrote on last edited by
                    #9

                    @SGaist
                    What example i don't see any. Btw you still hiring programmer ? :D

                    what is a signature ?? Lol

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      "for example" in this case means "is one of the possibilities to solve your problem".

                      You misunderstood my signature ;-)

                      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
                      0

                      • Login

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