diff --git a/src/com/opc_ua/opcua_action_info.cpp b/src/com/opc_ua/opcua_action_info.cpp index 96b408635..35d0f1703 100644 --- a/src/com/opc_ua/opcua_action_info.cpp +++ b/src/com/opc_ua/opcua_action_info.cpp @@ -27,8 +27,8 @@ bool CActionInfo::isRemote() const { return (!mEndpoint.empty()); } -CActionInfo* CActionInfo::getActionInfoFromParams(const char *paParams, COPC_UA_Layer &paLayer) { - CActionInfo *retVal = nullptr; +std::unique_ptr CActionInfo::getActionInfoFromParams(const char *paParams, COPC_UA_Layer &paLayer) { + std::unique_ptr retVal = nullptr; CParameterParser mainParser(paParams, ';'); size_t amountOfParameters = mainParser.parseParameters(); @@ -44,9 +44,9 @@ CActionInfo* CActionInfo::getActionInfoFromParams(const char *paParams, COPC_UA_ } if(CActionInfo::eCreateMethod == action) { - retVal = new CLocalMethodInfo(paLayer, endpoint); + retVal.reset(new CLocalMethodInfo(paLayer, endpoint)); } else { - retVal = new CActionInfo(paLayer, action, endpoint); + retVal.reset(new CActionInfo(paLayer, action, endpoint)); } bool somethingFailed = false; @@ -62,7 +62,6 @@ CActionInfo* CActionInfo::getActionInfoFromParams(const char *paParams, COPC_UA_ } if(somethingFailed) { - delete retVal; retVal = nullptr; } } diff --git a/src/com/opc_ua/opcua_action_info.h b/src/com/opc_ua/opcua_action_info.h index 10d6a1883..939c2cf9a 100644 --- a/src/com/opc_ua/opcua_action_info.h +++ b/src/com/opc_ua/opcua_action_info.h @@ -133,9 +133,9 @@ class CActionInfo { * @param paParams Parameters set in the ID of the FB (string contained in the square brackets of opc_ua[...]) * @param paLayer The layer that creates and executes the action * @param paTypes A list of type converters of the connections of the FB of the action (SDs/RDs) - * @return + * @return The new created action info if the parameters were valid, nullptr otherwise */ - static CActionInfo* getActionInfoFromParams(const char *paParams, COPC_UA_Layer &paLayer); + static std::unique_ptr getActionInfoFromParams(const char *paParams, COPC_UA_Layer &paLayer); /** * Retrieves the array of CIEC_ANY to be sent diff --git a/src/com/opc_ua/opcua_layer.cpp b/src/com/opc_ua/opcua_layer.cpp index 31ec20413..a2aeb77cb 100644 --- a/src/com/opc_ua/opcua_layer.cpp +++ b/src/com/opc_ua/opcua_layer.cpp @@ -33,7 +33,7 @@ using namespace forte::com_infra; COPC_UA_Layer::COPC_UA_Layer(CComLayer *paUpperLayer, CBaseCommFB *paComFB) : - CComLayer(paUpperLayer, paComFB), mInterruptResp(e_Nothing), mHandler(nullptr), mActionInfo(nullptr), mDataAlreadyPresent(false), mIsObjectNodeStruct(false), mRDBuffer(nullptr) { + CComLayer(paUpperLayer, paComFB), mInterruptResp(e_Nothing), mHandler(nullptr), mDataAlreadyPresent(false), mIsObjectNodeStruct(false) { } COPC_UA_Layer::~COPC_UA_Layer() = default; @@ -49,9 +49,8 @@ EComResponse COPC_UA_Layer::openConnection(char *paLayerParameter) { if(UA_STATUSCODE_GOOD == mHandler->initializeAction(*mActionInfo)) { CCriticalRegion criticalRegion(mRDBufferMutex); response = e_InitOk; - mRDBuffer = new CIEC_ANY*[getCommFB()->getNumRD()]; for(size_t i = 0; i < getCommFB()->getNumRD(); ++i) { - mRDBuffer[i] = getCommFB()->getRDs()[i]->clone(nullptr); + mRDBuffer.emplace_back(getCommFB()->getRDs()[i]->clone(nullptr)); } } } else { @@ -83,22 +82,16 @@ void COPC_UA_Layer::closeConnection() { if(mHandler) { CCriticalRegion criticalRegion(mRDBufferMutex); mHandler->uninitializeAction(*mActionInfo); - delete mActionInfo; + mActionInfo.reset(); if(mIsObjectNodeStruct) { mStructObjectHelper->uninitializeStruct(); } mHandler = nullptr; - if(mRDBuffer) { - if(mIsObjectNodeStruct) { - COPC_UA_ObjectStruct_Helper::deleteRDBufferEntries(*getCommFB(), mRDBuffer); - } else { - for (size_t i = 0; i < getCommFB()->getNumRD(); ++i) { - delete mRDBuffer[i]; - } - } - delete[] mRDBuffer; - mRDBuffer = nullptr; + if(mIsObjectNodeStruct) { + COPC_UA_ObjectStruct_Helper::deleteRDBufferEntries(*getCommFB(), mRDBuffer); + } else { + mRDBuffer.clear(); } } } diff --git a/src/com/opc_ua/opcua_layer.h b/src/com/opc_ua/opcua_layer.h index 9762916fd..533c398a1 100644 --- a/src/com/opc_ua/opcua_layer.h +++ b/src/com/opc_ua/opcua_layer.h @@ -91,7 +91,7 @@ class COPC_UA_Layer : public forte::com_infra::CComLayer { /** * Information about the action that's returned from the CActionInfo factory, and which is later passed to the handler when initializing, executing and uninitializing the action */ - CActionInfo *mActionInfo; + std::unique_ptr mActionInfo; /** * Called when INIT is triggered in the FB and QI is set to true @@ -151,9 +151,9 @@ class COPC_UA_Layer : public forte::com_infra::CComLayer { CStringDictionary::TStringId getLocalPortNameId(int paPortIndex, bool paIsSD) const; /** - * Array of ANY pointers used as buffer to store the received data + * List of ANY pointers used as buffer to store the received data */ - CIEC_ANY **mRDBuffer; + std::vector> mRDBuffer; /** * Mutex to access the mRDBuffer diff --git a/src/com/opc_ua/opcua_objectstruct_helper.cpp b/src/com/opc_ua/opcua_objectstruct_helper.cpp index 3bc693130..9cad0bf7f 100644 --- a/src/com/opc_ua/opcua_objectstruct_helper.cpp +++ b/src/com/opc_ua/opcua_objectstruct_helper.cpp @@ -222,7 +222,7 @@ int COPC_UA_ObjectStruct_Helper::getRDBufferIndexFromNodeId(const UA_NodeId *paN return -1; } -void COPC_UA_ObjectStruct_Helper::setMemberValues(CIEC_ANY** paRDs, CIEC_ANY **paRDBuffer) { +void COPC_UA_ObjectStruct_Helper::setMemberValues(CIEC_ANY** paRDs, const std::vector>& paRDBuffer) { // TODO implement layer to handle more than 1 struct CIEC_STRUCT& structType = static_cast(paRDs[0]->unwrap()); for(size_t i = 0; i < structType.getStructSize(); i++) { @@ -230,25 +230,21 @@ void COPC_UA_ObjectStruct_Helper::setMemberValues(CIEC_ANY** paRDs, CIEC_ANY **p } } -CIEC_ANY **COPC_UA_ObjectStruct_Helper::initializeRDBuffer() { +std::vector> COPC_UA_ObjectStruct_Helper::initializeRDBuffer() { // TODO implement layer to handle more than 1 struct CIEC_ANY** rds = mLayer.getCommFB()->getRDs(); CIEC_STRUCT& structType = static_cast(rds[0]->unwrap()); const size_t structSize = structType.getStructSize(); - CIEC_ANY **RDBuffer = new CIEC_ANY*[structSize]; + std::vector> RDBuffer;; for(size_t i = 0; i < structSize; i++) { - RDBuffer[i] = structType.getMember(i)->clone(nullptr); + RDBuffer.emplace_back(structType.getMember(i)->clone(nullptr)); } return RDBuffer; } -void COPC_UA_ObjectStruct_Helper::deleteRDBufferEntries(forte::com_infra::CBaseCommFB &paCommFB, CIEC_ANY **paRDBuffer) { +void COPC_UA_ObjectStruct_Helper::deleteRDBufferEntries(forte::com_infra::CBaseCommFB &paCommFB, std::vector>& paRDBuffer) { if(paCommFB.getComServiceType() == e_Subscriber) { - CIEC_ANY** rds = paCommFB.getRDs(); - CIEC_STRUCT& structType = static_cast(rds[0]->unwrap()); - for(size_t i = 0; i < structType.getStructSize(); i++) { - delete paRDBuffer[i]; - } + paRDBuffer.clear(); } } diff --git a/src/com/opc_ua/opcua_objectstruct_helper.h b/src/com/opc_ua/opcua_objectstruct_helper.h index 26bc622cf..5e37d75ae 100644 --- a/src/com/opc_ua/opcua_objectstruct_helper.h +++ b/src/com/opc_ua/opcua_objectstruct_helper.h @@ -81,13 +81,13 @@ class COPC_UA_ObjectStruct_Helper { * @param paRDs the array of data pointers to be sent * @param paRDBuffer The buffer for the data */ - static void setMemberValues(CIEC_ANY** paRDs, CIEC_ANY **paRDBuffer); + static void setMemberValues(CIEC_ANY** paRDs, const std::vector>& paRDBuffer); /** * Initialize RDBuffer for Object Structs * @return The initialized buffer */ - CIEC_ANY **initializeRDBuffer(); + std::vector> initializeRDBuffer(); /** * Delete all entries of the RDBuffer @@ -95,7 +95,7 @@ class COPC_UA_ObjectStruct_Helper { * @param paCommFB The comm fb for which the rdbuffer was created * @param paRDBuffer The buffer to be uninitialized */ - static void deleteRDBufferEntries(forte::com_infra::CBaseCommFB &paCommFB, CIEC_ANY **paRDBuffer); + static void deleteRDBufferEntries(forte::com_infra::CBaseCommFB &paCommFB, std::vector>& paRDBuffer); /** * Check if Data Connection is a Struct Type