Results 1 to 5 of 5

Thread: Memory Trouble

  1. #1
    Join Date
    Feb 2006
    Posts
    2
    Qt products
    Qt3
    Platforms
    Unix/X11

    Angry Memory Trouble

    This is my first major project using Qt, and I have a bit of a problem.
    I subclassed QMainWindow for my program, and got the layout and menus to work.
    The layout is made of QSplitters and in the main view I have a QWidgetStack.
    I wanted to keep the widget stack id's in integer variables, so I added two variables in the class definition,and tried:

    widgetStackItemId=mainview->id(wigetPointer);

    And later used:

    mainview->raiseWidget(widgetStackItemId);

    Now, this worked with one of the variables, but as soon as I tried using another variable, it started giving me segmentation faults.
    At first, I thought it was related to the QWidgetStack, but it seems that as soon as I assign a value to the second variable (can be just a number), the program crashes with a seg fault.
    I think I made a mistake with a pointer, but I don't know which one. Here is a code based on my real code. I changed variable and function names, and shortened some boring bits (menu items). The code works like that, I think the bug is a matter of scale.

    Header (mymainwindow.h):


    Qt Code:
    1. #ifndef MYMAINWINDOW_H
    2. #define MYMAINWINDOW_H
    3.  
    4. #include <qmainwindow.h>
    5. #include <qmenubar.h>
    6. #include <qpopupmenu.h>
    7. #include <qsplitter.h>
    8. #include <qlistbox.h>
    9. #include <qtable.h>
    10. #include <qscrollview.h>
    11. #include <qwidgetstack.h>
    12. #include <qstringlist.h>
    13. #include <qtextedit.h>
    14.  
    15.  
    16. class MyMainWindow: public QMainWindow
    17. {
    18. Q_OBJECT
    19. public:
    20. MyMainWindow(QWidget *parent=0,const char *name=0);
    21. protected:
    22. //CloseEvent is called whenever the user closes the program's window.
    23. void CloseEvent(QCloseEvent *event);
    24.  
    25. private slots:
    26. bool mnuFileQuit();
    27. void mnuViewItem();
    28.  
    29. void leftPane1Changed();
    30. private:
    31.  
    32. void initLayout();
    33. void initMenus();
    34.  
    35. void createMainWidget1();
    36. void createMainWidget2();
    37.  
    38. QSplitter *mainsplitter;
    39. QSplitter *leftsplitter;
    40. QWidgetStack *mainview;
    41. QListBox *leftPane1;
    42. QListBox *leftPane2;
    43.  
    44. //id's in the widget stack:
    45. int widgetstackid1;
    46. int widgetstackid2;
    47.  
    48. //Now actual menu items (rather than their functions) are defined:
    49. QPopupMenu *fileMenu;
    50. QPopupMenu *viewMenu;
    51. };
    52. #endif
    To copy to clipboard, switch view to plain text mode 


    Implementation:

    Qt Code:
    1. #include "mymainwindow.h"
    2. #include <qapplication.h>
    3.  
    4. MyMainWindow::MyMainWindow(QWidget *parent,const char *name):QMainWindow(parent,name){
    5. widgetstackid1=0;
    6. widgetstackid2=0; //In the real program, this line causes a seg fault.
    7.  
    8. initLayout();
    9. initMenus();
    10. }
    11. void MyMainWindow::initLayout(){
    12. mainsplitter=new QSplitter(Qt::Horizontal,this);
    13. setCentralWidget(mainsplitter);
    14.  
    15. leftsplitter=new QSplitter(Qt::Vertical,mainsplitter);
    16. leftPane1=new QListBox(leftsplitter);
    17. leftPane1->insertItem("Item 0");
    18. leftPane1->insertItem("Item 1");
    19.  
    20. connect(leftPane1,SIGNAL(currentChanged ( QListBoxItem * )),this,SLOT(leftPane1Changed()));
    21. leftPane2=new QListBox(leftsplitter);
    22.  
    23. mainview=new QWidgetStack(mainsplitter);
    24.  
    25. createMainWidget1();
    26. createMainWidget2();
    27. }
    28.  
    29. void MyMainWindow::initMenus(){
    30. fileMenu=new QPopupMenu (this);
    31. viewMenu=new QPopupMenu(this);
    32.  
    33. fileMenu->insertItem("&Quit",this,SLOT(mnuFileQuit()));
    34.  
    35.  
    36. viewMenu->insertItem("View Menu Item",this,SLOT(mnuViewItem()));
    37.  
    38. menuBar()->insertItem("&File",fileMenu);
    39. menuBar()->insertItem("&View",viewMenu);
    40. }
    41.  
    42. void MyMainWindow::createMainWidget1(){
    43. QTable *mainWidget1Table = new QTable( 4, 2, mainview );
    44. mainWidget1Table->setLeftMargin(0);
    45. mainWidget1Table->setReadOnly(true);
    46. QStringList labels;
    47. labels.append("col 1");
    48. labels.append("col 2");
    49.  
    50. int i;
    51. for(i=0;i<2;i++){
    52. mainWidget1Table->setColumnWidth(i,60);
    53. }
    54. mainWidget1Table->setColumnLabels(labels);
    55. mainWidget1Table->setSorting(true);
    56.  
    57. mainview->addWidget(mainWidget1Table);
    58.  
    59. widgetstackid1=mainview->id(mainWidget1Table);
    60. }
    61.  
    62. void MyMainWindow::createMainWidget2(){
    63. QTextEdit *mainWidget2Text;
    64. mainWidget2Text=new QTextEdit(mainview);
    65. mainWidget2Text->setReadOnly(true);
    66. mainWidget2Text->setText("<h1>hello</h1>world.");
    67. mainview->addWidget(mainWidget2Text);
    68. widgetstackid2=mainview->id(mainWidget2Text); //In the real program, this line causes a seg fault.
    69. }
    70.  
    71. //Menu functions
    72. void MyMainWindow::mnuViewItem(){
    73. qWarning("mnuViewItem() called.");
    74. }
    75. bool MyMainWindow::mnuFileQuit(){
    76. return (close(true));
    77. }
    78.  
    79.  
    80. void MyMainWindow::leftPane1Changed(){
    81. int currentItem=leftPane1->currentItem();
    82. switch(currentItem){
    83. case 0:
    84. mainview->raiseWidget(widgetstackid1);
    85. break;
    86. case 1:
    87. mainview->raiseWidget(widgetstackid2);
    88. break;
    89. }
    90. }
    91.  
    92. //Main program
    93. int main(int argc,char *argv[]){
    94. QApplication app(argc,argv);
    95. MyMainWindow windowThing;
    96. app.setMainWidget(&windowThing);
    97. windowThing.show();
    98.  
    99. return app.exec();
    100. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jacek; 5th February 2006 at 21:11. Reason: fixed code tags

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Memory Trouble

    It does not crash on my system. Try running "make clean && make.

    The only problem is with this method:
    Qt Code:
    1. bool MyMainWindow::mnuFileQuit(){
    2. return (close(true));
    3. }
    To copy to clipboard, switch view to plain text mode 
    You tell the widget created on a stack to autodestruct itself, which leads to double delete. Use close() method without any parameters.

  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    85
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Memory Trouble

    Another idea would be to try a memory debugger like valgrind: Compile your program in debug mode and then
    valgrind --tool=memcheck path/to/executable

  4. #4
    Join Date
    Feb 2006
    Posts
    2
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: Memory Trouble

    Recompiling everything did solve the problem.
    Still worried about instability, though. I'll see what happens later.

    Thanks for the tip about closing.

  5. #5
    Join Date
    Jan 2006
    Posts
    46
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: Memory Trouble

    If you think you have memory leaks, my tip would be to try SEVERAL
    checks. I usually use electric fence (CFLAGS = -O2 -g -lefence in Makefile)
    and LeakCheck (command line once you've downloaded the file). But not
    both at the same time. They catch different things, but if you try to use them
    both at the same time they interfere with one another.

    Mariane

Similar Threads

  1. Memory debugging in windows
    By txandi in forum Qt Programming
    Replies: 3
    Last Post: 20th February 2009, 13:45
  2. memory leak question
    By cool_qt in forum General Programming
    Replies: 3
    Last Post: 20th January 2009, 07:49
  3. Replies: 0
    Last Post: 26th September 2008, 21:33
  4. Memory leak weirdness
    By Darhuuk in forum General Programming
    Replies: 10
    Last Post: 10th January 2008, 18:51
  5. Memory Leak in my Application :-(
    By Svaths in forum Qt Programming
    Replies: 4
    Last Post: 27th July 2007, 19:42

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.