Results 1 to 13 of 13

Thread: Recursive function crashing progam

  1. #1
    Join Date
    Aug 2009
    Location
    United States
    Posts
    45
    Thanks
    20
    Qt products
    Qt4
    Platforms
    Windows

    Default Recursive function crashing progam

    I'm having trouble with a recursive function crashing my program. I'll post the function and then explain what it does:

    Qt Code:
    1. void MainWindow::sendGlobalCoordinates()
    2. {
    3. if(myThread->TXFlag == 1)
    4. {
    5. return sendGlobalCoordinates(); // try try again
    6. }
    7. else // TXFlag == 0
    8. {
    9. if(globalCoordinatesSent == 0) // if x&y not sent, Send the x
    10. {
    11. myThread->TXBytes[0] = 56; // setGlobalPosX
    12. myThread->TXData = (int)(pointVector[calPoint2].gX * (double)4000);
    13. myThread->bytesToSend = 4; // command(1) + data(2) + sign(1)
    14. myThread->TXFlag = 1;
    15. globalCoordinatesSent++;
    16. }
    17. else if(globalCoordinatesSent == 1) // If x sent, but y not sent, sent the y
    18. {
    19. myThread->TXBytes[0] = 57; // setGlobalPosY
    20. myThread->TXData = (int)(pointVector[calPoint2].gY * (double)4000);
    21. myThread->bytesToSend = 4; // command(1) + data(2) + sign(1)
    22. myThread->TXFlag = 1;
    23. globalCoordinatesSent++;
    24. }
    25. else if(globalCoordinatesSent == 2) // if x&y sent
    26. {
    27. startButton->setEnabled(1); // let the drilling begin!
    28. return; // the recursive loop must be broken.
    29. }
    30. return sendGlobalCoordinates(); // keep going
    31. }
    32. }
    To copy to clipboard, switch view to plain text mode 

    OK, so here's the deal. I have a QThread running - named myThread - and its sole purpose is to send and receive data over the serial port. The function you see above was written to solve a problem. I need to send two sets of data over the serial port, but the second set of data can't be sent until the first one has been sent. So my idea was to write this recursive function which first sends the first set of data (if nothing is currently being sent over the serial port) and then, after that, sends the second set of data. If the serial port is busy sending data then the function just returns calling itself and eventually the job should get done. Brilliant idea, right? Unfortunately, the program crashes when I run it, and I know that the recursion is causing the problem (commenting out " return sendGlobalCoordinates();" keeps the program from crashing).

    Any advice as to what I should do?

  2. #2
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Recursive function crashing progam

    What's the return value of a void function?

  3. #3
    Join Date
    Sep 2010
    Posts
    145
    Thanks
    1
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Recursive function crashing progam

    Quote Originally Posted by SixDegrees View Post
    What's the return value of a void function?
    The answer is: nothing, which is what a function with a void return type expects. Now for your question: what does this have to do with the question?

    To the OP: Can you show more code? Like the complete definition of MainWindow?

  4. #4
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Recursive function crashing progam

    Quote Originally Posted by N3wb View Post
    I need to send two sets of data over the serial port, but the second set of data can't be sent until the first one has been sent.
    Use a queue.

  5. #5
    Join Date
    Aug 2009
    Location
    United States
    Posts
    45
    Thanks
    20
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Recursive function crashing progam

    To the OP: Can you show more code? Like the complete definition of MainWindow?
    Sure, I can post more code. I'm not sure what's relevant to the problem though and there's a couple thousand lines in the program, too long to post on here. But here's the definition of MainWindow:

    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include "baction.h"
    6. #include "caction.h"
    7. #include "point.h"
    8. #include "serial.h"
    9. #include <QInputDialog>
    10. #include "arrowbutton.h"
    11.  
    12. #include "vector"
    13. using namespace std;
    14.  
    15. class MainWindow : public QMainWindow
    16. {
    17. Q_OBJECT // Q_OBJECT macro manditory for classes containing signals and slots.
    18.  
    19. public:
    20. MainWindow(); // Constructor
    21.  
    22. private slots:
    23. void setBaud(int);
    24. void setCom(int);
    25. void setComCustom();
    26. void open();
    27. void arrowClicked(QString number);
    28. void setCalPoint1();
    29. void setCalPoint2();
    30. void startDrilling();
    31. void pauseDrilling();
    32. void abortDrilling();
    33. void keyPressEvent(QKeyEvent *key);
    34. void testSlot(QString message);
    35. void sendData();
    36. //void calPoint1Changed(int value);
    37. //void calPoint2Changed(int value);
    38. void changeAngle(int);
    39. void SignalUpdate();
    40.  
    41. private:
    42. void createMenus(); // Creates the menus
    43. void createStatusBar(); // Creates the status bar
    44. void paintEvent( QPaintEvent *event );
    45. void getDrillCoordinates(QString);
    46. void calculateGlobalCoordinates();
    47.  
    48. void mousePressEvent(QMouseEvent * e); // new
    49.  
    50. void sendGlobalCoordinates();
    51.  
    52. QMenu *fileMenu;
    53. QMenu *baudMenu;
    54. QMenu *comMenu;
    55. QMenu *helpMenu;
    56.  
    57. // Declare file menu actions
    58. QAction *fileExitAction;
    59. QAction *fileOpenAction;
    60. // Declare baud menu actions
    61. BAction *baud110Action;
    62. BAction *baud600Action;
    63. BAction *baud2400Action;
    64. BAction *baud9600Action;
    65. BAction *baud56000Action;
    66. BAction *baud115200Action;
    67. // Declare com menu actions
    68. CAction *com1Action;
    69. CAction *com2Action;
    70. CAction *com3Action;
    71. CAction *com4Action;
    72. CAction *com5Action;
    73. CAction *com6Action;
    74. CAction *comCustomAction;
    75. // Declare help menu actions
    76. QAction *helpContentsAction;
    77. QAction *helpAboutAction;
    78.  
    79. QTextEdit *debugOutput;
    80. QTextEdit *serialOutput;
    81.  
    82. QVector<int> *xC; // Hole coordinates
    83. QVector<int> *yC;
    84.  
    85. //QVector<point> *pointVector;
    86. vector<point> pointVector;
    87.  
    88. bool drillFileLoaded;
    89. bool drillCalibrated;
    90.  
    91. int desiredX; // desired position of drill from origin in steps
    92. int desiredY;
    93. int desiredZ;
    94.  
    95. QLabel *statusLabel;
    96. QLabel *drillLoadedLabel;
    97. QLabel *calibrateStatusLabel;
    98. QLabel *drillStatusLabel;
    99. QLabel *boardHeightLabel;
    100. QLabel *boardWidthLabel;
    101. QLabel *calibrateLabel;
    102. QLabel *point1Label;
    103. QLabel *point2Label;
    104. QLabel *desiredZLabel;
    105.  
    106. Thread *myThread;
    107.  
    108. QRadioButton *stepRadio1;
    109. QRadioButton *stepRadio2;
    110. QRadioButton *stepRadio3;
    111. QRadioButton *stepRadio4;
    112. QRadioButton *stepRadio5;
    113. int stepRadio1Value;
    114. int stepRadio2Value;
    115. int stepRadio3Value;
    116. int stepRadio4Value;
    117. int stepRadio5Value;
    118.  
    119. int calPoint1Set;
    120. int calPoint2Set;
    121.  
    122. QPushButton *point1Button;
    123. QPushButton *point2Button;
    124. unsigned int calPoint1;
    125. unsigned int calPoint2;
    126.  
    127. QSpinBox *calPoint1SpinBox;
    128. QSpinBox *calPoint2SpinBox;
    129.  
    130. QPushButton *startButton;
    131. QPushButton *pauseButton;
    132. QPushButton *abortButton;
    133.  
    134. ArrowButton *upButton;
    135. ArrowButton *downButton;
    136. ArrowButton *leftButton;
    137. ArrowButton *rightButton;
    138. ArrowButton *zUpButton;
    139. ArrowButton *zDownButton;
    140.  
    141. double rotate;
    142.  
    143. int flipBoard;
    144.  
    145. int clickedX;
    146. int clickedY;
    147. int mouseClicked;
    148.  
    149.  
    150. double boardWidth;
    151. double boardHeight;
    152.  
    153. int globalCoordinatesSent; // 0 = none, 1 = x sent, 2 = y sent.
    154. };
    155.  
    156. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    Use a queue.
    I thought about implementing some sort of buffer, but it would rather complicate things and I was just looking for a simple and quick solution..

  6. #6
    Join Date
    Sep 2010
    Posts
    145
    Thanks
    1
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Recursive function crashing progam

    Cleaned this up a little for you.

    Qt Code:
    1. void MainWindow::sendGlobalCoordinates()
    2. {
    3. if(myThread->TXFlag == 0)
    4. {
    5.  
    6. if(globalCoordinatesSent == 0) // if x&y not sent, Send the x
    7. {
    8. /*is this actually safe? what are the assumptions about the validity of these writes?*/
    9. /*synchronization?*/
    10. myThread->TXBytes[0] = 56; // setGlobalPosX
    11. myThread->TXData = (int)(pointVector[calPoint2].gX * (double)4000);
    12. myThread->bytesToSend = 4; // command(1) + data(2) + sign(1)
    13. myThread->TXFlag = 1;
    14. globalCoordinatesSent++;
    15. }
    16. else if(globalCoordinatesSent == 1) // If x sent, but y not sent, sent the y
    17. {
    18. myThread->TXBytes[0] = 57; // setGlobalPosY
    19. myThread->TXData = (int)(pointVector[calPoint2].gY * (double)4000);
    20. myThread->bytesToSend = 4; // command(1) + data(2) + sign(1)
    21. myThread->TXFlag = 1;
    22. globalCoordinatesSent++;
    23. }
    24. else if(globalCoordinatesSent == 2) // if x&y sent
    25. {
    26. startButton->setEnabled(1); // let the drilling begin!
    27. return; // the recursive loop must be broken.
    28. }
    29. }
    30. sendGlobalCoordinates(); // keep going
    31. }
    To copy to clipboard, switch view to plain text mode 

  7. The following user says thank you to Timoteo for this useful post:

    N3wb (12th November 2010)

  8. #7
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Recursive function crashing progam

    Quote Originally Posted by tbscope View Post
    Use a queue.
    ...or a debugger.

  9. #8
    Join Date
    Aug 2009
    Location
    United States
    Posts
    45
    Thanks
    20
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Recursive function crashing progam

    Cleaned this up a little for you.
    Thanks, that is a bit clearer.

    Unfortunately the program still crashes.

    ...or a debugger.
    I actually tried using the debugger in Qt, but due to my lack of experience I accomplished nothing.

  10. #9
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Recursive function crashing progam

    What error messages are produced when the program fails?

  11. #10
    Join Date
    Aug 2009
    Location
    United States
    Posts
    45
    Thanks
    20
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Recursive function crashing progam

    Well, the program window kind of greys out and a Windows dialog box pops up saying "test_project.exe has stopped working." I click "close the program" and in "application output" in Qt Creator it says "... test_project.exe exited with code -1073741571"

    #
    /*is this actually safe? what are the assumptions about the validity of these writes?*/
    #
    /*synchronization?*/
    There is no confirmation that the data was sent and received correctly. I've never had data lost before so I just assume it transfers OK every time..

  12. #11
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Recursive function crashing progam

    Just looking at the few lines at the start:
    Qt Code:
    1. void MainWindow::sendGlobalCoordinates()
    2. {
    3. if(myThread->TXFlag == 1)
    4. {
    5. return sendGlobalCoordinates(); // try try again
    6. }
    To copy to clipboard, switch view to plain text mode 
    Assuming that TXFlag is 1 how many times is sendGlobalCoordinates() going to call itself before it returns? For every call there must be a matching return; are you sure you will get that?

    Every time you call a recursive function it has to record where to return to and create any new local variables on the stack. If the function calls itself in a tight loop like this the stack grows very quickly and eventually hits a system limit or exhausts system memory. How quickly will that happen? Assuming the TXFlag changes after a 4 characters are sent via the serial port at 9600 baud, that will take about 4 msecs (it may be longer). In that time your tight loop may call itself several thousand times. If the data block is longer then the situation gets worse. If your returns don't match your calls then these resources are permanently tied up.

    This problem does not strike me as a classic case for recursion. Have you thought about having the sender thread signal when it is done?

    Incidentally, 4 bytes does not seem to be correct: 1 byte command, an int data (4 bytes, but you should use sizeof(int)), and the sign (wherever that is defined).

  13. The following user says thank you to ChrisW67 for this useful post:

    N3wb (15th November 2010)

  14. #12
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Recursive function crashing progam

    Code -1073741571 indicates a stack overflow, in line with ChrisW67's post. Sounds like your recursion isn't terminating.

  15. The following user says thank you to SixDegrees for this useful post:

    N3wb (15th November 2010)

  16. #13
    Join Date
    Aug 2009
    Location
    United States
    Posts
    45
    Thanks
    20
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Recursive function crashing progam

    I still haven't been able to figure this out, so I'm just going to forget trying to use a recursive function. I'm not sure why it's not working, but it's more trouble than it's worth. I'll just use some sort of signal like someone else suggested.

    Thanks for all of your help guys!

Similar Threads

  1. Replies: 5
    Last Post: 15th August 2010, 21:34
  2. Recursive min problem
    By Petr_Kropotkin in forum General Programming
    Replies: 1
    Last Post: 27th February 2010, 19:24
  3. FTP recursive treeWidget
    By Aji Enrico in forum Qt Programming
    Replies: 2
    Last Post: 12th April 2008, 11:26
  4. recursive problem
    By Shawn in forum Qt Programming
    Replies: 6
    Last Post: 16th October 2007, 18:54
  5. how to recursive a directory in qt?
    By deweyjew in forum Qt Programming
    Replies: 5
    Last Post: 7th November 2006, 09:06

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.