mdyusuf000 / javacv

Automatically exported from code.google.com/p/javacv
GNU General Public License v2.0
0 stars 0 forks source link

Bad allocation error on multithreaded opencv_highgui.cvLoadImageM #248

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Create a Callable object that simply loads a .tif image (ca. 20 mb size) as 
a cvMat, or IplImage with the cvLoadImageM method, or cvLoadImage, AND then 
saves the image as an jpg file with cvSaveImage to a new destination. 

2. Now do this with all images in a folder, and foreach image create the above 
callable and feed it to an ExecutorCompletionService, like:

ExecutorService taskPool = 
Executors.newCachedThreadPool(Executors.privilegedThreadFactory());
ExecutorCompletionService<Boolean> taskCompletionService = new 
ExecutorCompletionService<>(taskPool);

3. Susequently poll all futures to see if the operation succeeded. 

What is the expected output? What do you see instead?

I expected that all my operations would return true, and I would have converted 
all .tif's to .jpg's.

Instead either the first fioe, or the first 2 returns this:

java.lang.RuntimeException: bad allocation
    at com.googlecode.javacv.cpp.opencv_highgui.cvLoadImageM(Native Method)
    at com.googlecode.javacv.cpp.opencv_highgui.cvLoadImageM(opencv_highgui.java:243)
    ...
    ...

But the subsequent files are converted without errors. Besides that I can see 
that I get the first error after 5 sec. running time, then after 92 sec. the 
second image is correctly created, and from there on the images are created in 
0 - 3 seconds. (Or atleast thats how long I wait before my poll on the 
ExecutorCompletionService returns something!).

What version of the product are you using? On what operating system?
I use a Win7 x64 machine, with i5-460M, 4 GB DDR3.
OpenCv 2.4.2 and JavaCv 0.2

Please provide any additional information below.
If i do it in 1 thread I have no issues.

Original issue reported on code.google.com by Stegger....@gmail.com on 11 Oct 2012 at 7:56

GoogleCodeExporter commented 9 years ago
Could provide a source file with a `main()` method that I could run here to try 
it out? thanks

Original comment by samuel.a...@gmail.com on 11 Oct 2012 at 9:32

GoogleCodeExporter commented 9 years ago
Sure thing Samuel, here you go.

In my own application the callable, and the image operation (loading and 
saving) are in their own classes, but for ease of use for you I have rewritten 
it so that you have it in one file. 

I ran the attached file, and it gives the same error. 

I will also mention that its not consistently the first file it breaks on, but 
sometimes 2 files or even 3.

Original comment by Stegger....@gmail.com on 11 Oct 2012 at 9:58

Attachments:

GoogleCodeExporter commented 9 years ago
Ah, I think I see what the problem is. The issue doesn't come up on my machine 
running Linux, but opencv_highgui doesn't try to protect with a mutex or 
something the initialization routines of libtiff here:
http://code.opencv.org/projects/opencv/repository/revisions/master/entry/modules
/highgui/src/grfmt_tiff.cpp#L61
We should be able to work around that by calling cvLoadImage() once before 
trying to use it in multiple threads simultaneously. Does that do the trick for 
your app?

Original comment by samuel.a...@gmail.com on 11 Oct 2012 at 1:21

GoogleCodeExporter commented 9 years ago
No not really, in fact it introduced this at the beginning (Maybe on the 
cvLoadImage() before i start threading):

OpenCV Error: Insufficient memory (Failed to allocate 52838420 bytes) in 
unknown function, file ..\..\..\src\opencv\modules\core\src\alloc.cpp, line 52

Do you know if this problem also appears on freebsd servers, cause that's where 
we intent to run the application?

Original comment by Stegger....@gmail.com on 11 Oct 2012 at 7:51

GoogleCodeExporter commented 9 years ago
Failed to allocate 52 megs? That looks like a Windows problem, doesn't it? BTW, 
you should add cvReleaseImage() somewhere in your code to free up some memory 
at some point. Maybe that's the problem...

Original comment by samuel.a...@gmail.com on 12 Oct 2012 at 1:17

GoogleCodeExporter commented 9 years ago
Hi Samuel. 

I tried running the original project on an Ubuntu installation, and just as 
you, I did not have any issues. So I will not be diving more into this issue as 
the software is not intended to run on a Windows installation.

Another thing, I would like to give you a personal thanks because of the great 
work your doing with javacv. It's very much appreciated.

Original comment by Stegger....@gmail.com on 12 Oct 2012 at 11:32

GoogleCodeExporter commented 9 years ago
BTW, do you know if it behaves like that outside of Java as well, using OpenCV 
from native C/C++?

Sure, you're welcome!

Original comment by samuel.a...@gmail.com on 5 Nov 2012 at 11:43

GoogleCodeExporter commented 9 years ago
Hi Samuel

I haven't tried to do any multithreading with OpenCV from native C/C++. I 
intend to perform the operations on a lot of images, on a pretty fast server. 
So Java's concurrency library came in very handy and really eases up the 
development.

So sorry, can't help you there. But my gut feeling tells me that this is simply 
Windows beeing a pain :-) and that it would not be an issue in native C/C++.

Original comment by Stegger....@gmail.com on 5 Nov 2012 at 6:38

GoogleCodeExporter commented 9 years ago
Issue 329 has been merged into this issue.

Original comment by samuel.a...@gmail.com on 18 Jun 2013 at 1:39

GoogleCodeExporter commented 9 years ago
BTW, can anyone check if that's been fixed with recent versions of OpenCV?

And if the problem still exist, we should report it upstream. Thanks!

Original comment by samuel.a...@gmail.com on 29 Apr 2014 at 12:45