heap corruption when using JNI
I have written a C++ DLL that has a class which accesses methods in a Java class through JNI. I wrote a C++ test driver in which I perform a couple of new on another class in the DLL(which have nothing to do with JNI). when a new is called a second time, the application crashes. The code is given below:
//This code is from the Test Driver
mStatus = mGDMAccess.OpenDB();//This call has some JNI related calls
if(mStatus != SUCCESS)
{
AfxMessageBox("Failed to Open Database");
CStringstrError;
strError = mGDMAccess.m_strErrMsg;
AfxMessageBox(strError);
return FALSE;
}
CSTransponderDevice*pDevice = NULL;
pDevice = new CSTransponderDevice("CMA-00011");
CSTransponderDevice *pDevice1 = NULL;
pDevice1 = new CSTransponderDevice("CMA-00001"); //Crashes here in the new
The JNI calls are given below:
WORD wVersionRequested;
WSADATA wsaData;
int err;
myIP = NULL;
jmethodID consid;
myIP = hostIP;
wVersionRequested = MAKEWORD( 1, 1 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
/* Tell the user that we couldn't find a usable */
/* WinSock DLL. */
//return;
}
HOSTENT*mHostEnt;
in_addrin;
if(!pGDMImpl)
pGDMImpl = CSGDMAccess_Impl::Init();//new CSGDMAccess_Impl;
else
{
return ENDOFFILE;
}
// Retrieve the IP address of the ORBIX server machine from the host file
if (myIP == NULL)
{
mHostEnt = gethostbyname("DbServer");
if(mHostEnt == NULL)
{
//printf("No Host table entry");
CString strMsg = "No Host table entry.";
m_strErrMsg = strMsg;
return EUNKNOWNHOST;
}
memcpy(&in.s_addr, *(mHostEnt->h_addr_list),4);
myIP = inet_ntoa(in);
}
Debug(PRINT_ALL_DEBUG,_T("GDevAccess: CSGDMAccess::InitGDM() IP Address:->%s<-\n"),(LPCTSTR)myIP);
TRACE("IP address is: %s\n",myIP);//(LPCTSTR)mHostEnt->h_addr_list[0]);
char classpath[1024];
JDK1_1InitArgs vm_args;
vm_args.version = 0x00010001;
JNI_GetDefaultJavaVMInitArgs(&vm_args);
sprintf(classpath, "%s%c%s", vm_args.classpath, PATH_SEPARATOR, /*USER_CLASSPATH*/getenv("CLASSPATH"));
vm_args.classpath = classpath;/* Create the Java VM */
/* Create the Java VM*/
pGDMImpl->m_nResult = JNI_CreateJavaVM(&(pGDMImpl->m_pJvm),(void**)&(pGDMImpl->m_pEnv),&vm_args);
if (pGDMImpl->m_nResult<0)
{
Debug(PRINT_ALL_DEBUG,_T("Could not create JavaVM\n"));
if(pGDMImpl->m_pEnv->ExceptionOccurred())
{
Debug(PRINT_ALL_DEBUG,_T("Could not create JavaVMException occurred\n"));
pGDMImpl->m_pEnv->ExceptionDescribe();
}
return EFAILURE;
}
Debug(PRINT_ALL_DEBUG,_T("Created JavaVM\n"));
pGDMImpl->m_pJvm->AttachCurrentThread((void**)&(pGDMImpl->m_pEnv),NULL);
pGDMImpl->m_cClass = pGDMImpl->m_pEnv->FindClass("com/acterna/cable/oa/DbBeans/clients/GDMAccess");
if (pGDMImpl->m_cClass == 0)
{
Debug(PRINT_ALL_DEBUG,_T("Can't find GDMAccess class\n"));
if(pGDMImpl->m_pEnv->ExceptionOccurred())
{
Debug(PRINT_ALL_DEBUG,_T("Can't find GDMAccess classException occurred\n"));
pGDMImpl->m_pEnv->ExceptionDescribe();
}
return EFAILURE;
}
jstring jstr = (jstring)pGDMImpl->m_pEnv->NewStringUTF((LPCTSTR)myIP);
if(jstr == 0)
{
Debug(PRINT_ALL_DEBUG,_T("Could not create JString\n"));
if(pGDMImpl->m_pEnv->ExceptionOccurred())
{
Debug(PRINT_ALL_DEBUG,_T("jstr!Exception occured\n"));
pGDMImpl->m_pEnv->ExceptionDescribe();
}
}
jobjectArray args = pGDMImpl->m_pEnv->NewObjectArray( 1,
pGDMImpl->m_pEnv->FindClass( "java/lang/String"), jstr);
if(args == 0)
{
Debug(PRINT_ALL_DEBUG,_T("Could not create JObjectArray\n"));
if(pGDMImpl->m_pEnv->ExceptionOccurred())
{
Debug(PRINT_ALL_DEBUG,_T("Args!Exception occured\n"));
pGDMImpl->m_pEnv->ExceptionDescribe();
}
}
consid = pGDMImpl->m_pEnv->GetMethodID(pGDMImpl->m_cClass,"<init>","([Ljava/lang/String;)V");
if(consid == 0)
{
Debug(PRINT_ALL_DEBUG,_T("InitGDM not found!\n"));
if(pGDMImpl->m_pEnv->ExceptionOccurred())
{
Debug(PRINT_ALL_DEBUG,_T("InitGDM not found!Exception occured\n"));
pGDMImpl->m_pEnv->ExceptionDescribe();
}
return EFAILURE;
}
Debug(PRINT_ALL_DEBUG,_T("Found Constructor\n"));
pGDMImpl->m_result = pGDMImpl->m_pEnv->NewObject(pGDMImpl->m_cClass,consid,args);
if(pGDMImpl->m_result == 0)
{
Debug(PRINT_ALL_DEBUG,_T("Could not instantiate\n"));
if(pGDMImpl->m_pEnv->ExceptionOccurred())
{
Debug(PRINT_ALL_DEBUG,_T("Could not instantiate:Exception occurred\n"));
pGDMImpl->m_pEnv->ExceptionDescribe();
}
return EFAILURE;
}
pGDMImpl->m_cClass1 = pGDMImpl->m_pEnv->GetObjectClass(pGDMImpl->m_result);
if(pGDMImpl->m_cClass1 == 0)
{
Debug(PRINT_ALL_DEBUG,_T("Could not GetObjectClass\n"));
if(pGDMImpl->m_pEnv->ExceptionOccurred())
{
Debug(PRINT_ALL_DEBUG,_T("Could not GetObjectClass:Exception occured\n"));
pGDMImpl->m_pEnv->ExceptionDescribe();
}
return EFAILURE;
}
pGDMImpl->m_pEnv->DeleteLocalRef(jstr);
return ESUCCESS;
I would appreciate any help in this matter.

