Results 1 to 9 of 9

Thread: Strange behavior of return statement

  1. #1
    Join Date
    Apr 2012
    Posts
    43
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Strange behavior of return statement

    Hi, I have this code:

    Qt Code:
    1. void MyClass::MyMethod()
    2. {
    3. message->setWindowTitle("Error");
    4. message->setText("foo");
    5. message->setInformativeText("bar");
    6. message->setIcon(QMessageBox::Critical);
    7. message->setStandardButtons(QMessageBox::Yes | QMessageBox::No);
    8. ret = message->exec();
    9.  
    10. if(ret == QMessageBox::No)
    11. {
    12. suggestAction->setEnabled(true);
    13. solutions += "baz";
    14. this->suggestSolutions();
    15. return;
    16. }
    17.  
    18. message->setWindowTitle("Title");
    19. message->setText("Message");
    20. message->setIcon(QMessageBox::Critical);
    21. message->setStandardButtons(QMessageBox::Ok);
    22. message->exec();
    23. this->MyMethod();
    24.  
    25. treeClicked(navigator->currentItem());
    26. }
    To copy to clipboard, switch view to plain text mode 

    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?

  2. #2
    Join Date
    May 2012
    Location
    Bangalore, India
    Posts
    271
    Thanks
    29
    Thanked 50 Times in 47 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Strange behavior of return statement

    I didnt understand your problem, share the code.
    Heavy Metal Rules. For those about to rock, we salute you.
    HIT THANKS IF I HELPED.

  3. #3
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Strange behavior of return statement

    look at the disassembly you will get more insight
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Strange behavior of return statement

    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.

  5. #5
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Strange behavior of return statement

    d_stranz has it.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  6. #6
    Join Date
    Apr 2012
    Posts
    43
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: Strange behavior of return statement

    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:

    Qt Code:
    1. void MyClass::MyMethod()
    2. {
    3. verifyPIN(); //user promted to enter PIN here
    4. if(verification was successfull)
    5. {
    6. do some atcions
    7. } else if(verification was unsuccessfull)
    8. {
    9. if(PIN was locked)
    10. {
    11. message->setWindowTitle("Error");
    12. message->setText("PIN was locked");
    13. message->setInformativeText("Do you wand to unblock PIN?");
    14. message->setIcon(QMessageBox::Critical);
    15. message->setStandardButtons(QMessageBox::Yes | QMessageBox::No);
    16. ret = message->exec();
    17.  
    18. if(ret == QMessageBox::Yes) unblockPIN();
    19. if(ret == QMessageBox::No)
    20. {
    21. suggestAction->setEnabled(true);
    22. solutions += "baz";
    23. this->suggestSolutions();
    24. }
    25. return;
    26. }
    27.  
    28. message->setWindowTitle("Title");
    29. message->setText("Wrong PIN");
    30. message->setIcon(QMessageBox::Critical);
    31. message->setStandardButtons(QMessageBox::Ok);
    32. message->exec();
    33. this->MyMethod();
    34. }
    35. treeClicked(navigator->currentItem());
    36. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Strange behavior of return statement

    Line 33 : MyMethod calls itself. This leads to a stack overflow.

  8. #8
    Join Date
    Apr 2012
    Posts
    43
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: Strange behavior of return statement

    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.

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Strange behavior of return statement

    ... and then you get a cascade of returns, which is what you observe.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. QSqlDatabase strange behavior
    By macav in forum Qt Programming
    Replies: 1
    Last Post: 9th February 2011, 15:21
  2. strange behavior of paintEvent
    By hashb in forum Qt Programming
    Replies: 3
    Last Post: 30th August 2010, 07:48
  3. Strange behavior of QSqlTableModel
    By venomj in forum Qt Programming
    Replies: 0
    Last Post: 13th January 2010, 03:29
  4. Strange resize behavior
    By Lykurg in forum Newbie
    Replies: 3
    Last Post: 9th January 2007, 13:56
  5. scrollbars strange behavior
    By siniy in forum Qt Programming
    Replies: 6
    Last Post: 29th December 2006, 10:27

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.