QOpcUa crashes when remote device goes offline during read
-
I have a quite large application that read/write values from PLCs using QOpcUa.
It works fine, but after running for several hours I get a crash of the application. Using the debugger, it stops at line 42096 of open62541.c:if(client->channel.state != UA_SECURECHANNELSTATE_OPEN) { UA_LOG_INFO(&client->config.logger, UA_LOGCATEGORY_CLIENT, "SecureChannel must be connected before sending requests"); return UA_STATUSCODE_BADSERVERNOTCONNECTED; }It crashes because
clientisnullptr(aka 0x00). I'm quite puzzled that there is no check of the pointer. And of course I cannot change their code.I inspected the call stack, and if I'm not wrong it happened when I was trying to read an attribute using the async function
readValueAttribute().It's hard to tell, but my wild guess is the remote device went offline while executing the reading. And this may happen all the time.
Here my function:void MyOpcUa::readAll() { QMapIterator<QString, Node_t> i(_mapOpcNodes); while (i.hasNext()) { i.next(); Node_t node = i.value(); if (node.enableRead) { node.updated = false; node.node->readValueAttribute(); } } }_mapOpcNodescontains the nodes to be read.
This function is executed for each instance of my class, one for each PLC.Here how I add a node to the map:
void MyOpcUa::insertNode(QString key, QString id, bool enableRead) { Node_t node; node.node = _opcUaClient->node(id); node.value = QVariant(); node.updated = false; node.enableRead = enableRead; _mapOpcNodes.insert(key, node); connect(node.node, &QOpcUaNode::attributeRead, this, &MyOpcUa::handleAttributes); }How to improve my code to avoid such a crash when anything goes wrong between the
readValueAttribute()call and the firing of theattributeRead()signal?