PDA

View Full Version : Strange behavior of return statement



Raadush
5th October 2012, 12:02
Hi, I have this code:



void MyClass::MyMethod()
{
message->setWindowTitle("Error");
message->setText("foo");
message->setInformativeText("bar");
message->setIcon(QMessageBox::Critical);
message->setStandardButtons(QMessageBox::Yes | QMessageBox::No);
ret = message->exec();

if(ret == QMessageBox::No)
{
suggestAction->setEnabled(true);
solutions += "baz";
this->suggestSolutions();
return;
}

message->setWindowTitle("Title");
message->setText("Message");
message->setIcon(QMessageBox::Critical);
message->setStandardButtons(QMessageBox::Ok);
message->exec();
this->MyMethod();

treeClicked(navigator->currentItem());
}


Problem is, that when I debug this and I press No, application jumps from line 15 on line 26 as expected, but then it jumps onto line 25, then again on 26, on 25 once more, then again on 26 and then out of MyMethod. Any suggestion why?

sonulohani
5th October 2012, 12:17
I didnt understand your problem, share the code.

Santosh Reddy
5th October 2012, 12:25
look at the disassembly you will get more insight

d_stranz
6th October 2012, 02:17
Any suggestion why?

Your code makes no sense. In the first half of MyMethod() (up to line 16), you're setting the parameters for a QMessageBox dialog of some sort, then posting it with exec().

If you click something other than "No" (like "Yes" maybe), then from line 18 onwards, you set up the message box parameters *again*, exec() the message box *again*, and then recursively call the same MyMethod(), which then posts the message box, and so on..

So what you are probably seeing is the result of unwinding the stack after two passes through the same code, because you've gone into MyMethod() at least twice.

If your poor user clicked Yes every time, he'd never get out of the recursion. Click Yes, click OK, click Yes again, click OK again, click Yes again, click OK again, until finally he's going to kill the process, uninstall your app and ask for his money back.

amleto
7th October 2012, 01:35
d_stranz has it.

Raadush
11th October 2012, 09:29
Ok, sorry for posting only that part of a code which is executed during one pass through MyMethod which is making problems. Whole MyMethod logic is:



void MyClass::MyMethod()
{
verifyPIN(); //user promted to enter PIN here
if(verification was successfull)
{
do some atcions
} else if(verification was unsuccessfull)
{
if(PIN was locked)
{
message->setWindowTitle("Error");
message->setText("PIN was locked");
message->setInformativeText("Do you wand to unblock PIN?");
message->setIcon(QMessageBox::Critical);
message->setStandardButtons(QMessageBox::Yes | QMessageBox::No);
ret = message->exec();

if(ret == QMessageBox::Yes) unblockPIN();
if(ret == QMessageBox::No)
{
suggestAction->setEnabled(true);
solutions += "baz";
this->suggestSolutions();
}
return;
}

message->setWindowTitle("Title");
message->setText("Wrong PIN");
message->setIcon(QMessageBox::Critical);
message->setStandardButtons(QMessageBox::Ok);
message->exec();
this->MyMethod();
}
treeClicked(navigator->currentItem());
}

Lesiok
11th October 2012, 09:44
Line 33 : MyMethod calls itself. This leads to a stack overflow.

Raadush
11th October 2012, 10:33
No, it does not. MyMethod calls itself only in case wrong PIN was entered and only limited times (3 times in most cases). After that, the pin is blocked and return statement is executed.

wysota
11th October 2012, 11:31
... and then you get a cascade of returns, which is what you observe.