How QDeclarativeView destruction
Hi all,
I face a problem in Qt, QDeclarativeView can not delete after constructing via pointer. It throws an exception: Segmentation fault. :crying:
Could you tell me a solution
This my code:
Code:
MainWindow::MainWindow()
{
m_pView = QDeclarativeView(); // CONSTRUCT NEW QtDeclarativeView
m_pView->setSource("qrc:/UI/main.qml");
setCentralWidget(m_pView);
setFocus();
centralWidget()->setFocus();
}
MainWindow::ChangeView()
{
QDeclarativeView* pOldView = m_pView;
m_pView = QDeclarativeView();
m_pView->setSource("qrc:/UI/main.qml");
setCentralWidget(m_pView);
setFocus();
centralWidget()->setFocus();
delete pOldView; // DESTROY THE POINTER; EXCEPTION HERE
}
main()
{
MainWindow w;
w.show();
w.ChangeView();
}
Re: How QDeclarativeView destruction
The source code doesn't clear because you do not use new operator to instantiate the object, but you try to destroy using delete operator.
Re: How QDeclarativeView destruction
Quote:
Originally Posted by
viulskiez
The source code doesn't clear because you do not use new operator to instantiate the object, but you try to destroy using delete operator.
Yes, it's my mistake. The source should be:
Code:
MainWindow::MainWindow()
{
m_pView = new QDeclarativeView(); // CONSTRUCT NEW QtDeclarativeView
m_pView->setSource("qrc:/UI/main.qml");
setCentralWidget(m_pView);
setFocus();
centralWidget()->setFocus();
}
But I can't not delete QDeclarativeView* after using new operator.
It's ok to continue the program without delete, but it can lead to memory leaking. How to avoid it
Re: How QDeclarativeView destruction
Maybe the pOldView still used by another objects, so simply use QObject::deleteLater.
Code:
pOldView->deleteLater();