0

my program keeps crashing with returncode 0. The cause is somewhere in my qtabwidget but I can't find the error.

 QTabWidget *layout_tabs;

// create tabs
void myclass::fill_tabs(void)
{
  kill_tabs(); // remove old tabs 
  layout_tabs = new QTabWidget();

  // program adds content into a few tabs, like:
  // widgets created, content created, put into layout, put into widget..
  layout_tabs->addTab(widget, "description");
  layout_tabs->addTab(widget2, "description2");

  layout_tabs->show();
}

void myclass::kill_tabs(void)
{
  if(layout_tabs==nullptr)
    return;
  layout_tabs->hide();

  QWidget *window;

  for ( int i=layout_tabs->count()-1; i>=0; --i)
    {
        window = layout_tabs->widget(i); // remember widget
        layout_tabs->removeTab(i); // remove tab
        free(window); // remove widget
    }

  free(layout_tabs); // remove qtabwidget
  layout_tabs=nullptr;
}

the filltabs() function is used a few times. The old tabwidget is destroyed and a new is created. It does not matter if I don't delete the tabwidget, but remove only the tabs. The program still exits with returncode 0.

Daniel Hedberg
  • 5,677
  • 4
  • 36
  • 61

2 Answers2

1

You call free(layout_tabs) but you allocate it with operator new(). You should deallocate it with delete layout_tabs instead. I don't see how your window variable is allocated but you should check if it too should be deallocated with operator delete(), or if your QTabWidget owns its memory (i.e. if it is responsible for managing that memory).

Void - Othman
  • 3,441
  • 18
  • 18
0

Set the QApplication::quitOnLastWindowClosed to false.

#include <QApplication>

// ...

qApp->setQuitOnLastWindowClosed (false);

Or you can go and set the container for your tabs (the main window/ main widget) to have the property of Qt::WA_QuitOnClose set to false.

myWidget->setAttribute(Qt::WA_QuitOnClose, false);

Either of those should fix it. Also returning with "0" is not a crash. Zero typically indicates a normal exit.

http://qt-project.org/doc/qt-4.8/qapplication.html#quitOnLastWindowClosed-prop

http://qt-project.org/doc/qt-4.8/qt.html#WidgetAttribute-enum

phyatt
  • 18,472
  • 5
  • 61
  • 80
  • Thanks for your answer. Perhaps I forgot to mention that the main windows does not close. The tabwindow is a separate window to show results of a calculation. When I quit my program by shutting the main window, the program 'crashed with returncode 0' as kdevelop says. So it it not really a 'normal end' as kdevelop otherwise says. – Kees Bergwerf Feb 12 '13 at 03:24
  • ok I solved the problem. I should use layout_tabs = new QTabWidget(0) ( not QTabWidget() ! ) in de constructor and not delete the tabwidget anymore, only remove the tabs. – Kees Bergwerf Feb 12 '13 at 23:13