1
votes

i'm developing blackberry 10 app. i have some trouble using GroupDataModel.

this is the code :

            GroupDataModel *searchModel;

            if (searchModel != NULL){
                qDebug() << "Masuk sini";
                searchModel->clear();
            }
            searchModel = new GroupDataModel(
                    new QListDataModel<QString>(
                            QList<QString>() << "suburbName" ));
            QVariantMap map;
            for (int i = 0; i < list.size(); ++i) {
                QVariantMap x = list.at(i).toMap();
                map["suburbName"] = x.value("address").toString();
                qDebug() << x;
                qDebug() << map;
                searchModel->insert(map);
            }

            searchList->resetDataModel();
            searchModel->setGrouping(ItemGrouping::None);

            searchList->setDataModel(searchModel);

this code called not just once. so when it's called i have to clear the GroupDataModel. but it's always crash. and when i debug, the problem is when searchModel->clear(); i've already tried replacing that with free(searchModel) but the crash still happen.

the crash says : Segmentation Fault

someone please help me! i've been struggling with this problem for several days.

Thanks

Regards, Yoga

3

3 Answers

1
votes

You are doing it in wrong way. If You want to re-use searchModel Object in you class , make it member variable then only perform your task.

0
votes

You have to initialize the GroupDataModel pointer with NULL. Otherwise the pointer is assigned some random value which is still in the memory. If you access the uninitialized pointer then you get a segmentation fault.

GroupDataModel *searchModel = 0;
0
votes

The code is crashing because you are dereferencing a non-initialized pointer. searchModel has not been initialized to anything when searchModel->clear() is called.

If your code is being called multiple times, a good idea would be to declare searchModel as a member variable:

GroupDataModel *searchModel;

Then you need to initialize it, for example in the containing class's constructor but not in the function that is called a lot as you only need one instance of the GroupDataModel.

searchModel = new GroupDataModel(QStringList() << "suburbName" );

Then the rest of your code should work.