Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Qt Academy Launch in California!

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

    General and Desktop
    4
    8
    6951
    Loading More Posts
    • 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
      eidgeare last edited by

      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 Reply Quote 0
      • V
        veeeee_d last edited by

        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 Reply Quote 0
        • G
          giesbert last edited by

          [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 Reply Quote 0
          • G
            goetz last edited by

            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 Reply Quote 0
            • V
              veeeee_d last edited by

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

              1 Reply Last reply Reply Quote 0
              • E
                eidgeare last edited by

                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 Reply Quote 0
                • G
                  goetz last edited by

                  @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 Reply Quote 0
                  • E
                    eidgeare last edited by

                    Understood,

                    Thank you very much,

                    Hugo Ribeira

                    1 Reply Last reply Reply Quote 0
                    • First post
                      Last post