Repository: opencv
Issue: java.lang.UnsupportedOperationException: Native object address is NULL in  org.opencv.core.Mat.<init>
Labels: bug,auto-transferred,priority: normal,affected: 2.4,category: java bindings

Transferred from http://code.opencv.org/issues/3613

```
|| Arsen L on 2014-03-16 19:24
|| Priority: Normal
|| Affected: 2.4.8 (latest release)
|| Category: java bindings
|| Tracker: Bug
|| Difficulty: 
|| PR: 
|| Platform: x64 / Windows
```
## java.lang.UnsupportedOperationException: Native object address is NULL in  org.opencv.core.Mat.<init>

```
Sporadically (quite rarely) receiving following exception in my java-application

<pre>
Exception in thread "pool-4-thread-4" java.lang.UnsupportedOperationException: Native object address is NULL
    at org.opencv.core.Mat.<init>(Mat.java:424)
    at org.opencv.utils.Converters.Mat_to_vector_Mat(Converters.java:266)
    at org.opencv.utils.Converters.Mat_to_vector_vector_Point(Converters.java:500)
    at org.opencv.imgproc.Imgproc.findContours(Imgproc.java:5851)
</pre>


Interesting, that if JVM launched in debug mode - this is not happen (issue relates to JVM?)

Tried on 1.7.0_45 and following one JVMs:
Java HotSpot(TM) 64-Bit Server VM; 1.7.0_51; 24.51-b03; mixed mode
```
## History
##### Daniil Osokin on 2014-03-17 07:11

```
Hi, Arsen!

Looks like it's an issue in your code. Anyway, please provide code sample, that reproduce this problem. Also may be you could debug this issue? It will help a lot!
-   Assignee changed from Vadim Pisarevsky to Arsen L
```
##### Arsen L on 2014-03-19 10:25

```
Daniil, 

I made working example (see file attached) on which you may reproduce this issue.
My current assumption is that this exception depends on some in-JVM concurrency optimizations.

I realize, that calling MyOpenCvUtils.init() in BorderDetectorWorker.borderField() smells, but leave it as is: it looks like it will increase probability to reproduce issue.
Replace path to image to real path (Main.IMAGE_PATH) on your machine. Also, launch JVM with -Djava.library.path="path_to_opencv_java248.dll" and declare library "opencv-248.jar" for sources which I prepared.

Please, try to launh Main.main() with Main.THREAS_NUM equals to number of your machine CPU cores.
After about 5-10 minutes of running you should see following excetion in console:

<pre>
Exception in thread "Thread-3" java.lang.UnsupportedOperationException: Native object address is NULL
    at org.opencv.core.Mat.<init>(Mat.java:424)
    at org.opencv.utils.Converters.Mat_to_vector_Mat(Converters.java:266)
    at org.opencv.utils.Converters.Mat_to_vector_vector_Point(Converters.java:500)
    at org.opencv.imgproc.Imgproc.findContours(Imgproc.java:5851)
    at BorderDetectorWorker.borderField(BorderDetectorWorker.java:22)
    at BorderDetectorWorker.run(BorderDetectorWorker.java:32)
    at java.lang.Thread.run(Unknown Source)
</pre>

Note, that if laumced JVM in debug mode exception will not be thrown (so, there is no sense to debug code).
So, that's why I started to think this bug relates to Sun/Oracle JVM.

Please, let me know if you reproduced this issue or if I can assist you with something.

-   File sources_to_reproduce.zip added
```
##### Arsen L on 2014-03-19 10:26

```
-   Assignee changed from Arsen L to Daniil Osokin
```
##### Daniil Osokin on 2014-03-21 07:53

```
Thanks, Arsen. We will try to reproduce.
-   Assignee changed from Daniil Osokin to Alexander Smorkalov
-   Category changed from imgproc, video to java bindings
```
##### Sergei Nosov on 2014-03-24 15:07

```
-   Status changed from New to Open
```
##### Piotr Betkier on 2014-09-08 12:41

```
I observe the same issue in my application when using OpenCV concurrently. I'm using OpenCV 2.4.9 on Linux.
```