[ 
https://issues.apache.org/jira/browse/GEODE-9171?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17326689#comment-17326689
 ] 

ASF GitHub Bot commented on GEODE-9171:
---------------------------------------

pivotal-jbarrett commented on a change in pull request #786:
URL: https://github.com/apache/geode-native/pull/786#discussion_r617702424



##########
File path: clicache/src/Cache.hpp
##########
@@ -329,6 +332,10 @@ namespace Apache
 
         Apache::Geode::Client::Internal::PdxTypeRegistry^ m_pdxTypeRegistry;
         Apache::Geode::Client::TypeRegistry^ m_typeRegistry;
+
+        Object^ m_disposedLock;

Review comment:
       Not necessary. 

##########
File path: clicache/src/Cache.cpp
##########
@@ -55,6 +54,20 @@ namespace Apache
         m_nativeptr = gcnew native_shared_ptr<native::Cache>(nativeptr);
         m_pdxTypeRegistry = gcnew 
Apache::Geode::Client::Internal::PdxTypeRegistry(this);
         m_typeRegistry = gcnew Apache::Geode::Client::TypeRegistry(this);
+
+        m_disposedLock = gcnew Object();
+      }
+
+      Cache::~Cache() {
+        msclr::lock lockInstance(m_disposedLock);

Review comment:
       lock on `this`. 

##########
File path: clicache/src/Cache.cpp
##########
@@ -55,6 +54,20 @@ namespace Apache
         m_nativeptr = gcnew native_shared_ptr<native::Cache>(nativeptr);
         m_pdxTypeRegistry = gcnew 
Apache::Geode::Client::Internal::PdxTypeRegistry(this);
         m_typeRegistry = gcnew Apache::Geode::Client::TypeRegistry(this);
+
+        m_disposedLock = gcnew Object();
+      }
+
+      Cache::~Cache() {
+        msclr::lock lockInstance(m_disposedLock);
+        if (!m_disposed) {
+          this->!Cache();
+          m_disposed = true;

Review comment:
       Checking and setting `m_nativeptr` to `nullptr` would negate the need 
for this `m_disposed` variable.

##########
File path: clicache/src/Cache.cpp
##########
@@ -57,6 +56,21 @@ namespace Apache
         m_typeRegistry = gcnew Apache::Geode::Client::TypeRegistry(this);
       }
 
+      Cache::~Cache() { //Destructor - deterministic
+        if (!_disposed) {
+          //Clean-up managed resources
+          this->!Cache();
+          _disposed = true;
+          //GC.SuppressFinalize(this) is automatically added here
+          //Base destructor is automatically called too if needed
+        }
+      }
+
+      Cache::!Cache() { //Finalizer - non-deterministic when called by GC
+        //Clean-up unmanaged resources
+        delete m_nativeptr;

Review comment:
       From my reading of this guide the C++/CLI should delete managed 
resources in the destructor and free native resources the finalizer.
   > Beside unmanaged data, a managed class can also contain managed data, i.e. 
instances of managed classes implementing IDisposable. Managed data is 
different from unmanaged data in that it should be disposed in Dispose() but 
not in the finalizer. This is because instances of managed classes may already 
have been garbage collected when the finalizer runs.
   ```C++
   ref class DataContainer {
   public:
      ...
   
     ~DataContainer() {
       if (m_isDisposed)
          return;
   
       delete m_managedData; // dispose managed data
       this->!DataContainer(); // call finalizer
       m_isDisposed = true;
     }
   
     // Finalizer
     !DataContainer() {
       DataProvider::DeleteUnmanagedData(m_unmanagedData); // free unmanaged 
data
     }
   
   private:
     bool m_isDisposed;
     IntPtr m_unmanagedData;
     IDisposable^ m_managedData;
   };
   ```
   
   This class technically only has managed data which should be deleted in the 
dispose/destructor.
   
   The unmanaged data is held in the managed `native_shared_ptr` class.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


> Threads Aren't Being Terminated
> -------------------------------
>
>                 Key: GEODE-9171
>                 URL: https://issues.apache.org/jira/browse/GEODE-9171
>             Project: Geode
>          Issue Type: Bug
>          Components: native client
>            Reporter: Michael Martell
>            Priority: Major
>              Labels: pull-request-available
>
> For .NET applications using the native client, threads aren't being 
> terminated when the native client Cache is closed. This causes a huge leak of 
> memory which can cause long running apps to crash.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to