Results 1 to 2 of 2

Thread: Program runs fine in Debug mode, but produces segmentation fault in Release Mode

  1. #1
    Join Date
    Feb 2012
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Program runs fine in Debug mode, but produces segmentation fault in Release Mode

    Hi Guys,

    Im quite new to Qt and have a problem, which i cant figure out how to solve:

    My program runs fine in Debug mode (using MingW compiler and Windows 7), but when i try to run it in Release mode, it produces "segmentation fault" errors when i close it or when i try to emit a signal in "NewGameWindow:n_pushButton_clicked()". I already searched on the internet and figured out, it probably has to be a wrong pointer, but i do not really know, what to look for in my code. If i show the stack in Debugging mode, it just shows "??" on every level.

    heres my source code:

    Go.pro:
    Qt Code:
    1. #-------------------------------------------------
    2. #
    3. # Project created by QtCreator 2012-02-29T14:20:46
    4. #
    5. #-------------------------------------------------
    6.  
    7. QT += core gui
    8.  
    9. TARGET = Go
    10. TEMPLATE = app
    11.  
    12.  
    13. SOURCES += main.cpp\
    14. Interface/mainwindow.cpp \
    15. Interface/newgamewindow.cpp \
    16. Model/TwoDGrid.cpp \
    17. Model/TwoDCoord.cpp \
    18. Model/GameLogic.cpp \
    19. Interface/GraphicsView/mouseovercircle.cpp
    20.  
    21. HEADERS += Interface/mainwindow.h \
    22. Interface/newgamewindow.h \
    23. Model/TwoDGrid.h \
    24. Model/TwoDCoord.h \
    25. Model/GameLogic.h \
    26. Interface/GraphicsView/mouseovercircle.h
    27.  
    28. FORMS += mainwindow.ui \
    29. newgamewindow.ui
    30.  
    31. OTHER_FILES += \
    32. Interface/White Stone60.bmp \
    33. Interface/White Circle30.bmp \
    34. Interface/Black Stone60.bmp \
    35. Interface/Black Circle30.bmp
    36.  
    37. RESOURCES += \
    38. GameImages.qrc
    To copy to clipboard, switch view to plain text mode 

    main.cpp:
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "Interface/mainwindow.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7.  
    8. MainWindow MainWdw;
    9. MainWdw.resize(700, 700);
    10. MainWdw.show();
    11.  
    12. return a.exec();
    13. }
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp:
    Qt Code:
    1. #include "Interface/mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include "Interface/newgamewindow.h"
    4. #include "Model/GameLogic.h"
    5.  
    6. #include <QtGui>
    7. #include <QLabel>
    8.  
    9. MainWindow::MainWindow(QWidget *parent) :
    10. QMainWindow(parent),
    11. ui(new Ui::MainWindow)
    12. {
    13. ui->setupUi(this);
    14.  
    15. scene = new QGraphicsScene(this);
    16. ui->graphicsView->setScene(scene);
    17. ui->graphicsView->setSceneRect(-300, -300, 600, 600);
    18. ui->graphicsView->setRenderHint(QPainter::Antialiasing);
    19. ui->graphicsView->setBackgroundBrush(QPixmap(":/Background600pattern.jpg"));
    20.  
    21. connect(&theGameLogic, SIGNAL(FieldChangedOut(int,int,int)), this, SLOT(FieldChanged(int,int,int)));
    22. }
    23.  
    24. MainWindow::~MainWindow()
    25. {
    26. delete ui;
    27. }
    28.  
    29. void MainWindow::on_actionNew_Game_triggered()
    30. {
    31. NewGameWindow NewGameWdw(this);
    32. connect(&NewGameWdw, SIGNAL(BoardSizeSet(int)), this, SLOT(BoardSizeSet(int)));
    33. connect(&NewGameWdw, SIGNAL(GameStarted()), this, SLOT(GameStarted()));
    34.  
    35. NewGameWdw.exec();
    36. }
    37. void MainWindow::on_actionExit_triggered()
    38. {
    39. this->close();
    40. }
    41.  
    42. void MainWindow::BoardSizeSet(int Size)
    43. {
    44. if(!theGameLogic.SetSize(Size))
    45. QMessageBox::warning(this, "Error", "Cannot set size. Game already started!");
    46. else
    47. {
    48. BoardSize = Size;
    49.  
    50. int LineWidth = 4;
    51. BorderWidth = 30;
    52. GridWidth = (float)(600 - 2 * BorderWidth) / (BoardSize - 1);
    53.  
    54. QPen LinePen(Qt::black);
    55. LinePen.setWidth(LineWidth);
    56. LinePen.setCapStyle(Qt::SquareCap);
    57.  
    58. for(int i = 0; i < BoardSize; i++)
    59. {
    60. scene->addLine(BorderWidth - 300 + (i * GridWidth), BorderWidth - 300, BorderWidth - 300 + (i * GridWidth), 300 - BorderWidth, LinePen); //paint vertical lines
    61. scene->addLine(BorderWidth - 300, BorderWidth - 300 + (i * GridWidth), 300 - BorderWidth, BorderWidth - 300 + (i * GridWidth), LinePen); //paint horizontal lines
    62. }
    63. }
    64. }
    65.  
    66. void MainWindow::GameStarted()
    67. {
    68. if(!theGameLogic.StartGame())
    69. QMessageBox::warning(this, "Error", "Cannot start game. Game already started!");
    70.  
    71. else
    72. {
    73. // Allocation of Board //
    74.  
    75. circle = new MouseoverCircle**[ BoardSize ];
    76.  
    77. for( int Column = 0; Column < BoardSize; Column++ )
    78. {
    79. circle[ Column ] = new MouseoverCircle*[ BoardSize ];
    80.  
    81. for( int Row = 0; Row < BoardSize; Row++ )
    82. {
    83. circle[ Column ][ Row ] = new MouseoverCircle( Column, Row );
    84. scene->addItem(circle[ Column ][ Row ]);
    85. circle[ Column ][ Row ]->setPos(-300 + BorderWidth + GridWidth * Column, -300 + BorderWidth + GridWidth * Row);
    86. connect(circle[Column][Row], SIGNAL(Mouseover(int,int)), this, SLOT(Mouseover(int,int)));
    87. connect(circle[Column][Row], SIGNAL(Clicked(int,int)), this, SLOT(Clicked(int,int)));
    88. }
    89. }
    90. }
    91. }
    92.  
    93. void MainWindow::Mouseover(int Column, int Row)
    94. {
    95. switch (theGameLogic.LegalTurn(Column, Row))
    96. {
    97. case 0:
    98. {
    99. circle[Column][Row]->Color = QColor(0, 0, 0, 0);
    100. }
    101. case 1:
    102. {
    103. circle[Column][Row]->Color = QColor(0, 0, 0, 127);
    104. break;
    105. }
    106. case 2:
    107. {
    108. circle[Column][Row]->Color = QColor(255, 255, 255, 127);
    109. break;
    110. }
    111. }
    112. }
    113.  
    114. void MainWindow::Clicked(int Column, int Row)
    115. {
    116. theGameLogic.ExecuteTurn(Column, Row);
    117. }
    118.  
    119. void MainWindow::FieldChanged(int Column, int Row, int NewValue)
    120. {
    121. QPen *RPen;
    122. QBrush *RBrush;
    123. switch(NewValue)
    124. {
    125. case 0:
    126. {
    127. RPen = new QPen(QColor(127, 127, 127));
    128. RBrush = new QBrush(QColor(127, 127, 127));
    129. break;
    130. }
    131. case 1:
    132. {
    133. RPen = new QPen(QColor(0, 0, 0));
    134. RBrush = new QBrush(QColor(0, 0, 0));
    135. break;
    136. }
    137. case 2:
    138. {
    139. RPen = new QPen(QColor(255, 255, 255));
    140. RBrush = new QBrush(QColor(255, 255, 255));
    141. break;
    142. }
    143. default:
    144. {
    145. RPen = new QPen(QColor(255, 0, 0));
    146. RBrush = new QBrush(QColor(255, 0, 0));
    147. break;
    148. }
    149. }
    150. scene->addEllipse(-325 + BorderWidth + GridWidth * Column, -325 + BorderWidth + GridWidth * Row, 50, 50, *RPen, *RBrush);
    151. }
    To copy to clipboard, switch view to plain text mode 

    newgamewindow.cpp:
    Qt Code:
    1. #include "Interface/newgamewindow.h"
    2. #include "ui_newgamewindow.h"
    3.  
    4. #include <QtGui>
    5. #include <QDebug>
    6.  
    7. NewGameWindow::NewGameWindow(QWidget *parent) :
    8. QDialog(parent),
    9. ui(new Ui::NewGameWindow)
    10. {
    11. ui->setupUi(this);
    12. }
    13.  
    14. NewGameWindow::~NewGameWindow()
    15. {
    16. delete ui;
    17. }
    18.  
    19. void NewGameWindow::on_pushButton_clicked()
    20. {
    21. if(ui->radioButton->isChecked())
    22. //QMessageBox::information(this, "board size set", "board size set to 9x9");
    23. emit BoardSizeSet(9);
    24. if(ui->radioButton_2->isChecked())
    25. //QMessageBox::information(this, "board size set", "board size set to 13x13");
    26. emit BoardSizeSet(13);
    27. if(ui->radioButton_3->isChecked())
    28. //QMessageBox::information(this, "board size set", "board size set to 19x19");
    29. emit BoardSizeSet(19);
    30. emit GameStarted();
    31. qDebug() << "test";
    32. this->close();
    33. }
    To copy to clipboard, switch view to plain text mode 

    if you need further information, just ask. Thank you for your help!

    EDIT: I figured out, the segmentation faults were coming from delete-commands in my model. After i removed them from the code, I now get segmentation faults when a mouseover over a circle occurs.

    EDIT2: Okay, i removed one line to much, so one pointer was set wront. The problem is now solved.
    Last edited by realJohnDoe; 7th March 2012 at 15:16.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,479
    Thanks
    252
    Thanked 709 Times in 699 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Program runs fine in Debug mode, but produces segmentation fault in Release Mode

    In your FieldChanged() method, it is not necessary to create new QPen and QBrush instances using "new", since you are passing them as const references into the scene->addEllipse() method. As it stands now, you have a memory leak every time the FieldChanged method is executed because you new up the pen and brush and never delete them..

    Simply declare QPen and QBrush instances on the stack and pass them in as is. Rewrite the code as follows:

    Qt Code:
    1. void MainWindow::FieldChanged(int Column, int Row, int NewValue)
    2. {
    3. QColor penColor;
    4. QColor brushColor;
    5. switch(NewValue)
    6. {
    7. case 0:
    8. {
    9. penColor = QColor(127, 127, 127);
    10. brushColor = QColor(127, 127, 127);
    11. break;
    12. }
    13. case 1:
    14. {
    15. penColor = QColor(0, 0, 0);
    16. brushColor = QColor(0, 0, 0);
    17. break;
    18. }
    19. case 2:
    20. {
    21. penColor = QColor(255, 255, 255);
    22. brushColor = QColor(255, 255, 255);
    23. break;
    24. }
    25. default:
    26. {
    27. penColor = QColor(255, 0, 0);
    28. brushColor = QColor(255, 0, 0);
    29. break;
    30. }
    31. }
    32. scene->addEllipse(-325 + BorderWidth + GridWidth * Column, -325 + BorderWidth + GridWidth * Row, 50, 50, QPen( penColor ), QBrush( brushColor ) );
    33. }
    To copy to clipboard, switch view to plain text mode 

    And regarding QWidget (QObject) ownership: As a general rule, any time you new a QObject, then give it to another QObject that will be its parent, the parent takes control of the lifetime of the object and you do not have to call delete on it. Likewise, calling setupUI on an embedded UI pointer passes ownership of that to the widget for which the ui is being created, and the QObject ownership mechanism will take care of deleting the pointer when the widget goes out of scope. Double deletes will cause the crashes you experienced, and it's a clear sign that you either are trying to use a pointer that hasn't been initialized or you're trying to delete one that's already been deleted.

    Any other type of pointer (like your own C++ objects) follow the normal C++ rules: if you new it, you must delete it.

Similar Threads

  1. Replies: 5
    Last Post: 14th April 2011, 19:10
  2. Replies: 4
    Last Post: 27th May 2010, 15:18
  3. Replies: 1
    Last Post: 2nd November 2009, 12:02
  4. Replies: 9
    Last Post: 15th April 2009, 06:23
  5. Replies: 8
    Last Post: 10th October 2007, 18:20

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.