Results 1 to 4 of 4

Thread: Crashing without qDebug() ?

  1. #1
    Join Date
    Jul 2008
    Posts
    7
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Question Crashing without qDebug() ?

    Hello,
    In my code I am calling a void-function which creates some variables. However it will crash if I use it like this:
    Qt Code:
    1. Boxes();
    2. emod.verticalLayout->addWidget(boxes[0]);
    3. emod.verticalLayout->addWidget(boxes[1]);
    4. emod.verticalLayout->addWidget(boxes[2]);
    5. emod.verticalLayout->addWidget(boxes[3]);
    6. emod.verticalLayout->addWidget(boxes[4]);
    To copy to clipboard, switch view to plain text mode 
    If I use this:
    Qt Code:
    1. qDebug() << "a1";
    2. Boxes();
    3. qDebug() << "a2";
    4. emod.verticalLayout->addWidget(boxes[0]);
    5. emod.verticalLayout->addWidget(boxes[1]);
    6. emod.verticalLayout->addWidget(boxes[2]);
    7. emod.verticalLayout->addWidget(boxes[3]);
    8. emod.verticalLayout->addWidget(boxes[4]);
    To copy to clipboard, switch view to plain text mode 
    It does not crash.

    Function boxes() in the header:
    Qt Code:
    1. public:
    2. void Boxes();
    To copy to clipboard, switch view to plain text mode 

    the code of boxes() is:
    Qt Code:
    1. void TableEditor::Boxes(){
    2. boxes.clear();
    3. int x;
    4. for(int i=0;i<15;i++){ //create 15 qcheckboxes;
    5. QCheckBox *temp = new QCheckBox(Dialog);
    6. temp->setObjectName("");
    7.  
    8. if(x < mod.size()){
    9. qDebug() << "added text" << mod[x];
    10. temp->setText(mod[x]);
    11. temp->setVisible(true);
    12. x++;
    13. }else{
    14. temp->setText("");
    15. temp->setVisible(true);
    16. }
    17.  
    18. boxes.append(temp);
    19. }
    20. }
    To copy to clipboard, switch view to plain text mode 
    boxes is a QLIST<QCheckbox*>. and mod is a QLIST<QString>.
    What can I do to solve this. I do not want to use qDebug() to fix the function. Could somebody tell me what is wrong and how I can solve this?
    any help would be appreciated.

  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: Crashing without qDebug() ?

    You forgot to initialize "int x".
    J-P Nurmi

  3. The following user says thank you to jpn for this useful post:

    xtreme (5th August 2008)

  4. #3
    Join Date
    Jul 2008
    Posts
    7
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Crashing without qDebug() ?

    Thanks, it seems to be the solution. I always thought when defining a standart variable like a int that there was no need to add =0 to it. Still a bit odd that when using qDebug() it does not crash.

  5. #4
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Crashing without qDebug() ?

    Welcome to the C/C++ joys of undefined behaviour.

    Your variable x was not initialized. Its value therefore 'random', which means: whatever value the stack had at the location reserved for x.
    Calling qDebug() seems to have had a side effect of leaving some value >= 0 at that position. (Pure coincidence...).
    Without qDebug() the value happend (pure good luck again) to be negative (I guess) which lead to a crash in accessing mod[x].

    You can and should not depend on something like that ;-)
    Your compiler should have printed a warning for the forgotten initialization. Try to fix all warnings. Bad bugs tend to hide behind and among them. It takes way less time to fix the warnings than to fix the bugs hiding there afterwards.
    And code that compiles with lots of warnings give an unpleasant feeling and lowers trust in that code. (I would not want to deliver code that compiles with thousands of warnings...)

    HTH

Similar Threads

  1. QStandardItemModel insertRow crashing
    By munna in forum Qt Programming
    Replies: 1
    Last Post: 27th June 2008, 11:55
  2. QPageSetupDialog is crashing always
    By senthilsp in forum Qt Programming
    Replies: 1
    Last Post: 29th November 2007, 10:21
  3. qDebug with/without MSVC
    By vonCZ in forum Newbie
    Replies: 2
    Last Post: 20th June 2007, 12:58
  4. qDebug() under Windows
    By jlbrd in forum Qt Programming
    Replies: 10
    Last Post: 20th March 2007, 07:10
  5. qDebug macro substitution
    By the_bis in forum Qt Programming
    Replies: 2
    Last Post: 15th November 2006, 08:31

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.