Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
Gerry_BB
Posts: 38
Registered: ‎12-06-2011
My Device: Playbook 64GB, Curve 9360, BB Z10 LE
Accepted Solution

ListView / DataModel crash

Hi,

 

I am having a consistent crash in my app and cannot find a solution. Basically I have a GroupDataModel as a QProperty in my main class. Then I have a page with three button. Each button creates and opens a different qml page. One of the qml pages contains a ListView connected to the above mentioned data model. The first time the page is opened there are no issues. Then when I go back (causing the page to be destroyed) and re-enter the page it crashes. None of the other pages are affected and can be opened infinite times. I know it is the GroupDataModel because if I change the listview datamodel to point to a temp .xml file I can open this page unlimited times again. Here are some code chunks that I feel are important to this issue :

 

in class.hpp

public :
Q_PROPERTY(bb::cascades::GroupDataModel *expensesModel READ expensesModel CONSTANT); private : bb::cascades::GroupDataModel* expensesModel() const; bb::cascades::GroupDataModel* m_expensesModel;

 in class.cpp

bb::cascades::GroupDataModel* ExpenseTracker::expensesModel() const {
	return m_expensesModel;
}

 

in page containing buttons :

Button {
                        text: "Loans"
                        onClicked: {
                            app.updateDataModel("", "Loan");
                            var page =loanPage.createObject();
                            page.type = "Loan"
                            
                            mainNavigationPane.push(page);
                        }
                        attachedObjects: ComponentDefinition {
                            id: loanPage
                            source: "Expenses_QML/ExpensePage.qml"
                        }
                    }
                    Button {
                        text: "Reports"
                        onClicked: {
                            var page = reportPage.createObject();
                            mainNavigationPane.push(page);
                        }
                        attachedObjects: ComponentDefinition {
                            id: reportPage
                            source: "Reports_QML/ReportPage.qml"
                        }
                    }

 reportPage can be opened continuously, expensePage only one.

In the navigation pane :

    onPopTransitionEnded: {
        console.log("got here");
        page.destroy();
    }

In ExpensePage.qml (which can only be opened once)

ListView {
            onTriggered: {
                var viewExpensePage = viewExpensePageDef.createObject();
                viewExpensePage.expenseData = dataModel.data(indexPath);
                mainNavigationPane.push(viewExpensePage)
            }
            layout: StackListLayout {
            }
            /* with only this changed page can be opened infinite times
             * dataModel: XmlDataModel {
             * source: "models/sheetmodel.xml"
             * } */
            dataModel: app.expensesModel
            listItemComponents: [
                ListItemComponent {
                    type: "item"
                    id: listItemComponent
                    ExpenseListItemView {
                        id: thumbListView
                    }
                }
            ]
        }

 I absolutely need that page to be created and deleted while being opened/closed as this is key in my architecture. Hence I cannot solve this the "cheating" way by not destroying the page and leaving it always loaded. Any suggestions are welcome! If more code is needed let me know and I will add it.

 

Thanks,

 

Gerry

 

 

Please use plain text.
Developer
kylefowler
Posts: 526
Registered: ‎05-17-2009
My Device: 9900

Re: ListView / DataModel crash

You cant use the same datamodel on multiple listviews, are you trying to do that?

Also, when a page is destroyed, everything on the page gets destroyed, so when the listview gets destroyed, it also deletes the datamodel, which then if you try and open the page again, you will be setting the datamodel of the new listview to be some random memory address with nothing there, which will crash it.
Like all of my posts
Please use plain text.
Developer
Zmey
Posts: 1,512
Registered: ‎12-18-2012
My Device: PlayBook, Z10, DAC

Re: ListView / DataModel crash

Hi Kyle! You're right, just a little clarification: it's possible to connect a single DataModel to multiple ListViews. ListView will take ownership of DataModel only if DataModel's parent is NULL.

 


Andrey Fidrya, @zmeyc on twitter
Please use plain text.
Developer
Gerry_BB
Posts: 38
Registered: ‎12-06-2011
My Device: Playbook 64GB, Curve 9360, BB Z10 LE

Re: ListView / DataModel crash

Thanks guys!

 

This solution also fixed another issue where my app would not close properly! (because I was trying to delete the data model in the desctructor but it was already deleted when the page was destroyed)

Please use plain text.
Developer
Zmey
Posts: 1,512
Registered: ‎12-18-2012
My Device: PlayBook, Z10, DAC

Re: ListView / DataModel crash

You're welcome!

 

Btw, if you create DataModel inside of QObject derived class, it's easier to set DataModel's parent to 'this' so it  will be deleted automatically. Usually destructors aren't needed.

 


Andrey Fidrya, @zmeyc on twitter
Please use plain text.