From f6069eebcd33ba5da237d3c2611676602a47cebf Mon Sep 17 00:00:00 2001 From: "abitha.s" Date: Fri, 14 Oct 2016 18:59:03 +0530 Subject: [PATCH] Fix for Memory leak results from static analyzer for NS C++ Wrapper. Change-Id: I97ca331ce6fcd7352350fd36426366433a5521a2 Signed-off-by: abitha.s Reviewed-on: https://gerrit.iotivity.org/gerrit/13307 Tested-by: jenkins-iotivity Reviewed-by: Uze Choi (cherry picked from commit 1b4cea1ba6bb1b57214e0c3bbb6e84f214dda971) Reviewed-on: https://gerrit.iotivity.org/gerrit/13403 Tested-by: Uze Choi --- .../cpp-wrapper/consumer/src/NSConsumerService.cpp | 8 ++++---- .../cpp-wrapper/consumer/src/NSProvider.cpp | 18 ++++++++++++++++++ .../examples/linux/notificationserviceconsumer.cpp | 6 ++++++ .../examples/linux/notificationserviceprovider.cpp | 6 ++++-- .../cpp-wrapper/provider/src/NSProviderService.cpp | 8 ++++++++ 5 files changed, 40 insertions(+), 6 deletions(-) diff --git a/service/notification/cpp-wrapper/consumer/src/NSConsumerService.cpp b/service/notification/cpp-wrapper/consumer/src/NSConsumerService.cpp index ec11649..4187c9c 100755 --- a/service/notification/cpp-wrapper/consumer/src/NSConsumerService.cpp +++ b/service/notification/cpp-wrapper/consumer/src/NSConsumerService.cpp @@ -36,12 +36,13 @@ namespace OIC NS_LOG_V(DEBUG, "provider Id : %s", provider->providerId); NS_LOG_V(DEBUG, "state : %d", (int)state); - NSProvider *nsProvider = new NSProvider(provider); - NSProvider *oldProvider = NSConsumerService::getInstance()->getProvider( - nsProvider->getProviderId()); + std::string provId; + provId.assign(provider->providerId, NS_UTILS_UUID_STRING_SIZE - 1); + NSProvider *oldProvider = NSConsumerService::getInstance()->getProvider(provId); if (oldProvider == nullptr) { + NSProvider *nsProvider = new NSProvider(provider); NS_LOG(DEBUG, "Provider with same Id do not exist. updating Data for New Provider"); auto discoveredCallback = NSConsumerService::getInstance()->getProviderDiscoveredCb(); nsProvider->setProviderState((NSProviderState)state); @@ -72,7 +73,6 @@ namespace OIC NS_LOG(DEBUG, "Provider with same Id exists. updating the old Provider data"); auto changeCallback = oldProvider->getProviderStateReceivedCb(); oldProvider->setProviderState((NSProviderState)state); - delete nsProvider; if (state == NS_ALLOW) { oldProvider->setProviderSubscribedState(NSProviderSubscribedState::SUBSCRIBED); diff --git a/service/notification/cpp-wrapper/consumer/src/NSProvider.cpp b/service/notification/cpp-wrapper/consumer/src/NSProvider.cpp index a9e4261..c70a317 100755 --- a/service/notification/cpp-wrapper/consumer/src/NSProvider.cpp +++ b/service/notification/cpp-wrapper/consumer/src/NSProvider.cpp @@ -160,6 +160,24 @@ namespace OIC NS_LOG_V(DEBUG, "calling Lower layer UpdateTopicList for Provider Id : %s", getProviderId().c_str()); NSResult result = (NSResult) NSConsumerUpdateTopicList(getProviderId().c_str(), topicLL); + + if(topicLL); + { + NSTopicLL * iter = topicLL; + NSTopicLL * following = NULL; + + while (iter) + { + following = iter->next; + if(iter); + { + NSOICFree(iter->topicName); + iter->next = NULL; + NSOICFree(iter); + } + iter = following; + } + } NS_LOG(DEBUG, "updateTopicList - OUT"); return result; } diff --git a/service/notification/cpp-wrapper/examples/linux/notificationserviceconsumer.cpp b/service/notification/cpp-wrapper/examples/linux/notificationserviceconsumer.cpp index 1efff70..c722713 100755 --- a/service/notification/cpp-wrapper/examples/linux/notificationserviceconsumer.cpp +++ b/service/notification/cpp-wrapper/examples/linux/notificationserviceconsumer.cpp @@ -19,6 +19,7 @@ ******************************************************************/ #include +#include #include #include "NSConsumerService.h" @@ -203,6 +204,8 @@ int main(void) topicList->addTopic("OCF_TOPIC3", NSTopic::NSTopicState::UNSUBSCRIBED); provider->updateTopicList(topicList); + delete topicList; + delete provider; } } break; @@ -218,9 +221,12 @@ int main(void) #endif case 6: std::cout << "6. Exit" << std::endl; + NSConsumerService::getInstance()->stop(); isExit = true; break; default: + std::cin.clear(); + std::cin.ignore(numeric_limits::max(), '\n'); break; } } diff --git a/service/notification/cpp-wrapper/examples/linux/notificationserviceprovider.cpp b/service/notification/cpp-wrapper/examples/linux/notificationserviceprovider.cpp index 374405e..eef42db 100755 --- a/service/notification/cpp-wrapper/examples/linux/notificationserviceprovider.cpp +++ b/service/notification/cpp-wrapper/examples/linux/notificationserviceprovider.cpp @@ -113,7 +113,7 @@ int main() std::cout << "12. Disable NS Provider RemoteService" << std::endl; #endif std::cout << "13. Stop the Notification Provider" << std::endl; - std::cout << "0. Exit()" << std::endl; + std::cout << "14. Exit()" << std::endl; std::cout << "input : "; @@ -284,12 +284,14 @@ int main() case 13: NSProviderService::getInstance()->stop(); break; - case 0: + case 14: NSProviderService::getInstance()->stop(); isExit = true; break; default: std::cout << "Under Construction" << std::endl; + std::cin.clear(); + std::cin.ignore(numeric_limits::max(), '\n'); break; } diff --git a/service/notification/cpp-wrapper/provider/src/NSProviderService.cpp b/service/notification/cpp-wrapper/provider/src/NSProviderService.cpp index 5eed943..79d2dc5 100755 --- a/service/notification/cpp-wrapper/provider/src/NSProviderService.cpp +++ b/service/notification/cpp-wrapper/provider/src/NSProviderService.cpp @@ -114,6 +114,7 @@ namespace OIC nsConfig.resourceSecurity = config.resourceSecurity; NSResult result = (NSResult) NSStartProvider(nsConfig); + OICFree(nsConfig.userInfo); NS_LOG(DEBUG, "start - OUT"); return result; } @@ -166,6 +167,12 @@ namespace OIC NS_LOG_V(DEBUG, "nsMsg->providerId : %s", nsMsg->providerId); result = (NSResult) NSSendMessage(nsMsg); + OICFree(nsMsg->dateTime); + OICFree(nsMsg->title); + OICFree(nsMsg->contentText); + OICFree(nsMsg->sourceName); + OICFree(nsMsg->topic); + OICFree(nsMsg->extraInfo); delete nsMsg->mediaContents; delete nsMsg; } @@ -197,6 +204,7 @@ namespace OIC NS_LOG_V(DEBUG, "Provider ID : %s", nsMessage->getProviderId().c_str()); NS_LOG(DEBUG, "createMessage - OUT"); + OICFree(message); return nsMessage; } -- 2.7.4