PDA

View Full Version : Image Capture 'hangs' when JNI executed a few times



Cotters
March 19, 2012, 03:11:00
Good Day,
I have a simple Java JNI code running a C++ native code method to generate a BMP image from snapping a live image from a DFK72BUC02 camera. The code works perfectly but when ran a few times (e.g. 4 - 5) always hangs up (i.e. need to close command window to escape). This seems to be a memory leak problem but Windows Task Mgr, Performance Monitor and Java tools do not indicate a leakage. Also I have tried various ways to prevent leakage in C++ code with no change. My environment is: camera connected by USB to Intel 32 PC with Windows XP SP3. I have also installed Processor State Idle Manager. Below is code:
JNICature.java


public class JNICapture
{
public native int capture();
static
{
System.loadLibrary("JNICapture");
}
public static void main(String[] args)
{
JNICapture sample = new JNICapture();
//boolean open = sample.initialise();
int response = sample.capture();
}
}

JNICapture.cpp


// Basic capture image in JNI
//

#include <tisudshl.h>
#include "JNICapture.h"

using namespace _DSHOWLIB_NAMESPACE;

JNIEXPORT jint JNICALL Java_JNICapture_capture
(JNIEnv *env, jobject obj) {

Grabber *pGrabber;
tFrameHandlerSinkPtr pSink;
MemBufferCollection::tMemBufferPtr pBuffer;

pGrabber = new DShowLib::Grabber();

assert(pGrabber);

if( !DShowLib::InitLibrary( "............." ) )
{
printf("The library could not be initialized ");
printf("(invalid license key?).\n");
exit( 1 );
}
pGrabber->showDevicePage();
pSink = FrameHandlerSink::create(1);
pGrabber->setSinkType(pSink);
pGrabber->startLive(false);
pSink->snapImages(1);
pBuffer = pSink->getLastAcqMemBuffer();
pGrabber->stopLive();
pGrabber->closeDev();
pBuffer->save( "RGB8.bmp" );

pSink = NULL;
pBuffer = NULL;
delete pGrabber;
atexit(DShowLib::ExitLibrary);

return 0;

}

void main(){}

Could you provide some direction on how to reslove this issue?
Many Thanks
Mark

Stefan Geissler
March 19, 2012, 10:35:08
Hello,

First off, the JNICapture.cpp looks for a Win32 console application to me. Therefore, you do not call "atexit(DShowLib::ExitLibrary);" but simply "DShowLib::ExitLibrary()" (without "atexit").

Also I would like to suggest to change the sequence from

pBuffer = pSink->getLastAcqMemBuffer();
pGrabber->stopLive();
pGrabber->closeDev();
pBuffer->save( "RGB8.bmp" );


to

pBuffer = pSink->getLastAcqMemBuffer();
pBuffer->save( "RGB8.bmp" );
pGrabber->stopLive();
pGrabber->closeDev();


But this should not make a big difference. I think, you try this with the ExitLibrary() change and then lets see, what happens.

Cotters
March 21, 2012, 23:33:45
Good Day Stefan,
Purely coincidently I received a second DFK72BUC02 camera this week and this had no problem. I updated the driver for the older camera from Version 1.2.2.1 to the same as the new camera (Version 2.1.5.616) and it worked.
Cheers
Mark

Stefan Geissler
March 22, 2012, 09:31:05
Mark,

Thank you for updating me, and good to read, the problem is solved.