From d436c19b8c679b0bf28013a139215f76f1bbb93a Mon Sep 17 00:00:00 2001 From: "U-AD\\010850292" <010850292@Anubis.ad.jp.omron.net> Date: Thu, 9 Feb 2017 14:19:08 +0900 Subject: [PATCH 1/2] fix memory leak in AmqpsIothubConnection If proton failed sending, release dlv object. Otherwise release it when received a disposition frame from proton. --- .../amqps/AmqpsIotHubConnection.java | 36 ++++++++++++------- 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/device/iot-device-client/src/main/java/com/microsoft/azure/sdk/iot/device/transport/amqps/AmqpsIotHubConnection.java b/device/iot-device-client/src/main/java/com/microsoft/azure/sdk/iot/device/transport/amqps/AmqpsIotHubConnection.java index af89d70a0d..f421a0653f 100644 --- a/device/iot-device-client/src/main/java/com/microsoft/azure/sdk/iot/device/transport/amqps/AmqpsIotHubConnection.java +++ b/device/iot-device-client/src/main/java/com/microsoft/azure/sdk/iot/device/transport/amqps/AmqpsIotHubConnection.java @@ -355,20 +355,30 @@ public Integer sendMessage(Message message) } // Codes_SRS_AMQPSIOTHUBCONNECTION_15_017: [The function shall set the delivery tag for the sender.] byte[] tag = String.valueOf(this. nextTag++).getBytes(); - Delivery dlv = sender.delivery(tag); + try + { + Delivery dlv = sender.delivery(tag); - logger.LogInfo("Attempting to send the message using the sender link, method name is %s ", logger.getMethodName()); - // Codes_SRS_AMQPSIOTHUBCONNECTION_15_018: [The function shall attempt to send the message using the sender link.] - sender.send(msgData, 0, length); + logger.LogInfo("Attempting to send the message using the sender link, method name is %s ", logger.getMethodName()); + // Codes_SRS_AMQPSIOTHUBCONNECTION_15_018: [The function shall attempt to send the message using the sender link.] + sender.send(msgData, 0, length); - logger.LogInfo("Advancing the sender link, method name is %s ", logger.getMethodName()); - // Codes_SRS_AMQPSIOTHUBCONNECTION_15_019: [The function shall advance the sender link.] - sender.advance(); - - // Codes_SRS_AMQPSIOTHUBCONNECTION_15_020: [The function shall set the delivery hash to the value returned by the sender link.] - deliveryHash = dlv.hashCode(); - logger.LogInfo("Delivery hash returned by the sender link %s, method name is %s ", deliveryHash, logger.getMethodName()); - } + logger.LogInfo("Advancing the sender link, method name is %s ", logger.getMethodName()); + // Codes_SRS_AMQPSIOTHUBCONNECTION_15_019: [The function shall advance the sender link.] + sender.advance(); + + // Codes_SRS_AMQPSIOTHUBCONNECTION_15_020: [The function shall set the delivery hash to the value returned by the sender link.] + deliveryHash = dlv.hashCode(); + logger.LogInfo("Delivery hash returned by the sender link %s, method name is %s ", deliveryHash, logger.getMethodName()); + } + catch (Exception e) + { + // If proton failed sending, release dlv object. Otherwise release it when received a disposition frame from proton. + sender.advance(); + dlv.free(); + deliveryHash = -1; + } + } // Codes_SRS_AMQPSIOTHUBCONNECTION_15_021: [The function shall return the delivery hash.] return deliveryHash; @@ -595,6 +605,8 @@ public void onDelivery(Event event) { listener.messageSent(d.hashCode(), state); } + // release the delivery object which created in sendMessage(). + d.free(); } } logger.LogDebug("Exited from method %s", logger.getMethodName()); From b5e18578c77dce2c4f0be6a6f79b41598f53f7cd Mon Sep 17 00:00:00 2001 From: "U-AD\\010850292" <010850292@Anubis.ad.jp.omron.net> Date: Fri, 10 Feb 2017 10:23:59 +0900 Subject: [PATCH 2/2] should create dlv, outof try-catch. --- .../sdk/iot/device/transport/amqps/AmqpsIotHubConnection.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/device/iot-device-client/src/main/java/com/microsoft/azure/sdk/iot/device/transport/amqps/AmqpsIotHubConnection.java b/device/iot-device-client/src/main/java/com/microsoft/azure/sdk/iot/device/transport/amqps/AmqpsIotHubConnection.java index f421a0653f..ca149b3dcc 100644 --- a/device/iot-device-client/src/main/java/com/microsoft/azure/sdk/iot/device/transport/amqps/AmqpsIotHubConnection.java +++ b/device/iot-device-client/src/main/java/com/microsoft/azure/sdk/iot/device/transport/amqps/AmqpsIotHubConnection.java @@ -355,9 +355,9 @@ public Integer sendMessage(Message message) } // Codes_SRS_AMQPSIOTHUBCONNECTION_15_017: [The function shall set the delivery tag for the sender.] byte[] tag = String.valueOf(this. nextTag++).getBytes(); + Delivery dlv = sender.delivery(tag); try { - Delivery dlv = sender.delivery(tag); logger.LogInfo("Attempting to send the message using the sender link, method name is %s ", logger.getMethodName()); // Codes_SRS_AMQPSIOTHUBCONNECTION_15_018: [The function shall attempt to send the message using the sender link.]