Results 1 to 6 of 6

Thread: Getting Started Programming with Qt tutorial part2 giving assertion fail

  1. #1
    Join Date
    Aug 2010
    Posts
    7
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Getting Started Programming with Qt tutorial part2 giving assertion fail

    I am attempting the tutorial "part2" on http://doc.qt.nokia.com/4.7/gettingstartedqt.html. part1 successfully compiled and ran.

    part2, however, is giving me an assertion fail when I exit it.
    Qt Code:
    1. File: f:\dd\vctools\crt_bld\self_x86\crt\src\dbgdel.cpp
    2. Line: 52
    3.  
    4. Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)
    To copy to clipboard, switch view to plain text mode 

    This is when I clicked the red X, click the Quit button (part of the tutorial), or Alt-F4.

    Here is the tutorial code:
    Qt Code:
    1. #include <QtGui>
    2.  
    3. int main(int argv, char **args)
    4. {
    5. QApplication app(argv, args);
    6.  
    7. QTextEdit textEdit;
    8. QPushButton quitButton("Quit");
    9.  
    10. QObject::connect(&quitButton, SIGNAL(clicked()), qApp, SLOT(quit()));
    11.  
    12. QVBoxLayout layout;
    13. layout.addWidget(&textEdit);
    14. layout.addWidget(&quitButton);
    15.  
    16. QWidget window;
    17. window.setLayout(&layout);
    18.  
    19. window.show();
    20.  
    21. return app.exec();
    22. }
    To copy to clipboard, switch view to plain text mode 

    I've noticed that if the layout.addWidget(... lines are commented out, it does not produce an assertion error.

    What could be causing this to happen?
    Thanks.

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Getting Started Programming with Qt tutorial part2 giving assertion fail

    You will probably learn to allocate widget on the heap anyway.
    You get the error because of the order in which the stack allocated objects get destructed when they get out of scope.

    So you can do one of the following solutions:
    1)
    Qt Code:
    1. #include <QtGui>
    2.  
    3. int main(int argv, char **args)
    4. {
    5. QApplication app(argv, args);
    6.  
    7. QWidget window; //create the window QWidget first, than the rest
    8. QTextEdit textEdit;
    9. QPushButton quitButton("Quit");
    10. //... rest of the example, obviously don't let two copy of the: QWidget window; line
    To copy to clipboard, switch view to plain text mode 

    2) This is the recommended solution: Learn to use widgets on the heap, use pointers, allocate memory with new, and Qt parent-child relationship.
    Qt Code:
    1. #include <QtGui>
    2.  
    3. int main(int argv, char **args)
    4. {
    5. QApplication app(argv, args);
    6.  
    7. QWidget* window = new QWidget; // this is how you allocate on the heap
    8. //the main window/widget can be allocated on the stack
    9.  
    10.  
    11. QTextEdit *textEdit = new QTextEdit; //
    12. QPushButton *quitButton = new QPushButton("Quit");
    13.  
    14. QObject::connect(quitButton, SIGNAL(clicked()), qApp, SLOT(quit())); //note that you just write the name without & operator
    15.  
    16. QVBoxLayout *layout = new QVBoxLayout;
    17. layout->addWidget(textEdit); // -> this is the operator to access members if use pointers
    18. layout->addWidget(quitButton);
    19.  
    20. window->setLayout(layout); //again without &, and all the widgets added to layout are child of window
    21.  
    22.  
    23. window->show();
    24.  
    25. int r = app.exec(); //this is the proper way to free resources if you allocate the main window/widget om the heap
    26.  
    27. delete window;//here you free the memory for QWidget window, this will automatically delete the childs of window
    28.  
    29. return r;
    30. }
    To copy to clipboard, switch view to plain text mode 

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

    jetimms (29th September 2010)

  4. #3
    Join Date
    Aug 2010
    Posts
    7
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Getting Started Programming with Qt tutorial part2 giving assertion fail

    Interesting!

    I haven't seen anything using pointers suggested yet, but I haven't been doing this long. Why the heap instead of the stack, though? Is it to allow the programmer more control of what's using memory?

    So, the "parent" object, the window QWidget in this example, needs to be defined and instantiated first?

    I'm kind of a completist, so would it be okay to delete everything myself, instead of letting "delete window" propagate them?

    Thanks very much for the info!

  5. #4
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Getting Started Programming with Qt tutorial part2 giving assertion fail

    Quote Originally Posted by jetimms View Post
    I haven't seen anything using pointers suggested yet, but I haven't been doing this long. Why the heap instead of the stack, though? Is it to allow the programmer more control of what's using memory?
    Than i really suggest you take a least a C++ tutorial before continuing with Qt, you are going to extensivelly use pointers and object oriented programming.

    A book is more recommended, you can search for Thinking in C++ written by Bruce Eckel, there are two volumes, free to download.
    Quote Originally Posted by jetimms View Post
    So, the "parent" object, the window QWidget in this example, needs to be defined and instantiated first?

    I'm kind of a completist, so would it be okay to delete everything myself, instead of letting "delete window" propagate them?
    The parent child relationship is the Qt way of simplyfing the C++ memory management, so you will need to understand that too, but first "play" a little with pointers and C++ (i really recommend at least the first volume of Thinking in C++), after you are comfortable with basic C++, pointers and OOP, Qt will be a really simple framework.

  6. #5
    Join Date
    Aug 2010
    Posts
    7
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Getting Started Programming with Qt tutorial part2 giving assertion fail

    I completed the Koenig & Moo C++ book sometime in 2008, so I am familiar with pointers and iterators on a intermediate-amateur level.

    What I meant to say is I haven't seen any Qt references that uses pointers, however I haven't delved too far down into the Qt demo or example code.

    I am frankly relieved to see pointers in use. Without them, the beginner Qt code was starting to look a little too much like Visual Basic.

    Thanks for your explanation. I can't wait to get back home tonight and try it out!
    Last edited by jetimms; 29th September 2010 at 13:59.

  7. #6
    Join Date
    Aug 2010
    Posts
    7
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Getting Started Programming with Qt tutorial part2 giving assertion fail

    It works!

    Actually I tried moving the "QWidget window" declaration and then implemented the program using pointers. Both methods worked, of course.

    Just as a note, later in the tutorial it recommended using the heap to allocate.

Similar Threads

  1. Replies: 5
    Last Post: 19th September 2008, 15:24
  2. Getting started learning network programming
    By as001622 in forum Qt Programming
    Replies: 1
    Last Post: 20th June 2008, 13:02
  3. Debug Assertion Failed
    By ^NyAw^ in forum General Programming
    Replies: 5
    Last Post: 28th December 2007, 11:48
  4. setPixmap cause assertion?
    By Equilibrium in forum Qt Programming
    Replies: 3
    Last Post: 23rd October 2007, 00:34
  5. QGLWIdget renderText() assertion...
    By adonel in forum Qt Programming
    Replies: 3
    Last Post: 22nd October 2007, 21:30

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.