Results 1 to 4 of 4

Thread: Critical error detected c0000374 - on close MainWindow

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,330
    Thanks
    317
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Critical error detected c0000374 - on close MainWindow

    2b)I'm confused about:
    OK, I will explain. The reason your code is crashing is because you have declared your member variable widgets directly, as QWidget instances, rather than as pointers to QWidget instances. Memory is being corrupted in the MainWindow destructor because the member variables are destroyed by C++ before the MainWindow instance is destroyed. However, because in Qt, these child widgets are owned by the MainWindow, Qt is trying to destroy them a second time (through the Qt parent-child relationship, which is responsible for destroying them), after C++ has already destroyed them. The memory they used to occupy is gone, and so you get a crash because Qt is trying to access an object that no longer exists. (It could be happening the other way around - Qt destroys them first, and then C++ tries to do it again, but it's the same effect).

    This has nothing to do with arguments about whether it is better to use heap vs. stack in any particular situation. It is entirely about Qt's parent-child ownership hierarchy of QObjects and who is in charge of the child objects' lifetimes.

    So the solution to your problem is pretty simple:

    1. Change every one of your QWidget-based member variables in MainWindow to "QWidget *".
    2. In the constructor for MainWindow, call the "new" operator to create the QWidget instances
    3. Use these widgets through the pointer operator (->)

    Qt Code:
    1. // MainWindow.h
    2.  
    3. // Change #1:
    4. private:
    5. GlWidget * glWidgetWindow;
    6. BuiltSphere * built_sphere_window;
    7. BuiltCylinder * built_cylinder_window;
    8. EditObject * edit_object_window;
    9.  
    10. QComboBox * builder_select;
    11. QStackedWidget * builder_stacked_widget;
    12. QTabWidget * tab_widget_01;
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. // MainWindow.cpp:
    2.  
    3. MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
    4. {
    5. // Change #2:
    6. glWidgetWindow = new GlWidget( this );
    7. built_sphere_window = new BuiltSphere( this );
    8. // etc.
    9.  
    10. // Change #3:
    11. glWidgetWindow->setFixedSize( 1024, 768 );
    12. // etc.
    To copy to clipboard, switch view to plain text mode 

    In general, you should never declare QWidget-based member variables as QWidget, but always as QWidget *. (Actually, anything QObject-based).

    The only places you should use QWidget directly are these (and maybe a few other similar cases):

    1. In a method where the QWidget is in scope only for the life of the method, such as posting a dialog to get information from the user.

    Qt Code:
    1. void MainWindow::showTheDialog()
    2. {
    3. QDialog theDialog;
    4. theDialog->exec();
    5. }
    To copy to clipboard, switch view to plain text mode 

    2. For MainWindow in main.cpp:

    Qt Code:
    1. int main( int argc, char * argv[] )
    2. {
    3. QApplication a( argc, argv );
    4. MainWindow w;
    5. w.show();
    6. return a.exec();
    7. }
    To copy to clipboard, switch view to plain text mode 

    In both of these cases, the QObject instances are completely contained within the scope of the method so it is safe to create them on the stack.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  2. The following user says thank you to d_stranz for this useful post:

    andreaQt (23rd October 2020)

Similar Threads

  1. Critical/Fatal error detection
    By JasonKretzer in forum Qt Programming
    Replies: 0
    Last Post: 24th June 2014, 15:11
  2. Replies: 7
    Last Post: 14th August 2013, 16:02
  3. Replies: 1
    Last Post: 11th June 2012, 01:23
  4. QT application not close after close the mainwindow
    By artome in forum Qt Programming
    Replies: 1
    Last Post: 22nd July 2011, 22:23
  5. Error: **** glibc detected ****
    By Manohar in forum Qt Programming
    Replies: 2
    Last Post: 9th May 2008, 05:32

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
  •  
Qt is a trademark of The Qt Company.