Skip to content

Commit

Permalink
Fix memory leak with UA Logger
Browse files Browse the repository at this point in the history
  • Loading branch information
m-meingast committed Oct 10, 2024
1 parent 7c1b3a6 commit e85950b
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 14 deletions.
13 changes: 4 additions & 9 deletions src/com/opc_ua/opcua_local_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ void COPC_UA_Local_Handler::run() {
mUaServer = UA_Server_new();
if(mUaServer) {
UA_ServerConfig *uaServerConfig = UA_Server_getConfig(mUaServer);
/* The original logger is needed to avoid memory leak on shutdown */
UA_Logger uaLogger = *uaServerConfig->logging;
*uaServerConfig->logging = COPC_UA_HandlerAbstract::getLogger();

UA_ServerStrings serverStrings;
generateServerStrings(gOpcuaServerPort, serverStrings);
Expand Down Expand Up @@ -116,6 +119,7 @@ void COPC_UA_Local_Handler::run() {
} else {
DEVLOG_ERROR("[OPC UA LOCAL]: Couldn't initialize Nodesets\n", gOpcuaServerPort);
}
*uaServerConfig->logging = uaLogger;
UA_Server_delete(mUaServer);
mUaServer = nullptr;
}
Expand Down Expand Up @@ -155,7 +159,6 @@ void COPC_UA_Local_Handler::generateServerStrings(TForteUInt16 paUAServerPort, U
}

void COPC_UA_Local_Handler::configureUAServer(UA_ServerStrings &paServerStrings, UA_ServerConfig &paUaServerConfig, UA_UInt16 paServerPort) const {
configureUAServerLogger(paUaServerConfig);
#ifdef FORTE_COM_OPC_UA_MULTICAST
paUaServerConfig.applicationDescription.applicationType = UA_APPLICATIONTYPE_DISCOVERYSERVER;
// hostname will be added by mdns library
Expand Down Expand Up @@ -201,14 +204,6 @@ void COPC_UA_Local_Handler::configureUAServer(UA_ServerStrings &paServerStrings,
}
}

void COPC_UA_Local_Handler::configureUAServerLogger(UA_ServerConfig &paUaServerConfig) const {
UA_free(paUaServerConfig.logging);
paUaServerConfig.logging = &COPC_UA_HandlerAbstract::getLogger();
paUaServerConfig.sessionPKI.logging = &COPC_UA_HandlerAbstract::getLogger();
paUaServerConfig.secureChannelPKI.logging = &COPC_UA_HandlerAbstract::getLogger();
paUaServerConfig.eventLoop->logger = &COPC_UA_HandlerAbstract::getLogger();
}

void COPC_UA_Local_Handler::referencedNodesIncrement(const CSinglyLinkedList<UA_NodeId*> &paNodes, CActionInfo &paActionInfo) {
for(CSinglyLinkedList<UA_NodeId*>::Iterator iterNode = paNodes.begin(); iterNode != paNodes.end(); ++iterNode) {
bool found = false;
Expand Down
6 changes: 1 addition & 5 deletions src/com/opc_ua/opcua_local_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,18 +155,14 @@ class COPC_UA_Local_Handler : public COPC_UA_HandlerAbstract, public CThread {
* @param paServerStrings Place to store the generated strings
*/
void generateServerStrings(TForteUInt16 paUAServerPort, UA_ServerStrings &paServerStrings) const;

/**
* Creates the configuration for the OPC UA Server.
* @param paServerStrings Strings needed to configure the server
* @param paUaServerConfig Place to store all the configurations
* @param paServerPort Port to the server
*/
void configureUAServer(UA_ServerStrings &paServerStrings, UA_ServerConfig &paUaServerConfig, UA_UInt16 paServerPort) const;
/**
* Assigns the UA_Forte_Logger to the OPC UA Server.
* @param paUaServerConfig Place to store all the configurations
*/
void configureUAServerLogger(UA_ServerConfig &paUaServerConfig) const;

/**
* Handler of the OPC UA stack server
Expand Down

0 comments on commit e85950b

Please sign in to comment.