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
    Mar 2018
    Posts
    34
    Thanks
    18
    Qt products
    Qt5
    Platforms
    Windows

    Default Critical error detected c0000374 - on close MainWindow

    When i try to test the code, the program do the job, only at quit (by pressing X on the MainWindow) i have this issue:
    in the "Application Ouput" window is write the current message "Critical error detected c0000374" and the debugger enter in editobject.h file with the yellow arrow at line 6;
    what wrong i do?
    Attached Files Attached Files

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Critical error detected c0000374 - on close MainWindow

    And did you ask Google what this error message means? This hit seems like it might be particularly relevant. (Not the specific error the poster experienced, but what the error means). And no, it isn't a bug in Qt.

    Hint: Look at how you have declared your QWidget-based variables in MainWindow.h. Why do all other Qt applications create child windows on the heap and not the stack (as your code does)? And how do you think that might result in memory corruption when your MainWindow is closed (and deleted)?
    Last edited by d_stranz; 21st October 2020 at 21:47.
    <=== 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.

  3. #3
    Join Date
    Mar 2018
    Posts
    34
    Thanks
    18
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Critical error detected c0000374 - on close MainWindow

    I try to answer:
    1a) In Qt app is better to create child window or object on the heap because on close window or delete parent object all childs are deleted.
    1a) On the stack is better to store small size data object and with contest access (like functions or slots), in the heap large size data object and without constest access.
    2a) Like i read "Whether or not the variables are on the heap or stack depends upon how the object is allocated. So all members of the class will be allocated how the object is allocated." in https://forum.qt.io/topic/108617/cla...-stack-or-heap so: MainWindow is allocated on the stack, like in my code glWidgetWindow+built_sphere_window+built_cylinder_ window+and so on (from Mainwindow.h) are allocated on the stack, what it is happen is an overflow of the stack?
    2b)I'm confused about:
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
    2. {
    3. QLabel test_stack_label(tr("Stored in Stack")); // this label is stored in the stack like MainWindow
    4. QLabel test_heap_label(tr("Stored in Heap")); // this label is stored in the heap or is stored in the stack like its parent MainWindow
    5. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 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.

  5. 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, 16:11
  2. Replies: 7
    Last Post: 14th August 2013, 17:02
  3. Replies: 1
    Last Post: 11th June 2012, 02:23
  4. QT application not close after close the mainwindow
    By artome in forum Qt Programming
    Replies: 1
    Last Post: 22nd July 2011, 23:23
  5. Error: **** glibc detected ****
    By Manohar in forum Qt Programming
    Replies: 2
    Last Post: 9th May 2008, 06: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.