From 51d4d12cacb66851fb3f52b5d8af5bf507669f30 Mon Sep 17 00:00:00 2001 From: "senthil.gs@samsung.com" Date: Thu, 28 Nov 2019 12:04:25 +0530 Subject: [PATCH] Fix RE unit test case. (#632) Unit test case which was failing: RemoteResourceObjectTest.IsCachingReturnsTrueAfterStartCaching() Reason for failure: Shared pointer reference for RCSRemoteResourceObject was not released. Due to which, there was a problem in tearing down the test case. This PR has the fix. Additionally, application will receive caching callback from RCSRemoteResourceObject only if remote resource object is available. https://github.sec.samsung.net/RS7-IOTIVITY/IoTivity/pull/632 (cherry-picked from e8fb156d8239ace91f9529830f4781ea16e85bd3) Change-Id: Idf587d1f5add4360a717a6959dcc839bc7aacb9f Signed-off-by: Senthil Kumar G S Signed-off-by: DoHyun Pyun --- .../include/RCSRemoteResourceObject.h | 1 + .../src/resourceClient/RCSRemoteResourceObject.cpp | 31 ++++++++++++---------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/service/resource-encapsulation/include/RCSRemoteResourceObject.h b/service/resource-encapsulation/include/RCSRemoteResourceObject.h index 05b189c..19d3e98 100644 --- a/service/resource-encapsulation/include/RCSRemoteResourceObject.h +++ b/service/resource-encapsulation/include/RCSRemoteResourceObject.h @@ -581,6 +581,7 @@ namespace OIC std::vector< std::string > getInterfaces() const; private: + std::weak_ptr< RCSRemoteResourceObject > weakFromThis(); std::shared_ptr< PrimitiveResource > m_primitiveResource; CacheID m_cacheId; BrokerID m_brokerId; diff --git a/service/resource-encapsulation/src/resourceClient/RCSRemoteResourceObject.cpp b/service/resource-encapsulation/src/resourceClient/RCSRemoteResourceObject.cpp index 26d83f9..80c25fe 100644 --- a/service/resource-encapsulation/src/resourceClient/RCSRemoteResourceObject.cpp +++ b/service/resource-encapsulation/src/resourceClient/RCSRemoteResourceObject.cpp @@ -98,27 +98,26 @@ namespace { SCOPE_LOG_F(DEBUG, TAG); + std::shared_ptr resource = resourcePtr.lock(); + if(!resource) + { + OIC_LOG(ERROR, TAG, "Resource object is null"); + return OC_STACK_OK; + } + //If error code is failure then RE Cache module should //do clean up for caching flags, maps etc. if(eCode > 4) { - OIC_LOG_V(ERROR, TAG, "Error code: %d",eCode); + OIC_LOG_V(ERROR, TAG, "Error code: %d", eCode); try { - std::shared_ptr resource = resourcePtr.lock(); - if(resource) - { - resource->stopCaching(); - } - else - { - OIC_LOG(ERROR, TAG, "Resource object is null"); - } + resource->stopCaching(); } catch(...) { //Exception will be thrown: stack will return OC_STACK_ERROR - // if it already stopped observe. This call is reqired for clearing + //if it already stopped observe. This call is reqired for clearing //Cache manager. OIC_LOG(DEBUG, TAG, "Cleared Cache"); } @@ -217,6 +216,11 @@ namespace OIC } } + std::weak_ptr< RCSRemoteResourceObject > RCSRemoteResourceObject::weakFromThis() + { + return shared_from_this(); + } + RCSRemoteResourceObject::Ptr RCSRemoteResourceObject::fromOCResource( std::shared_ptr< OC::OCResource > ocResource) { @@ -340,17 +344,16 @@ namespace OIC m_primitiveResource, std::bind(cachingCallback, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, - std::move(cb), shared_from_this()), CACHE_METHOD::OBSERVE_ONLY, + std::move(cb), weakFromThis()), CACHE_METHOD::OBSERVE_ONLY, freq, 0); } - else if (cb) { m_cacheId = ResourceCacheManager::getInstance()->requestResourceCache( m_primitiveResource, std::bind(cachingCallback, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, - std::move(cb), shared_from_this()), CACHE_METHOD::ITERATED_GET, + std::move(cb), weakFromThis()), CACHE_METHOD::ITERATED_GET, freq, 0); } else -- 2.7.4