Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Setting Fields value through JNI  RSS feed

 
Em Aiy
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

There is a java class name myClass which has an ArrayList field named as data.

There is a function in java class


now i am calling this method from JNI to add data which is being collected from the system specific dll. MyDll is basically a wrapper dll for that system level dll.

I am not having problem in calling the addData method from within the JNI. Even i am able to pass data in the addData.

If i have simply a print statement in addData, it works fine. but the problem arises when i add the data in ArrayList.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you don't say what the value is, but I suspect that the problem is simply that whatever you're passing to the method isn't a properly allocated, valid String object. Maybe you could show us the code you're using?
 
Em Aiy
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well, i'll post it in few hours.

however, i do want to mention that if i omit the line which is adding the incoming string to the ArrayList, it works fine. Even i have tried to print that string in addData method of java, instead of adding in ArrayList and in that case it does not throw any exception.

Is there any synchronization/threads issue in JNI calling Java Method. (my program is running only in one thread, there is no EXPLICIT threading in it).
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Muhammad Ali Amin:

Is there any synchronization/threads issue in JNI calling Java Method. (my program is running only in one thread, there is no EXPLICIT threading in it).


No. One possible explanation is that you've allocated the characters for the String in automatic storage (i.e., on the stack) and that storage goes away when the method returns; so printing the String inside the method would be OK, but adding it to an ArrayList (and this expecting it to live past the invocation of the native method) would not.
 
Em Aiy
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here is the Exception that i am getting.


The code of JNI method is


Please help me out.
 
Jayesh Lalwani
Ranch Hand
Posts: 502
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might want to look at the javadoc for the exception



This exception may be thrown by methods that have detected concurrent modification of an object when such modification is not permissible.

For example, it is not generally permissible for one thread to modify a Collection while another thread is iterating over it. In general, the results of the iteration are undefined under these circumstances. Some Iterator implementations (including those of all the general purpose collection implementations provided by the JRE) may choose to throw this exception if this behavior is detected. Iterators that do this are known as fail-fast iterators, as they fail quickly and cleanly, rather that risking arbitrary, non-deterministic behavior at an undetermined time in the future.

Note that this exception does not always indicate that an object has been concurrently modified by a different thread. If a single thread issues a sequence of method invocations that violates the contract of an object, the object may throw this exception. For example, if a thread modifies a collection directly while it is iterating over the collection with a fail-fast iterator, the iterator will throw this exception.

Note that fail-fast behavior cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification. Fail-fast operations throw ConcurrentModificationException on a best-effort basis. Therefore, it would be wrong to write a program that depended on this exception for its correctness: ConcurrentModificationException should be used only to detect bugs.



I think the problem is not because of JNI. The problem is with the java code that uses the Array. Possibly, you have an iterator open and you are trying to modify the list while the iterator is open
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!