ivandasch commented on a change in pull request #9521:
URL: https://github.com/apache/ignite/pull/9521#discussion_r740772780



##########
File path: modules/platforms/cpp/core/include/ignite/jni/utils.h
##########
@@ -124,8 +119,16 @@ namespace ignite
             }
 
         private:
+            /** Initializer */
+            void Init(const 
common::concurrent::SharedPointer<java::JniContext>& ctx0, jobject obj0) {
+                ctx = ctx0;
+
+                if (ctx.IsValid())
+                    this->obj = ctx.Get()->Acquire(obj0);
+            }
+
             /** Context. */
-            java::JniContext* ctx;
+            common::concurrent::SharedPointer<java::JniContext> ctx;

Review comment:
       I've created small test to simulate our situation
   
   ```
   #include <memory>
   #include <iostream>
   
   using namespace std;
   
   
   class Ctx {
   public:
       ~Ctx() {
           cout << "destroyed";
       }
   };
   
   class Wrapper{
   public:
       Wrapper() {}
       Wrapper(shared_ptr<Ctx> pCtx_): pCtx(pCtx_) {}
   private:
       shared_ptr<Ctx> pCtx;
   };
   
   class Env {
   public:
       Env(shared_ptr<Ctx> pCtx_): pCtx(pCtx_) {}
       void AddWrapper() {
           cout << "Num references before setting wrapper " << pCtx.use_count() 
<< endl;
           this->wrapper = Wrapper(pCtx);
           cout << "Num references after after setting wrapper " << 
pCtx.use_count() << endl;
       }
   private:
       shared_ptr<Ctx> pCtx;
       Wrapper wrapper;
   };
   
   int main() {
       auto pCtx = make_shared<Ctx>();
       cout << "Num references before assignment " << pCtx.use_count() << endl;
       {
           Env env(pCtx);
           env.AddWrapper();
           env.AddWrapper();
       }
       cout << "Num references final " << pCtx.use_count() << endl;
       return 0;
   }
   ```
   Num references before assignment 1
   Num references before setting wrapper 2
   Num references after after setting wrapper 3
   Num references before setting wrapper 3
   Num references after after setting wrapper 3
   Num references final 1
   destroyed
   ```
   




-- 
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.

To unsubscribe, e-mail: [email protected]

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


Reply via email to