PDA

View Full Version : How QDeclarativeView destruction



xman_ss
27th April 2012, 04:44
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:




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();
}

viulskiez
27th April 2012, 07:56
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.

xman_ss
27th April 2012, 08:52
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:



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

viulskiez
27th April 2012, 08:55
Maybe the pOldView still used by another objects, so simply use QObject::deleteLater.

pOldView->deleteLater();