i hava a c++ method(for java,jni) like follow,when i repeat call this from java (every 150ms),about after 4 hours. Memory overflow….
JNIEXPORT jint JNICALL Java_nc_mes_pub_hardware_PCI1761_readChanel
(JNIEnv *, jobject, jint channel){
HRESULT hr ;
CLSID clsid;
hr = CLSIDFromProgID(L"AdvDIO.AdvDIOCtrl", &clsid);
CComPtr<IAdvDIO> advlib;
hr = advlib.CoCreateInstance(clsid);
if ( SUCCEEDED( hr ) )
{
advlib->DeviceNumber = 0;
if(advlib->DeviceNumber < 0){
return -100;
}
int i =advlib->ReadDiChannel( channel );
// advlib.Release();
advlib = NULL;
return i;
}
else
{
return -1;
}
}
The problem is this line of code:
The
advlibobject needs the correct value in order to do its job. By destroying its value and setting it to NULL, it can no longer free the correct instance.Uncommenting
advlib.Release()will likely make it work. But the correct fix is to remove theadvlib = NULL;and allow theCComPtrto do its job.