JNI vs. C++ Object Instances -


I have just started with a new job. Here we are new to using JNI (for C ++ / Java bridging). I am new to JNI so please forgive me noobness :)

In our (Win32) Java application we are loading a C + + DLL. On Java side we have many examples of "SomeJClass" , All these examples require the same example of "SomeCClass" on DLL. Entry points such as Global Do Somingin () in DLL are highlighted here I have to call the example method of DOR :: DoSomethingInC (). That's why I want an easy way of mapping the respective pointers, I need a single mapping, when some DLL thread finds something interesting that it needs to inform this Java frequency.

I can think of many solutions, but I do not like them too much. My question is, is there a better way than this?

1 Java Call C: GetNewInstance (). It gives an int which is actually an indicator for the new instance. Java reserves it in m_myCInstance So Java GlobalDoSomethingInC (), and 1a

  // DLL calls Global Void GlobalDoSomethingInC () // This brings back the indicator / call back to Java: Job jmg = NewGlobalRef (env, obj); Door * myDoer = & lt; Reinterpret_cast & gt; (Door) tmpJ- & gt; GetMyCInstance (); MyDoer- & gt; DoSomething (); DeleteGlobalRef (env, tmpJ); // Arrrrgh}  

1b or:

  // ** For each call ** that java adds a parameter, which is stored int: M_IIC instances, and door * MyDoure = & lt; Reintep_cast & gt; (Door) Instance Para-> Doosing INC (); // Can we improve it? To call Java from C, look at things, perhaps, better  
  Creator calls back in Java in C and stores a Java frequency reference to a member variable m_myJInstance. In all subsequent calls m_myJInstance can be used to call back Java. In the Destroyer we need to call "deleting globular" (env, m_myJInstance);  

Not too bad I guess. But it is actually safe to store job references. I mean: what happens when the GC takes the object around?

Our current solution "works" But it is instead :)

Thanax

Usually it will depend on To your environment, to some extent I have only used KNI, which is more ancient than JNI. I think a fair bit of ugliness is indispensable because you are mixing memory tracking on two systems, there is only one GC.

In general, I came to know that all the calls to C code in the works which took care of bad casting, which I think is indispensable. (BTW, I will use C for non-Java code here)

On the side, the sounds of Java objects are definitely a potential problem, it will depend on your platform, but I hope that Unless you are in Lib, you can expect that someone is a Java GC, then your objects are stable. You need to make sure. On the other hand, if this is not the case, then you have become much worse. Let's say that this is the case, you want to do the same thing to isolate the deering / castings in front of JNI function, so that you can work happily with normal C objects in all your tasks.

Where this can actually be ugly, if you can exclude objects from both sides, then the two sides may be placed in the context of your object here we are finally here on the Java side Used, as well as the use of destroyer on the C side. It was not very good, but I think that something is indispensable.

So, fewer answers, it will be somewhat unattractive, separate the ugliness around the interface between the two languages, so that in the work, either language, you do not need to worry about such things. is.

There is also a base class for objects on this interface, as you can also differentiate some ugliness here.


Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -