本文介绍了C ++ / JNI内存泄漏,需要帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 嗨组! 我正在使用C ++和Java与JNI在RICHEDIT中获取一些文本到我的 java程序。我这样做是通过每秒访问一个C ++方法。这一切 工作正常,除了每次调用C ++ 方法都会泄漏内存。 任何人都可以帮忙我有这个问题吗?我不确定 泄漏是否在C ++或java代码中。但我想这是在C ++方面 ,因为我在那里有点新手。 是否有办法释放内存分配给一个LPTSTR C ++? 请看下面的代码,这是我用java调用每个秒的方法。我相信这是LPTSTR泄漏,因为当字符串变大时,泄漏更多。 < -------- ------- LPTSTR输出; JNIEXPORT jstring JNICALL Java_JNIHandler_getDealerOutput(JNIEnv * env,jobject obj) { size = SendMessage( (HWND)textarea,//目标窗口句柄 WM_GETTEXTLENGTH,//消息到发送 (WPARAM)0, (LPARAM)0)+1; 输出=新TCHAR [size]; SendMessage(textarea, WM_GETTEXT, 大小, (LPARAM)(无效* )输出); 返回env-> NewStringUTF(dealerStr); } --- ---------------> 我试图释放NewStringUTF 时分配的内存使用JNI.h方法env - > ReleaseStringUTFChars(str, dealerStr)无济于事。 我也尝试使用try / finally块并在最终设置中: output = NULL;删除输出;但是没有区别。 干杯 安德烈亚斯Hi group!I am using C++ and java with JNI to get some text in a RICHEDIT to myjava program. I do so by accessing a C++ method every second. It allworks fine except that it leaks memory every call I make to the C++method.Can anyone please help me with this problem? I am not sure wether theleakage is in the C++ or java code. But i guess it''s on the C++ sidesince I''m kind of newbee there.Is there perhaps a way to release the memory allocated for a LPTSTR inC++?Please take a look at the code below, it is the method i call everysecond from java. I belive it is the LPTSTR that is leaking, since itleaks more when the string gets larger.<---------------LPTSTR output;JNIEXPORT jstring JNICALLJava_JNIHandler_getDealerOutput(JNIEnv *env, jobject obj){size =SendMessage((HWND) textarea, // handle to destination windowWM_GETTEXTLENGTH, // message to send(WPARAM) 0,(LPARAM) 0 ) +1;output = new TCHAR[size];SendMessage(textarea,WM_GETTEXT,size,(LPARAM)(void*)output);return env->NewStringUTF(dealerStr);}------------------>I have tried to release the memory allocated when the NewStringUTF ismade with the JNI.h method env -> ReleaseStringUTFChars(str,dealerStr) to no avail.I hae also tried to use a try/finally block and in the finally set:output= NULL; delete output; but there is no difference.CheersAndreas推荐答案 最后不是C ++语言的一部分 试试这个 jstring res = env-> ; NewStringUTF(dealerStr); 删除[]输出; 返回res; 我想你更像是Java程序员而不是C ++程序员。 size = SendMessage(textarea,WM_GETTEXTLENGTH,0,0); std :: vector< TCHAR>输出(大小+ 1); SendMessage(textarea,WM_GETTEXT,大小,(LPARAM)(void *)& output [0]); 返回env-> NewStringUTF(& output [0 ]); 我对Windows和JNI的细节有点模糊,但最重要的是,如果你用new []分配内存,那么你必须通过删除来释放它[ ],如果你使用矢量,那么你根本就不需要解除分配。 finally is not part of the C++ language Try this jstring res = env->NewStringUTF(dealerStr); delete[] output; return res; I guess you are more of a Java programmer than a C++ programmer. Another method would be to use a vector, then you wouldn''t have to worry about memory allocation. size = SendMessage(textarea, WM_GETTEXTLENGTH, 0, 0); std::vector<TCHAR> output(size + 1); SendMessage(textarea, WM_GETTEXT, size, (LPARAM)(void*)&output[0]); return env->NewStringUTF(&output[0]); I''m a bit hazy on the Windows and JNI details but the bottom line is that if you allocate memory with new[] then you have to free it with delete[], and if you use a vector then you don''t have to deallocate at all. 约翰所说的,但考虑使用std: :basic_string的< TCHAR>而不是 std :: vector< TCHAR>。 C ++中的基本字符串具有更多类似Java的语法,例如,例如运算符[]是范围检查的,并且可以使用运算符+进行连接 。What John said, but consider using a std::basic_string<TCHAR> instead ofstd::vector<TCHAR>. The basic string in C++ has more Java-like syntax,e.g. the operator [ ] is range-checked, and concatenation can be donewith operator +. dealerStr来自哪里来自?这应该是''输出''吗?我那么 你应该把''output''作为局部变量 Java_JNIHandler_getDealerOutput()。 我认为你也可能有问题将文本从窗口传递到 NewStringUTF(),该函数需要特定格式的字节,而不是" just纯文本 (虽然在很多情况下它都适用于纯文本)。我不认为这会导致内存泄漏,只是奇怪。字符串从您的原生方法返回 (或者可能崩溃JVM ;-) - chrisWhere does dealerStr come from ? Is this supposed to be ''output'' ? I so thenyou should probably make ''output'' be a local variable ofJava_JNIHandler_getDealerOutput().I think you also may have problems with passing the text from the window toNewStringUTF(), that function requires bytes in a specific format that isn''t"just plain text" (though it''ll work with plain text in many cases). I don''tthink that''d be causing memory leaks, though, just "strange" Strings returnedfrom your native method (or possibly crashing the JVM ;-)-- chris 但是std :: basic_string的问题是你不能得到一个可写的 指针。因此它不适合传递给SendMessage(win, WM_GETTEXT ......)。 johnBut the problem with std::basic_string is that you cannot get a writablepointer from it. Therefore it''s not suitable for passing to SendMessage(win,WM_GETTEXT ...).john 这篇关于C ++ / JNI内存泄漏,需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-30 15:10