PDA

View Full Version : Fatal Signal on invoke methode



patrikd
30th October 2014, 11:03
Hi all,
I'm running into a strange problem:
- I have a QObject which holds a list of items


class CGrowersList : public QObject
{
Q_OBJECT
...
QQmlListProperty<CGrower> growers();
Q_INVOKABLE CGrower* byShortName(const QString& shortName) const;
private:
QList<CGrower*> m_growers;



- The CGrowerList item is published to qml via contextproperty in my main.cpp:


CGrowersList growers;
viewer.rootContext()->setContextProperty("growers", &growers);


- the m_growers are used as a model for list:


QQmlListProperty<CGrower> CGrowersList::growers(){
return QQmlListProperty<CGrower>(this, m_growers);
}


this works so far so good.
Problem appears when i try to invoke the methode "byShortName" from within the qml-code.


CGrower* CGrowersList::byShortName(const QString& shortName) const {
qDebug() << "enter by shortname: " << shortName;
foreach (CGrower* item, m_growers) {
if (item->shortName().compare(shortName) == 0){
qDebug() << "return : " << item->name();
return item;
}
}
qDebug() << "return nullgrower: ";
return m_nullGrower;
}

It works 2-10 times and suddenly I get a Fatal signal 11 (SIGSEGV). The methode is entered
correctly but when the methode tries to access its member (m_growers) it crashes.
I call several methodes on the CGrowersList wrapper, e.g. sort, filter,... Everything works perfectly
as long as I don't return a object. Same problem appears if I try to use a "at" function.
Any ideas?

Thx,
Patrik

anda_skoa
30th October 2014, 12:24
When you return a QObject from a Q_INVOKABLE then the QML engine will assume that it is now the owner of the object and has to delete it.

In your case you don't want that, you want to keep ownership with the C++ side.



QQmlEngine::setObjectOwnership(item, QQmlEngine::CppOwnership);
return item;


Cheers,
_

patrikd
30th October 2014, 13:05
Hi Anda,
you saved my day :)
Many thanks,
Patrik