Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Visual Studio and Example Program = Debug Assertion Failed

[SOLVED] Visual Studio and Example Program = Debug Assertion Failed

Scheduled Pinned Locked Moved General and Desktop
8 Posts 4 Posters 7.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    eidgeare
    wrote on last edited by
    #1

    Hello guys,

    I've just built qt wi vs 2010 pro. Everything seems to be running fine except that when I run this exemple:

    @#include <QtGui>
    #include <qtextedit.h>
    int main(int argv, char **args)
    {
    QApplication app(argv, args);

    QTextEdit textEdit;
    QPushButton quitButton("Quit");

    QObject::connect(&quitButton, SIGNAL(clicked()), qApp, SLOT(quit()));

    QVBoxLayout layout;
    layout.addWidget(&textEdit);
    layout.addWidget(&quitButton);

    QWidget window;
    window.setLayout(&layout);

    window.show();
    return app.exec();
    }@

    As soon as I hit quitButton the debugger kicks in with an Debug Assertion Failed.

    Anyone had this problem?

    Thanks,

    Hugo Ribeira

    1 Reply Last reply
    0
    • V Offline
      V Offline
      veeeee_d
      wrote on last edited by
      #2

      That problem occurs due to a memory cleanup mess-up by your compiler. Qt encourages you to always allocate any QWidget and QWidget-derived object (QPushButton, QTextEdit, etc.) dynamically, with operator new.

      @#include <QtGui>

      int main(int argv, char **args)
      {
      QApplication app(argv, args);

      QTextEdit* textEdit = new QTextEdit();
      QPushButton* quitButton = new QPushButton("Quit");

      QObject::connect(quitButton, SIGNAL(clicked()), qApp, SLOT(quit()));

      QVBoxLayout layout;
      layout.addWidget(textEdit);
      layout.addWidget(quitButton);

      QWidget* window = new QWidget();
      window->setLayout(&layout);

      window->show();
      return app.exec();
      }@

      I removed the line

      @#include <qtextedit.h>@

      since QtGui includes all of the GUI's classes.

      1 Reply Last reply
      0
      • G Offline
        G Offline
        giesbert
        wrote on last edited by
        #3

        [quote author="veeeee_d" date="1323054904"]That problem occurs due to a memory cleanup mess-up by your compiler. Qt encourages you to always allocate any QWidget and QWidget-derived object (QPushButton, QTextEdit, etc.) dynamically, with operator new.
        [/quote]

        This is not 100% correct, so one correction:

        Qt cleans up all child elements of any QObject explicitly with delete when the parent object is deleted. So, top levvel objects are not cleand up, in you code, that would be

        @
        QWidget* window = new QWidget();
        @

        so the correct code would be:

        @
        #include <QtGui>

        int main(int argv, char **args)
        {
        QApplication app(argv, args);

        QWidget window;
        
        QTextEdit* textEdit = new QTextEdit(&window);
        QPushButton* quitButton = new QPushButton("Quit", &window);
        
        QObject::connect(quitButton, SIGNAL(clicked()), qApp, SLOT(quit()));
        
        QVBoxLayout layout;
        window->setLayout(&layout);
        layout.addWidget(textEdit);
        layout.addWidget(quitButton);
        
        window->show();
        return app.exec();
        

        }
        @

        I also suggest to create the parent before adding the widgets to the layout, and to give the parent in the C#tor as it makes it clearer, where an object lives.

        Nokia Certified Qt Specialist.
        Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

        1 Reply Last reply
        0
        • G Offline
          G Offline
          goetz
          wrote on last edited by
          #4

          See the "DocNote":http://developer.qt.nokia.com/doc/qt-4.7/gettingstartedqt.html#note-22 in the DevNet version of that page for corrections and other missing code of that guide.

          http://www.catb.org/~esr/faqs/smart-questions.html

          1 Reply Last reply
          0
          • V Offline
            V Offline
            veeeee_d
            wrote on last edited by
            #5

            Thanks for the correction and the note, good to know.

            1 Reply Last reply
            0
            • E Offline
              E Offline
              eidgeare
              wrote on last edited by
              #6

              I'm pretty new (as in the newest possible) in qt programming, that being said:

              Using new to allocate the widgets and not using delete to clean them up won't result in memory loss?

              Thanks,

              Hugo Ribeira

              1 Reply Last reply
              0
              • G Offline
                G Offline
                goetz
                wrote on last edited by
                #7

                @eidgeare

                The story is a bit more detailed:

                In principle you must delete a widget that you have created with new. There is an exception to this rule: If you create a widget with a parent (or put it in a layout, which will give it a parent too), you do not need to delete it manually, as the parent widget deletes its children on its own destruction. So, in practice you will usually only need to delete the toplevel widget (dialog, mainwindow, etc)

                http://www.catb.org/~esr/faqs/smart-questions.html

                1 Reply Last reply
                0
                • E Offline
                  E Offline
                  eidgeare
                  wrote on last edited by
                  #8

                  Understood,

                  Thank you very much,

                  Hugo Ribeira

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved