How to get rid of segmentation fault in the following code ?
-
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 ?
-
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 formessageDisplayTime
and QTimer. -
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 ) ?
-
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) ; ...
-
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 ? -
You can build both the timer and dialog only once and re-use them for example.
-
"for example" in this case means "is one of the possibilities to solve your problem".
You misunderstood my signature ;-)