Results 1 to 6 of 6

Thread: Dialog executes in debug build but not release

  1. #1
    Join Date
    Jun 2008
    Location
    Glenwood, NJ USA
    Posts
    32
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Dialog executes in debug build but not release

    My apologies if this question is better suited in the newbie forum. Please let me know. I have a dialog that I am creating that works fine in debug mode but in release mode it locks up. I'm not really sure why but I suspect a pointer to Null somewhere. Any ideas?

    Qt Code:
    1. /*
    2.  * newnetworkdialog.h
    3.  * bkupsim
    4.  *
    5.  * Created by Andrew White on 6/23/08.
    6.  * Copyright 2008 __MyCompanyName__. All rights reserved.
    7.  *
    8.  */
    9.  
    10. #ifndef NEWNETWORKDIALOG_H
    11. #define NEWNETWORKDIALOG_H
    12.  
    13. #include<QDialog>
    14.  
    15. class QFormLayout;
    16. class QLineEdit;
    17. class QComboBox;
    18.  
    19. class NewNetworkDialog : public QDialog {
    20. Q_OBJECT
    21. public:
    22. NewNetworkDialog(QWidget* parent = 0);
    23. signals:
    24.  
    25. private slots:
    26. void accept();
    27. void reject();
    28.  
    29. private:
    30. QLineEdit* nameLineEdit;
    31. QLineEdit* ipLineEdit;
    32. QLineEdit* netmaskLineEdit;
    33. QComboBox* thruputComboBox;
    34. QFormLayout* formLayout;
    35. QDialogButtonBox* okCancelButtonBox;
    36.  
    37. };
    38.  
    39. #endif
    40. /*
    41.  * newnetworkdialog.cpp
    42.  * bkupsim
    43.  *
    44.  * Created by Andrew White on 6/23/08.
    45.  * Copyright 2008 __MyCompanyName__. All rights reserved.
    46.  *
    47.  */
    48.  
    49. #include "newnetworkdialog.h"
    50. #include<QtGui>
    51.  
    52. NewNetworkDialog::NewNetworkDialog(QWidget* parent) :
    53. QDialog(parent) {
    54.  
    55. nameLineEdit = new QLineEdit;
    56. ipLineEdit = new QLineEdit;
    57. netmaskLineEdit = new QLineEdit;
    58. thruputComboBox = new QComboBox;
    59. formLayout = new QFormLayout;
    60. formLayout->addRow(tr("Name:"), nameLineEdit);
    61. formLayout->addRow(tr("IP Address:"), ipLineEdit);
    62. formLayout->addRow(tr("Netmask:"), netmaskLineEdit);
    63. formLayout->addRow(tr("Thruput Type:"), thruputComboBox);
    64. thruputComboBox->addItem(tr("10-B-T"), 1000);
    65. thruputComboBox->addItem(tr("100-B-T"), 10000);
    66. thruputComboBox->addItem(tr("1000-B-T"), 100000);
    67.  
    68.  
    69. okCancelButtonBox = new QDialogButtonBox(QDialogButtonBox::Ok
    70. | QDialogButtonBox::Cancel);
    71.  
    72. formLayout->addRow(okCancelButtonBox);
    73. setLayout(formLayout);
    74. connect(okCancelButtonBox, SIGNAL(accepted()), this, SLOT(accept()));
    75. connect(okCancelButtonBox, SIGNAL(rejected()), this, SLOT(reject()));
    76. }
    77.  
    78. void NewNetworkDialog::accept() {
    79. QDialog::accept();
    80. }
    81.  
    82. void NewNetworkDialog::reject() {
    83. QDialog::reject();
    84. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Dialog executes in debug build but not release

    Does it lock up even if you use the dialog like this:
    Qt Code:
    1. // main.cpp
    2. #include <QApplication>
    3. #include "newnetworkdialog.h"
    4.  
    5. int main(int argc, char* argv[])
    6. {
    7. QApplication app(argc, argv);
    8. NewNetworkDialog dialog;
    9. return dialog.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 
    The idea is to use the dialog alone and nothing else.
    J-P Nurmi

  3. #3
    Join Date
    Jun 2008
    Location
    Glenwood, NJ USA
    Posts
    32
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: Dialog executes in debug build but not release

    That works without a problem. So it might be the way I am instantiating the dialog:

    Qt Code:
    1. dialog->show();
    2. dialog->raise();
    3. dialog->activateWindow();
    To copy to clipboard, switch view to plain text mode 

    I call this through a slot declared in my main window as a result of a signal from a menu option.
    Last edited by jpn; 24th June 2008 at 15:22. Reason: missing [code] tags

  4. #4
    Join Date
    Jun 2008
    Location
    Glenwood, NJ USA
    Posts
    32
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: Dialog executes in debug build but not release

    I found the problem. When I call the slot:

    Qt Code:
    1. void MainWindow::newNetwork() {
    2. if(!newnet)
    3. newnet = new NewNetworkDialog(this);
    4. newnet->exec();
    5. }
    To copy to clipboard, switch view to plain text mode 

    I declared it in newnetworkdialog.h as:

    Qt Code:
    1. QDialog* newnet;
    To copy to clipboard, switch view to plain text mode 

    I wrongly assumed it would initialize to zero. By adding to the MainWindow constructor:

    Qt Code:
    1. newnet = 0;
    To copy to clipboard, switch view to plain text mode 

    It works now. My apologies as this is a basic C++ issue. But my question is why did it work in debug mode and would there be a better way to initialize the newnet to point to null?
    Last edited by jpn; 24th June 2008 at 15:22. Reason: missing [code] tags

  5. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Dialog executes in debug build but not release

    Without getting bogged down into details, let's just say that compilers tend to optimize things in release mode. You should always initialize pointers because you cannot rely on that they would be initialized to any sane value like it might be the case in debug mode with some compilers. More details: Surviving the Release Version.

    The correct way to initialize member variables in C++ is to use the initializer list:
    Qt Code:
    1. SomeClass::SomeClass(/* args */)
    2. : BaseClass(/* args */), memberPtr(0), memberVar(123, 456) // <--
    3. {
    4. }
    To copy to clipboard, switch view to plain text mode 
    One might argue about the readability versus:
    Qt Code:
    1. SomeClass::SomeClass(/* args */)
    2. : BaseClass(/* args */)
    3. {
    4. memberPtr = 0;
    5. memberVar = SomeType(123, 456);
    6. }
    To copy to clipboard, switch view to plain text mode 
    but the fact is that in the latter method the member object gets constructed twice: 1) by using it's default constructor and 2) in the constructor body. You might not want to do that at least in case of complex types.
    J-P Nurmi

  6. #6
    Join Date
    Jun 2008
    Location
    Glenwood, NJ USA
    Posts
    32
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: Dialog executes in debug build but not release

    Thanks JPN.

Similar Threads

  1. build debug with shared and release with static?
    By Thor28 in forum Qt Programming
    Replies: 4
    Last Post: 14th April 2008, 22:32
  2. Need debug and release versions of Qwt?
    By Tiansen in forum Qwt
    Replies: 1
    Last Post: 28th March 2008, 07:55
  3. Some very weird compilation warnings
    By MarkoSan in forum Qt Programming
    Replies: 21
    Last Post: 23rd January 2008, 16:48
  4. Replies: 2
    Last Post: 8th November 2007, 20:15
  5. Adding custom defines when on debug build
    By chus in forum Qt Programming
    Replies: 2
    Last Post: 2nd March 2007, 11:38

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.