JNI global COM Interface memory problem...
I'm trying to use a COM object globally in my JNI DLL and running into problems when the DLL is unloaded. Specifically I have a global smart pointer to one of the interfaces defined in my COM object. I call CreateInstance and get a valid interface object which I use in multiple DLL function calls. Everything works beautifully up until the DLL is unloaded. At that point the Visual Studio C++ debugger tells me I have a memory leak and my COM interace pointer is now pointing to garbage which causes an exception. Does anyone out there know what could be wrong with this?
By the way. I've created another DLL which has the same exact code, except not JNI, and tested it on a straight C++ client. No problems whatsoever. It must be something that Java is doing... sigh...
The following code (important parts) is from my JNI DLL:
=========================================
// Import the typelibrary for the COM component //
/////////////////////////////////////////////////////////
#import"PaymentProcessorComponent.tlb" no_namespace
#define kProgID"PaymentProcessorComponent.PaymentProcessor"
// Globals
//////////
struct InitOle
{
InitOle(){ ::CoInitialize(NULL);}
~InitOle(){::CoUninitialize();}
} _init_InitOle_;
IPaymentServerPtr gspPaymentServer;
// JNI functions
/////////////////
JNIEXPORTvoid JNICALL
Java_PPCWrapper_Init(JNIEnv *env, jobject obj)
{
DebugMsg("PPCWrapper::Init()");
HRESULT hr;
try
{
hr = gspPaymentServer.CreateInstance(kProgID);
if (hr != ERROR_SUCCESS)
{
ThrowJavaException(env, hr);
}
}
catch (_com_error err)
{
ThrowJavaException(env, err);
}
}
JNIEXPORTvoid JNICALL
Java_PPCWrapper_Connect(JNIEnv *env, jobject obj, jstring strIP, jint nPort, jint nTimeout)
{
DebugMsg("PPCWrapper::Connect()");
try
{
constchar *str = env->GetStringUTFChars(strIP, 0);
HRESULT hr = gspPaymentServer->Connect(str, nPort, nTimeout);
if (hr != ERROR_SUCCESS)
{
ThrowJavaException(env, hr);
}
env->ReleaseStringUTFChars(strIP, str);
}
catch (_com_error err)
{
ThrowJavaException(env, err);
}
}
JNIEXPORTvoid JNICALL
Java_PPCWrapper_Disconnect(JNIEnv *env, jobject obj)
{
DebugMsg("PPCWrapper::Disconnect()");
try
{
HRESULT hr = gspPaymentServer->Disconnect();
if (hr != ERROR_SUCCESS)
ThrowJavaException(env, hr);
}
catch (_com_error err)
{
ThrowJavaException(env, err);
}
}

