Skip to content

Memory leaks appear when adding tabs programatically #307

@jporcher

Description

@jporcher

I picked up provided simple example, compiled and run using with leak detection software (VLD). As provided, there is no memory leak.

Now, I change the code to have more than one docking widget:

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    // Create the dock manager. Because the parent parameter is a QMainWindow
    // the dock manager registers itself as the central widget.
	m_DockManager = new ads::CDockManager(this);

	for ( int i = 0; i != 5; ++i )
	{
		// Create example content label - this can be any application specific
		// widget
		QLabel* l = new QLabel();
		l->setWordWrap(true);
		l->setAlignment(Qt::AlignTop | Qt::AlignLeft);
		l->setText("Lorem ipsum dolor sit amet, consectetuer adipiscing elit. ");

		// Create a dock widget with the title Label 1 and set the created label
		// as the dock widget content
		ads::CDockWidget* DockWidget = new ads::CDockWidget("Label " + QString::number(i));
		DockWidget->setWidget(l);

		// Add the toggleViewAction of the dock widget to the menu to give
		// the user the possibility to show the dock widget if it has been closed
		ui->menuView->addAction(DockWidget->toggleViewAction());

		// Add the dock widget to the top dock widget area
		m_DockManager->addDockWidget(ads::TopDockWidgetArea, DockWidget);
	}
}

Still, no memory leak.

Now:

  • If I replace m_DockManager->addDockWidget(ads::TopDockWidgetArea, DockWidget); by m_DockManager->addDockWidgetTab(ads::TopDockWidgetArea, DockWidget); then I get memory leaks (the 5 QLabel and QDockWidget do not get destroyed)
  • Or, if I change the code to use the third parameter of addDockWidget to group Label 0 with Label 1 and Label 2 with Label 3 as below:
ads::CDockAreaWidget* previous = NULL;
for ( int i = 0; i != 5; ++i )
{
    // Create example content label - this can be any application specific
    // widget
    QLabel* l = new QLabel();
    l->setWordWrap(true);
    l->setAlignment(Qt::AlignTop | Qt::AlignLeft);
    l->setText("Lorem ipsum dolor sit amet, consectetuer adipiscing elit. ");

    // Create a dock widget with the title Label 1 and set the created label
    // as the dock widget content
    ads::CDockWidget* DockWidget = new ads::CDockWidget("Label " + QString::number(i));
    DockWidget->setWidget(l);

    // Add the toggleViewAction of the dock widget to the menu to give
    // the user the possibility to show the dock widget if it has been closed
    ui->menuView->addAction(DockWidget->toggleViewAction());

    // Add the dock widget to the top dock widget area
    auto added = m_DockManager->addDockWidget(ads::CenterDockWidgetArea, DockWidget, previous);
    if ( previous )
        previous = NULL;
    else
        previous = added;
}

Again the 5 QLabel and QDockWidget do not get destroyed on close.

Note: Adding the code below in MainWindow fixes the leak, but I suppose user is not expected to do that:

std::vector<ads::CDockAreaWidget*> areas;
for ( int i = 0; i != m_DockManager->dockAreaCount(); ++i )
{
    areas.push_back( m_DockManager->dockArea(i) );
}
for ( auto area : areas )
{
    for ( auto widget : area->dockWidgets() )
        delete widget;
		
    delete area;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions