Browse Source

fixed memleaks, see #32

Leon Urbas 11 years ago
parent
commit
32b7dd45ce
4 changed files with 17 additions and 26 deletions
  1. 3 3
      include/opcua_basictypes.h
  2. 9 19
      src/opcua_secureLayer.c
  3. 3 2
      src/opcua_transportLayer.c
  4. 2 2
      tool/generate_builtin.py

+ 3 - 3
include/opcua_basictypes.h

@@ -67,7 +67,7 @@ UA_Int32 TYPE##_decode(UA_Byte const * src, UA_Int32* pos, TYPE * dst);\
 UA_Int32 TYPE##_delete(TYPE * p);\
 UA_Int32 TYPE##_delete(TYPE * p);\
 UA_Int32 TYPE##_deleteMembers(TYPE * p); \
 UA_Int32 TYPE##_deleteMembers(TYPE * p); \
 UA_Int32 TYPE##_init(TYPE * p); \
 UA_Int32 TYPE##_init(TYPE * p); \
-UA_Int32 TYPE##_new(TYPE * p);
+UA_Int32 TYPE##_new(TYPE ** p);
 
 
 
 
 #define UA_TYPE_METHOD_CALCSIZE_SIZEOF(TYPE) \
 #define UA_TYPE_METHOD_CALCSIZE_SIZEOF(TYPE) \
@@ -112,10 +112,10 @@ UA_Int32 TYPE##_init(TYPE * p){ \
 }
 }
 
 
 #define UA_TYPE_METHOD_NEW_DEFAULT(TYPE) \
 #define UA_TYPE_METHOD_NEW_DEFAULT(TYPE) \
-UA_Int32 TYPE##_new(TYPE * p){ \
+UA_Int32 TYPE##_new(TYPE ** p){ \
 	UA_Int32 retval = UA_SUCCESS;\
 	UA_Int32 retval = UA_SUCCESS;\
 	retval |= UA_alloc((void**)p, TYPE##_calcSize(UA_NULL));\
 	retval |= UA_alloc((void**)p, TYPE##_calcSize(UA_NULL));\
-	retval |= TYPE##_init(p);\
+	retval |= TYPE##_init(*p);\
 	return retval;\
 	return retval;\
 }
 }
 
 

+ 9 - 19
src/opcua_secureLayer.c

@@ -271,12 +271,10 @@ UA_Int32 SL_openSecureChannel(UA_connection *connection,
 
 
 	printf("-----> reserved: %i\n", response.length);
 	printf("-----> reserved: %i\n", response.length);
 	printf("-----> encoded: %i\n", pos);
 	printf("-----> encoded: %i\n", pos);
-	//FIXME: Sten: here is a problem
 
 
 	//449 = openSecureChannelResponse
 	//449 = openSecureChannelResponse
 	SL_send(connection, &response, 449);
 	SL_send(connection, &response, 449);
-	//FIXME: this call crashs in UA_free
-	//UA_ByteString_deleteMembers(&response);
+	UA_ByteString_deleteMembers(&response);
 
 
 	return UA_SUCCESS;
 	return UA_SUCCESS;
 }
 }
@@ -304,17 +302,9 @@ UA_Int32 SL_createSecurityToken(UA_connection *connection, UA_Int32 lifeTime) {
 }
 }
 
 
 UA_Int32 SL_processMessage(UA_connection *connection, UA_ByteString message) {
 UA_Int32 SL_processMessage(UA_connection *connection, UA_ByteString message) {
-	// UA_DiagnosticInfo serviceDiagnostics;
-
+	UA_Int32 retval = UA_SUCCESS;
 	UA_Int32 pos = 0;
 	UA_Int32 pos = 0;
-	// UA_RequestHeader requestHeader;
-	// UA_UInt32 clientProtocolVersion;
 	UA_NodeId serviceRequestType;
 	UA_NodeId serviceRequestType;
-	// UA_Int32 requestType;
-	// UA_Int32 securityMode;
-	// UA_Int32 requestedLifetime;
-	// UA_ByteString clientNonce;
-	// UA_StatusCode serviceResult;
 
 
 	// Every Message starts with a NodeID which names the serviceRequestType
 	// Every Message starts with a NodeID which names the serviceRequestType
 	UA_NodeId_decode(message.data, &pos,
 	UA_NodeId_decode(message.data, &pos,
@@ -329,10 +319,6 @@ UA_Int32 SL_processMessage(UA_connection *connection, UA_ByteString message) {
 		 * to the definition in part 4 */
 		 * to the definition in part 4 */
 		// 	Req-1) RequestHeader requestHeader
 		// 	Req-1) RequestHeader requestHeader
 		UA_RequestHeader requestHeader;
 		UA_RequestHeader requestHeader;
-		UA_ExtensionObject additionalHeader;
-		//FIXME: Sten the structure was empty and decode attempted to fill it -> segfault
-		//FIXME: Sten: probably not needed anymore, since memory gets malloced in the decoder
-		requestHeader.additionalHeader = &additionalHeader;
 		// 	Req-2) UInt32 ClientProtocolVersion
 		// 	Req-2) UInt32 ClientProtocolVersion
 		UA_UInt32 clientProtocolVersion;
 		UA_UInt32 clientProtocolVersion;
 		// 	Req-3) Enum SecurityTokenRequestType requestType
 		// 	Req-3) Enum SecurityTokenRequestType requestType
@@ -430,14 +416,15 @@ UA_Int32 SL_processMessage(UA_connection *connection, UA_ByteString message) {
 		//TODO process requestedLifetime
 		//TODO process requestedLifetime
 
 
 		// 62541-4 §7.27 "The requestHandle given by the Client to the request."
 		// 62541-4 §7.27 "The requestHandle given by the Client to the request."
-		return SL_openSecureChannel(connection, &requestHeader, SC_Good);
+		retval = SL_openSecureChannel(connection, &requestHeader, SC_Good);
+		UA_RequestHeader_deleteMembers(&requestHeader);
 	} else {
 	} else {
 		printf("SL_processMessage - unknown service request");
 		printf("SL_processMessage - unknown service request");
 		//TODO change error code
 		//TODO change error code
-		return UA_ERROR;
+		retval = UA_ERROR;
 
 
 	}
 	}
-	return UA_SUCCESS;
+	return retval;
 }
 }
 /*
 /*
  * receive and process data from underlying layer
  * receive and process data from underlying layer
@@ -534,6 +521,9 @@ void SL_receive(UA_connection *connection, UA_ByteString *serviceMessage) {
 	} else {
 	} else {
 		printf("SL_receive - no data received \n");
 		printf("SL_receive - no data received \n");
 	}
 	}
+	// Clean up
+	UA_SecureConversationMessageHeader_deleteMembers(&secureConvHeader);
+	UA_AsymmetricAlgorithmSecurityHeader_deleteMembers(&asymAlgSecHeader);
 	/*
 	/*
 	 Int32 readPosition = 0;
 	 Int32 readPosition = 0;
 
 

+ 3 - 2
src/opcua_transportLayer.c

@@ -125,8 +125,6 @@ UA_Int32 TL_process(UA_connection *connection,UA_Int32 packetType, UA_Int32 *pos
 	UA_OPCUATcpAcknowledgeMessage *ackMessage;
 	UA_OPCUATcpAcknowledgeMessage *ackMessage;
 	UA_OPCUATcpMessageHeader *ackHeader;
 	UA_OPCUATcpMessageHeader *ackHeader;
 
 
-
-
 	printf("TL_process - entered \n");
 	printf("TL_process - entered \n");
 
 
 	switch(packetType)
 	switch(packetType)
@@ -187,6 +185,8 @@ UA_Int32 TL_process(UA_connection *connection,UA_Int32 packetType, UA_Int32 *pos
 			UA_OPCUATcpAcknowledgeMessage_encode(ackMessage,&tmpPos,tmpMessage.data);
 			UA_OPCUATcpAcknowledgeMessage_encode(ackMessage,&tmpPos,tmpMessage.data);
 
 
 			printf("TL_process - Size messageToSend = %d \n",ackHeader->messageSize);
 			printf("TL_process - Size messageToSend = %d \n",ackHeader->messageSize);
+			UA_OPCUATcpMessageHeader_delete(ackHeader);
+			UA_OPCUATcpAcknowledgeMessage_delete(ackMessage);
 			/* ------------------------ Body ------------------------ */
 			/* ------------------------ Body ------------------------ */
 			// protocol version
 			// protocol version
 			printf("TL_process - localConf.protocolVersion = %d \n",connection->transportLayer.localConf.protocolVersion);
 			printf("TL_process - localConf.protocolVersion = %d \n",connection->transportLayer.localConf.protocolVersion);
@@ -200,6 +200,7 @@ UA_Int32 TL_process(UA_connection *connection,UA_Int32 packetType, UA_Int32 *pos
 			printf("TL_process - localConf.maxChunkCount = %d \n", connection->transportLayer.localConf.maxChunkCount);
 			printf("TL_process - localConf.maxChunkCount = %d \n", connection->transportLayer.localConf.maxChunkCount);
 			UA_ByteString_printf("encoded data",&tmpMessage);
 			UA_ByteString_printf("encoded data",&tmpMessage);
 			TL_send(connection, &tmpMessage);
 			TL_send(connection, &tmpMessage);
+			// do not delete tmpMessage - this is the responsibility of the send thread
 		}
 		}
 		else
 		else
 		{
 		{

+ 2 - 2
tool/generate_builtin.py

@@ -146,7 +146,7 @@ def createStructured(element):
     print("UA_Int32 " + name + "_delete("+ name + "* p);", end='\n', file=fh)
     print("UA_Int32 " + name + "_delete("+ name + "* p);", end='\n', file=fh)
     print("UA_Int32 " + name + "_deleteMembers(" + name + "* p);", end='\n', file=fh)
     print("UA_Int32 " + name + "_deleteMembers(" + name + "* p);", end='\n', file=fh)
     print("UA_Int32 " + name + "_init("+ name + " * p);", end='\n', file=fh)
     print("UA_Int32 " + name + "_init("+ name + " * p);", end='\n', file=fh)
-    print("UA_Int32 " + name + "_new(" + name + " * p);", end='\n', file=fh)
+    print("UA_Int32 " + name + "_new(" + name + " ** p);", end='\n', file=fh)
 
 
     print("UA_Int32 "  + name + "_calcSize(" + name + " const * ptr) {", end='', file=fc)
     print("UA_Int32 "  + name + "_calcSize(" + name + " const * ptr) {", end='', file=fc)
     print("\n\tif(ptr==UA_NULL){return sizeof("+ name +");}", end='', file=fc)
     print("\n\tif(ptr==UA_NULL){return sizeof("+ name +");}", end='', file=fc)
@@ -201,7 +201,7 @@ def createStructured(element):
 				print("\tretval |= UA_Array_decode(src,dst->"+n+"Size, UA_" + t[0:t.find("*")].upper()+",pos,(void const**) (dst->"+n+"));", end='\n', file=fc) #not tested
 				print("\tretval |= UA_Array_decode(src,dst->"+n+"Size, UA_" + t[0:t.find("*")].upper()+",pos,(void const**) (dst->"+n+"));", end='\n', file=fc) #not tested
             elif t.find("*") != -1:
             elif t.find("*") != -1:
 				#allocate memory using new
 				#allocate memory using new
-				print('\tretval |= UA_'+ t[0:t.find("*")] +"_new(dst->" + n + ");", end='\n', file=fc)
+				print('\tretval |= UA_'+ t[0:t.find("*")] +"_new(&(dst->" + n + "));", end='\n', file=fc)
 				print('\tretval |= UA_' + t[0:t.find("*")] + "_decode(src,pos,dst->"+ n +");", end='\n', file=fc)
 				print('\tretval |= UA_' + t[0:t.find("*")] + "_decode(src,pos,dst->"+ n +");", end='\n', file=fc)
             else:
             else:
                 print('\tretval |= UA_'+t+"_decode(src,pos,&(dst->"+n+"));", end='\n', file=fc)
                 print('\tretval |= UA_'+t+"_decode(src,pos,&(dst->"+n+"));", end='\n', file=fc)