Qt5.12:调用qml回调函数会导致零星的分段错误

时间:2019-01-31 22:24:59

标签: c++ qt qml

我有一个GridView,我在其中调用这样的C ++方法

function delegateDataHandler()
{
    print(Qt.formatTime(new Date(),"h:m:s:z"), "delegateDataHandler finished")
}

GridView.onAdd : {
    cppProxy.startObserver( itemDelegate.observerPointName, itemDelegate.delegateDataHandler );
}

这是我的cpp代码:

void FacadeProxy::startObserver( QVariant observerName, QJSValue dataHandler )
{
   QString observerNameAsString = observerName.toString();

   try {
      m_facade->startObserver( observerNameAsString.toStdString(),
             [dataHandler, this]( std::vector<float> in ) mutable -> void {
                     if ( dataHandler.isCallable() ) {
                        QJSValue val = dataHandler.engine()->newObject();
                        val.setProperty("x", 10);
                        QQmlEngine::setObjectOwnership( val.toQObject(), QQmlEngine::JavaScriptOwnership);

                        QJSValueList args;
                        args.push_back( val );
                        dataHandler.call( args );

                     }
            }
      );
      Info("observer \"%s\" started", observerNameAsString.toStdString().c_str() );
   } catch(...) {
      Error("observer \"%s\" start failed", observerNameAsString.toStdString().c_str() );
   }
}

偶尔我的dataHandler.call( args );出现段故障,此实现是否存在任何明显的问题?

0 个答案:

没有答案