Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory overflow?

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;
}

}

like image 929
suigara Avatar asked Aug 28 '26 04:08

suigara


1 Answers

The problem is this line of code:

advlib = NULL;

The advlib object 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 the
advlib = NULL; and allow the CComPtr to do its job.

like image 74
David Schwartz Avatar answered Aug 29 '26 19:08

David Schwartz