问题1:在下面的函数中,我想在返回值之前调用 方法。在这种情况下,我如何做到这一点可能会导致应用程序崩溃。ReleaseStringUTFChars
String JNIXMLDOMDocument::GetXML() const
{
String strXML;
jmethodID method = m_JavaEnv->GetMethodID(m_XMLDomDocClass, "GetXML", "(Lorg/w3c/dom/Document;)Ljava/lang/String;");
if(method == NULL) {
return NULL;
}
jstring jstrXML = (jstring)m_JavaEnv->CallObjectMethod(m_XMLDomDocClassObject, method, m_XMLDomDocument);
if (jstrXML == NULL) {
return strXML;
}
strXML = m_JavaEnv->GetStringUTFChars(jstrXML, 0);
/* Question 1: **/
m_JavaEnv->ReleaseStringUTFChars(jstrXML, strXML.c_str());
return strXML;
} /* String GetXML() const **/
问题2:如果我的应用程序使用多线程,在构造函数中附加线程并在所有函数中使用它并在析构函数中分离是否正确?
JNIXMLDOMDocument::JNIXMLDOMDocument()
{
/* Check If the current thread is attached to the virtual machine. **/
int needToDetach;
/* Question 2: **/
m_JavaEnv = AttachCurrentThread(&needToDetach);
if (m_JavaEnv != NULL)
{
/* Try to find JAVA 'XMLDOMDocument' class. **/
m_XMLDomDocClass = m_JavaEnv->FindClass("com/fido/android/framework/service/XMLDOMDocument");
if (m_XMLDomDocClass != NULL) {
jmethodID constructor = m_JavaEnv->GetMethodID(m_XMLDomDocClass, "<init>", "()V");
if (constructor != NULL) {
m_XMLDomDocClassObject = m_JavaEnv->NewObject(m_XMLDomDocClass, constructor);
}
else DBG_ERROR(DBG_CTX, ("Can't create object of 'XMLDOMDocument' class!\n"));
}
else DBG_ERROR(DBG_CTX, ("Can't find 'XMLDOMDocument' class!\n"));
/* Try to find 'XMLDOMNode' class. **/
jclass localXMLDomNodeClass = m_JavaEnv->FindClass("com/fido/android/framework/service/XMLDOMNode");
if (localXMLDomNodeClass != NULL) {
jmethodID constructor = m_JavaEnv->GetMethodID(localXMLDomNodeClass, "<init>", "()V");
if (constructor != NULL) {
m_XMLDomNodeClassObject = m_JavaEnv->NewObject(localXMLDomNodeClass, constructor);
}
else DBG_ERROR(DBG_CTX, ("Can't create object of 'XMLDOMNode' class!\n"));
}
else DBG_ERROR(DBG_CTX, ("Can't find 'XMLDOMNode' class!\n"));
}
} /* JNIXMLDOMDocument() **/