geode-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jbarr...@apache.org
Subject [01/23] geode-native git commit: GEODE-2741: Store shared_ptr in Dictionary to avoid double free.
Date Tue, 06 Jun 2017 17:56:27 GMT
Repository: geode-native
Updated Branches:
  refs/heads/develop 09843302c -> 11467dd9b


GEODE-2741: Store shared_ptr in Dictionary to avoid double free.


Project: http://git-wip-us.apache.org/repos/asf/geode-native/repo
Commit: http://git-wip-us.apache.org/repos/asf/geode-native/commit/ac38453a
Tree: http://git-wip-us.apache.org/repos/asf/geode-native/tree/ac38453a
Diff: http://git-wip-us.apache.org/repos/asf/geode-native/diff/ac38453a

Branch: refs/heads/develop
Commit: ac38453a9e1c99a246661f84bd52c9764341151b
Parents: 0984330
Author: Jacob Barrett <jbarrett@pivotal.io>
Authored: Wed May 17 17:53:07 2017 +0000
Committer: Jacob Barrett <jbarrett@pivotal.io>
Committed: Tue Jun 6 10:43:11 2017 +0000

----------------------------------------------------------------------
 src/clicache/src/CqAttributesFactory.cpp | 73 +++++++++++++--------------
 src/clicache/src/CqAttributesMutator.cpp | 62 ++++++++++-------------
 src/clicache/src/CqAttributesMutator.hpp |  4 +-
 3 files changed, 64 insertions(+), 75 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/geode-native/blob/ac38453a/src/clicache/src/CqAttributesFactory.cpp
----------------------------------------------------------------------
diff --git a/src/clicache/src/CqAttributesFactory.cpp b/src/clicache/src/CqAttributesFactory.cpp
index d504335..45dcbb7 100644
--- a/src/clicache/src/CqAttributesFactory.cpp
+++ b/src/clicache/src/CqAttributesFactory.cpp
@@ -38,51 +38,50 @@ namespace Apache
       namespace native = apache::geode::client;
 
       generic<class TKey, class TResult>
-      void CqAttributesFactory<TKey, TResult>::AddCqListener(Client::ICqListener<TKey,
TResult>^ cqListener )
+      void CqAttributesFactory<TKey, TResult>::AddCqListener( Client::ICqListener<TKey,
TResult>^ cqListener )
       {
-        native::CqListenerPtr listenerptr;
+        native_shared_ptr<native::CqListener>^ listenerptr;
         if ( cqListener != nullptr ) {
-          auto cqStatusListener = dynamic_cast<ICqStatusListener<TKey, TResult>^>(cqListener);
-          if (cqStatusListener != nullptr) {
+          if (auto cqStatusListener = dynamic_cast<ICqStatusListener<TKey, TResult>^>(cqListener))
{
             auto sLstr = gcnew CqStatusListenerGeneric<TKey, TResult>();
             sLstr->AddCqListener(cqListener);
-            listenerptr = std::shared_ptr<native::ManagedCqStatusListenerGeneric>(new
native::ManagedCqStatusListenerGeneric(cqListener));
+            listenerptr = gcnew native_shared_ptr<native::CqListener>(std::shared_ptr<native::ManagedCqStatusListenerGeneric>(new
native::ManagedCqStatusListenerGeneric(cqListener)));
             try {
               CqListenerHelper<TKey, TResult>::g_readerWriterLock->AcquireWriterLock(-1);
-              if ( CqListenerHelper<TKey, TResult>::m_ManagedVsUnManagedCqLstrDict->ContainsKey(
cqListener) ) {
-                CqListenerHelper<TKey, TResult>::m_ManagedVsUnManagedCqLstrDict[cqListener]
= (IntPtr)listenerptr.get();
+              if ( CqListenerHelper<TKey, TResult>::m_ManagedVsUnManagedCqLstrDict->ContainsKey(cqListener)
) {
+                CqListenerHelper<TKey, TResult>::m_ManagedVsUnManagedCqLstrDict[cqListener]
= listenerptr;
               }
               else {
-                CqListenerHelper<TKey, TResult>::m_ManagedVsUnManagedCqLstrDict->Add(cqListener,
(IntPtr)listenerptr.get());
+                CqListenerHelper<TKey, TResult>::m_ManagedVsUnManagedCqLstrDict->Add(cqListener,
listenerptr);
               }
-            } finally {
-                CqListenerHelper<TKey, TResult>::g_readerWriterLock->ReleaseWriterLock();
             }
-            ((native::ManagedCqStatusListenerGeneric*)listenerptr.get())->setptr(sLstr);
+            finally {
+              CqListenerHelper<TKey, TResult>::g_readerWriterLock->ReleaseWriterLock();
+            }
+            ((native::ManagedCqStatusListenerGeneric*)listenerptr->get())->setptr(sLstr);
           }
           else {
             //TODO::split
             auto cqlg = gcnew CqListenerGeneric<TKey, TResult>();
             cqlg->AddCqListener(cqListener);
-            listenerptr = std::shared_ptr<native::ManagedCqListenerGeneric>(new native::ManagedCqListenerGeneric(cqListener));
+            listenerptr = gcnew native_shared_ptr<native::CqListener>(std::shared_ptr<native::ManagedCqListenerGeneric>(new
native::ManagedCqListenerGeneric(cqListener)));
             try {
               CqListenerHelper<TKey, TResult>::g_readerWriterLock->AcquireWriterLock(-1);
-              if ( CqListenerHelper<TKey, TResult>::m_ManagedVsUnManagedCqLstrDict->ContainsKey(
cqListener) ) {
-                CqListenerHelper<TKey, TResult>::m_ManagedVsUnManagedCqLstrDict[cqListener]
= (IntPtr)listenerptr.get();
+              if ( CqListenerHelper<TKey, TResult>::m_ManagedVsUnManagedCqLstrDict->ContainsKey(cqListener)
) {
+                CqListenerHelper<TKey, TResult>::m_ManagedVsUnManagedCqLstrDict[cqListener]
= listenerptr; 
               }
               else {
-                CqListenerHelper<TKey, TResult>::m_ManagedVsUnManagedCqLstrDict->Add(cqListener,
(IntPtr)listenerptr.get());
+                CqListenerHelper<TKey, TResult>::m_ManagedVsUnManagedCqLstrDict->Add(cqListener,
listenerptr);
               }
             } finally {
                 CqListenerHelper<TKey, TResult>::g_readerWriterLock->ReleaseWriterLock();
             }
-            ((native::ManagedCqListenerGeneric*)listenerptr.get())->setptr(cqlg);
+            ((native::ManagedCqListenerGeneric*)listenerptr->get())->setptr(cqlg);
           
           }
         }
-
         try
         {
-          m_nativeptr->get()->addCqListener( listenerptr );
+          m_nativeptr->get()->addCqListener( listenerptr->get_shared_ptr() );
         }
         finally
         {
@@ -91,48 +90,46 @@ namespace Apache
       }
 
       generic<class TKey, class TResult>
-      void CqAttributesFactory<TKey, TResult>::InitCqListeners(array<Client::ICqListener<TKey,
TResult>^>^ cqListeners)
+      void CqAttributesFactory<TKey, TResult>::InitCqListeners(array<Client::ICqListener<TKey,
TResult>^>^ newListeners)
       {
         native::CqAttributes::listener_container_type vrr;
-        for( int i = 0; i < cqListeners->Length; i++ )
+        for( int i = 0; i < newListeners->Length; i++ )
         {
-          auto lister = dynamic_cast<ICqStatusListener<TKey, TResult>^>(cqListeners[i]);
-          if (lister != nullptr) {
-            auto cptr = std::shared_ptr<native::ManagedCqStatusListenerGeneric>(new
native::ManagedCqStatusListenerGeneric(lister));
-            vrr.push_back(std::static_pointer_cast<native::CqListener>(cptr));
+          if (auto lister = dynamic_cast<Client::ICqStatusListener<TKey, TResult>^>(newListeners[i]))
{
+            auto cptr = gcnew native_shared_ptr<native::CqListener>(std::shared_ptr<native::ManagedCqStatusListenerGeneric>(new
native::ManagedCqStatusListenerGeneric(lister)));
+            vrr.push_back(cptr->get_shared_ptr());
             auto cqlg = gcnew CqStatusListenerGeneric<TKey, TResult>();
-            cqlg->AddCqListener(cqListeners[i]);
+            cqlg->AddCqListener(newListeners[i]);
             try {
               CqListenerHelper<TKey, TResult>::g_readerWriterLock->AcquireWriterLock(-1);
-              if ( CqListenerHelper<TKey, TResult>::m_ManagedVsUnManagedCqLstrDict->ContainsKey(
cqListeners[i]) ) {
-                CqListenerHelper<TKey, TResult>::m_ManagedVsUnManagedCqLstrDict[cqListeners[i]]
= (IntPtr)cptr.get();
+              if ( CqListenerHelper<TKey, TResult>::m_ManagedVsUnManagedCqLstrDict->ContainsKey(
newListeners[i]) ) {
+                CqListenerHelper<TKey, TResult>::m_ManagedVsUnManagedCqLstrDict[newListeners[i]]
= cptr;
               }
               else {
-                CqListenerHelper<TKey, TResult>::m_ManagedVsUnManagedCqLstrDict->Add(cqListeners[i],
(IntPtr)cptr.get());
+                CqListenerHelper<TKey, TResult>::m_ManagedVsUnManagedCqLstrDict->Add(newListeners[i],
cptr);
               }
             } finally {
                 CqListenerHelper<TKey, TResult>::g_readerWriterLock->ReleaseWriterLock();
             }
-            ((native::ManagedCqStatusListenerGeneric*)vrr[i].get())->setptr(cqlg);
+            ((native::ManagedCqStatusListenerGeneric*)cptr->get())->setptr(cqlg);
           }
           else {
-            auto lister = cqListeners[i];
-            auto cptr = std::shared_ptr<native::ManagedCqListenerGeneric>(new native::ManagedCqListenerGeneric(lister));
-            vrr.push_back(cptr);
-            CqListenerGeneric<TKey, TResult>^ cqlg = gcnew CqListenerGeneric<TKey,
TResult>();
-            cqlg->AddCqListener(cqListeners[i]);
+            auto cptr = gcnew native_shared_ptr<native::CqListener>(std::shared_ptr<native::ManagedCqListenerGeneric>(new
native::ManagedCqListenerGeneric(newListeners[i])));
+            vrr.push_back(cptr->get_shared_ptr());
+            auto cqlg = gcnew CqListenerGeneric<TKey, TResult>();
+            cqlg->AddCqListener(newListeners[i]);
             try {
               CqListenerHelper<TKey, TResult>::g_readerWriterLock->AcquireWriterLock(-1);
-              if ( CqListenerHelper<TKey, TResult>::m_ManagedVsUnManagedCqLstrDict->ContainsKey(
cqListeners[i]) ) {
-                CqListenerHelper<TKey, TResult>::m_ManagedVsUnManagedCqLstrDict[cqListeners[i]]
= (IntPtr)cptr.get();
+              if ( CqListenerHelper<TKey, TResult>::m_ManagedVsUnManagedCqLstrDict->ContainsKey(
newListeners[i]) ) {
+                CqListenerHelper<TKey, TResult>::m_ManagedVsUnManagedCqLstrDict[newListeners[i]]
= cptr;
               }
               else {
-                CqListenerHelper<TKey, TResult>::m_ManagedVsUnManagedCqLstrDict->Add(cqListeners[i],
(IntPtr)cptr.get());
+                CqListenerHelper<TKey, TResult>::m_ManagedVsUnManagedCqLstrDict->Add(newListeners[i],
cptr);
               }
             } finally {
                 CqListenerHelper<TKey, TResult>::g_readerWriterLock->ReleaseWriterLock();
             }
-            ((native::ManagedCqListenerGeneric*)vrr[i].get())->setptr(cqlg);
+            ((native::ManagedCqListenerGeneric*)cptr->get())->setptr(cqlg);
           }
         }
 

http://git-wip-us.apache.org/repos/asf/geode-native/blob/ac38453a/src/clicache/src/CqAttributesMutator.cpp
----------------------------------------------------------------------
diff --git a/src/clicache/src/CqAttributesMutator.cpp b/src/clicache/src/CqAttributesMutator.cpp
index e1d9e32..d728746 100644
--- a/src/clicache/src/CqAttributesMutator.cpp
+++ b/src/clicache/src/CqAttributesMutator.cpp
@@ -38,49 +38,48 @@ namespace Apache
       generic<class TKey, class TResult>
       void CqAttributesMutator<TKey, TResult>::AddCqListener( Client::ICqListener<TKey,
TResult>^ cqListener )
       {
-        native::CqListenerPtr listenerptr;
+        native_shared_ptr<native::CqListener>^ listenerptr;
         if ( cqListener != nullptr ) {
-          auto cqStatusListener = dynamic_cast<ICqStatusListener<TKey, TResult>^>(cqListener);
-          if (cqStatusListener != nullptr) {
+          if (auto cqStatusListener = dynamic_cast<ICqStatusListener<TKey, TResult>^>(cqListener))
{
             auto sLstr = gcnew CqStatusListenerGeneric<TKey, TResult>();
             sLstr->AddCqListener(cqListener);
-            listenerptr = std::shared_ptr<native::ManagedCqStatusListenerGeneric>(new
native::ManagedCqStatusListenerGeneric(cqListener));
+            listenerptr = gcnew native_shared_ptr<native::CqListener>(std::shared_ptr<native::ManagedCqStatusListenerGeneric>(new
native::ManagedCqStatusListenerGeneric(cqListener)));
             try {
               CqListenerHelper<TKey, TResult>::g_readerWriterLock->AcquireWriterLock(-1);
               if ( CqListenerHelper<TKey, TResult>::m_ManagedVsUnManagedCqLstrDict->ContainsKey(cqListener)
) {
-                CqListenerHelper<TKey, TResult>::m_ManagedVsUnManagedCqLstrDict[cqListener]
= (IntPtr)listenerptr.get();
+                CqListenerHelper<TKey, TResult>::m_ManagedVsUnManagedCqLstrDict[cqListener]
= listenerptr;
               }
               else {
-                CqListenerHelper<TKey, TResult>::m_ManagedVsUnManagedCqLstrDict->Add(cqListener,
(IntPtr)listenerptr.get());
+                CqListenerHelper<TKey, TResult>::m_ManagedVsUnManagedCqLstrDict->Add(cqListener,
listenerptr);
               }
             }
             finally {
               CqListenerHelper<TKey, TResult>::g_readerWriterLock->ReleaseWriterLock();
             }
-            ((native::ManagedCqStatusListenerGeneric*)listenerptr.get())->setptr(sLstr);
+            ((native::ManagedCqStatusListenerGeneric*)listenerptr->get())->setptr(sLstr);
           }
           else {
             //TODO::split
             auto cqlg = gcnew CqListenerGeneric<TKey, TResult>();
             cqlg->AddCqListener(cqListener);
-            listenerptr = std::shared_ptr<native::ManagedCqListenerGeneric>(new native::ManagedCqListenerGeneric(cqListener));
+            listenerptr = gcnew native_shared_ptr<native::CqListener>(std::shared_ptr<native::ManagedCqListenerGeneric>(new
native::ManagedCqListenerGeneric(cqListener)));
             try {
               CqListenerHelper<TKey, TResult>::g_readerWriterLock->AcquireWriterLock(-1);
               if ( CqListenerHelper<TKey, TResult>::m_ManagedVsUnManagedCqLstrDict->ContainsKey(cqListener)
) {
-                CqListenerHelper<TKey, TResult>::m_ManagedVsUnManagedCqLstrDict[cqListener]
= (IntPtr)listenerptr.get(); 
+                CqListenerHelper<TKey, TResult>::m_ManagedVsUnManagedCqLstrDict[cqListener]
= listenerptr; 
               }
               else {
-                CqListenerHelper<TKey, TResult>::m_ManagedVsUnManagedCqLstrDict->Add(cqListener,
(IntPtr)listenerptr.get());
+                CqListenerHelper<TKey, TResult>::m_ManagedVsUnManagedCqLstrDict->Add(cqListener,
listenerptr);
               }
             } finally {
                 CqListenerHelper<TKey, TResult>::g_readerWriterLock->ReleaseWriterLock();
             }
-            ((native::ManagedCqListenerGeneric*)listenerptr.get())->setptr(cqlg);    
       
+            ((native::ManagedCqListenerGeneric*)listenerptr->get())->setptr(cqlg);
           
           }
         }
         try
         {
-          m_nativeptr->get()->addCqListener( listenerptr );
+          m_nativeptr->get()->addCqListener( listenerptr->get_shared_ptr() );
         }
         finally
         {
@@ -91,22 +90,19 @@ namespace Apache
       generic<class TKey, class TResult>
       void CqAttributesMutator<TKey, TResult>::RemoveCqListener( Client::ICqListener<TKey,
TResult>^ cqListener )
       {
-        auto lister = dynamic_cast<Client::ICqStatusListener<TKey, TResult>^>(cqListener);
-        if (lister != nullptr) {
+        if (auto lister = dynamic_cast<Client::ICqStatusListener<TKey, TResult>^>(cqListener))
{
           auto cqlg = gcnew CqStatusListenerGeneric<TKey, TResult>();
           cqlg->AddCqListener(cqListener);
           native::CqStatusListenerPtr lptr = std::shared_ptr<native::ManagedCqStatusListenerGeneric>(
             new native::ManagedCqStatusListenerGeneric(lister));
           ((native::ManagedCqStatusListenerGeneric*)lptr.get())->setptr(cqlg);
           try {
-            IntPtr value;
+            native_shared_ptr<native::CqListener>^ value;
             CqListenerHelper<TKey, TResult>::g_readerWriterLock->AcquireWriterLock(-1);
             if ( CqListenerHelper<TKey, TResult>::m_ManagedVsUnManagedCqLstrDict->TryGetValue(cqListener,
value) ) {
-              // TODO shared_ptr this will break, need to keep shared_ptr
-              native::CqStatusListenerPtr lptr((native::CqStatusListener*)value.ToPointer());
               try
               {
-                m_nativeptr->get()->removeCqListener(lptr);
+                m_nativeptr->get()->removeCqListener(value->get_shared_ptr());
               }
               finally
               {
@@ -124,14 +120,12 @@ namespace Apache
             new native::ManagedCqListenerGeneric(cqListener));
           ((native::ManagedCqListenerGeneric*)lptr.get())->setptr(cqlg);
           try {
-            IntPtr value;
+            native_shared_ptr<native::CqListener>^ value;
             CqListenerHelper<TKey, TResult>::g_readerWriterLock->AcquireWriterLock(-1);
             if ( CqListenerHelper<TKey, TResult>::m_ManagedVsUnManagedCqLstrDict->TryGetValue(cqListener,
value) ) {
-              // TODO shared_ptr this will break, need to keep shared_ptr
-              native::CqListenerPtr lptr((native::CqListener*)value.ToPointer());
               try
               {
-                m_nativeptr->get()->removeCqListener(lptr);
+                m_nativeptr->get()->removeCqListener(value->get_shared_ptr());
               }
               finally
               {
@@ -150,43 +144,41 @@ namespace Apache
         native::CqAttributes::listener_container_type vrr;
         for( int i = 0; i < newListeners->Length; i++ )
         {
-          auto lister = dynamic_cast<Client::ICqStatusListener<TKey, TResult>^>(newListeners[i]);
-          if (lister != nullptr) {
-            auto cptr = std::shared_ptr<native::ManagedCqStatusListenerGeneric>(new
native::ManagedCqStatusListenerGeneric(lister));
-            vrr.push_back(cptr);
+          if (auto lister = dynamic_cast<Client::ICqStatusListener<TKey, TResult>^>(newListeners[i]))
{
+            auto cptr = gcnew native_shared_ptr<native::CqListener>(std::shared_ptr<native::ManagedCqStatusListenerGeneric>(new
native::ManagedCqStatusListenerGeneric(lister)));
+            vrr.push_back(cptr->get_shared_ptr());
             auto cqlg = gcnew CqStatusListenerGeneric<TKey, TResult>();
             cqlg->AddCqListener(newListeners[i]);
             try {
               CqListenerHelper<TKey, TResult>::g_readerWriterLock->AcquireWriterLock(-1);
               if ( CqListenerHelper<TKey, TResult>::m_ManagedVsUnManagedCqLstrDict->ContainsKey(
newListeners[i]) ) {
-                CqListenerHelper<TKey, TResult>::m_ManagedVsUnManagedCqLstrDict[newListeners[i]]
= (IntPtr)cptr.get();
+                CqListenerHelper<TKey, TResult>::m_ManagedVsUnManagedCqLstrDict[newListeners[i]]
= cptr;
               }
               else {
-                CqListenerHelper<TKey, TResult>::m_ManagedVsUnManagedCqLstrDict->Add(newListeners[i],
(IntPtr)cptr.get());
+                CqListenerHelper<TKey, TResult>::m_ManagedVsUnManagedCqLstrDict->Add(newListeners[i],
cptr);
               }
             } finally {
                 CqListenerHelper<TKey, TResult>::g_readerWriterLock->ReleaseWriterLock();
             }
-            ((native::ManagedCqStatusListenerGeneric*)vrr[i].get())->setptr(cqlg);
+            ((native::ManagedCqStatusListenerGeneric*)cptr->get())->setptr(cqlg);
           }
           else {
-            auto lister = newListeners[i];
-            auto cptr = std::shared_ptr<native::ManagedCqListenerGeneric>(new native::ManagedCqListenerGeneric(lister));
-            vrr.push_back(cptr);
+            auto cptr = gcnew native_shared_ptr<native::CqListener>(std::shared_ptr<native::ManagedCqListenerGeneric>(new
native::ManagedCqListenerGeneric(newListeners[i])));
+            vrr.push_back(cptr->get_shared_ptr());
             auto cqlg = gcnew CqListenerGeneric<TKey, TResult>();
             cqlg->AddCqListener(newListeners[i]);
             try {
               CqListenerHelper<TKey, TResult>::g_readerWriterLock->AcquireWriterLock(-1);
               if ( CqListenerHelper<TKey, TResult>::m_ManagedVsUnManagedCqLstrDict->ContainsKey(
newListeners[i]) ) {
-                CqListenerHelper<TKey, TResult>::m_ManagedVsUnManagedCqLstrDict[newListeners[i]]
= (IntPtr)cptr.get();
+                CqListenerHelper<TKey, TResult>::m_ManagedVsUnManagedCqLstrDict[newListeners[i]]
= cptr;
               }
               else {
-                CqListenerHelper<TKey, TResult>::m_ManagedVsUnManagedCqLstrDict->Add(newListeners[i],
(IntPtr)cptr.get());
+                CqListenerHelper<TKey, TResult>::m_ManagedVsUnManagedCqLstrDict->Add(newListeners[i],
cptr);
               }
             } finally {
                 CqListenerHelper<TKey, TResult>::g_readerWriterLock->ReleaseWriterLock();
             }
-            ((native::ManagedCqListenerGeneric*)vrr[i].get())->setptr(cqlg);
+            ((native::ManagedCqListenerGeneric*)cptr->get())->setptr(cqlg);
           }
         }
 

http://git-wip-us.apache.org/repos/asf/geode-native/blob/ac38453a/src/clicache/src/CqAttributesMutator.hpp
----------------------------------------------------------------------
diff --git a/src/clicache/src/CqAttributesMutator.hpp b/src/clicache/src/CqAttributesMutator.hpp
index 498de30..7e04b65 100644
--- a/src/clicache/src/CqAttributesMutator.hpp
+++ b/src/clicache/src/CqAttributesMutator.hpp
@@ -44,9 +44,9 @@ namespace Apache
       generic<class TKey, class TResult>
       private ref class CqListenerHelper sealed{
         public:
-        static IDictionary<Client::ICqListener<TKey, TResult>^, IntPtr>^
+        static Dictionary<Client::ICqListener<TKey, TResult>^, native_shared_ptr<native::CqListener>^>^
           m_ManagedVsUnManagedCqLstrDict = gcnew 
-          Dictionary<Client::ICqListener<TKey, TResult>^, IntPtr>();
+          Dictionary<Client::ICqListener<TKey, TResult>^, native_shared_ptr<native::CqListener>^>();
 
         static ReaderWriterLock^ g_readerWriterLock = gcnew ReaderWriterLock();
       };


Mime
View raw message