C ++ / JNI内存泄漏,需要帮助 [英] C++ / JNI memory leakage, help needed
问题描述
嗨组!
我正在使用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 my
java program. I do so by accessing a C++ method every second. It all
works 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 the
leakage is in the C++ or java code. But i guess it''s on the C++ side
since I''m kind of newbee there.
Is there perhaps a way to release the memory allocated for a LPTSTR in
C++?
Please take a look at the code below, it is the method i call every
second from java. I belive it is the LPTSTR that is leaking, since it
leaks more when the string gets larger.
<---------------
LPTSTR output;
JNIEXPORT jstring JNICALL
Java_JNIHandler_getDealerOutput(JNIEnv *env, jobject obj){
size = SendMessage(
(HWND) textarea, // handle to destination window
WM_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 is
made 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.
Cheers
Andreas
推荐答案
John哈里森写道:
John Harrison wrote:
" Sambucus" <一个*************** @ aetdata.com>在消息中写道
新闻:d3 ************************** @ posting.google.c om ...
"Sambucus" <an***************@aetdata.com> wrote in message
news:d3**************************@posting.google.c om...
嗨组!
我正在使用C ++和Java与JNI在RICHEDIT中获取一些文本到我的程序中。我这样做是通过每秒访问一个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;
output =新的TCHAR [大小];
SendMessage(textarea,
WM_GETTEXT,
大小,
(LPARAM)(无效*)输出);
return env-> NewStringUTF(dealerStr);
}
------------------>
我试图释放使用JNI.h方法创建NewStringUTF时分配的内存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 my
java program. I do so by accessing a C++ method every second. It all
works 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 the
leakage is in the C++ or java code. But i guess it''s on the C++ side
since I''m kind of newbee there.
Is there perhaps a way to release the memory allocated for a LPTSTR in
C++?
Please take a look at the code below, it is the method i call every
second from java. I belive it is the LPTSTR that is leaking, since it
leaks more when the string gets larger.
<---------------
LPTSTR output;
JNIEXPORT jstring JNICALL
Java_JNIHandler_getDealerOutput(JNIEnv *env, jobject obj){
size = SendMessage(
(HWND) textarea, // handle to destination window
WM_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 is
made 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.
最后不是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 of
std::vector<TCHAR>. The basic string in C++ has more Java-like syntax,
e.g. the operator [ ] is range-checked, and concatenation can be done
with operator +.
Sambucus写道:
Sambucus wrote:
output = new TCHAR [size];
此商店已分配但从未获释。你需要代码在某个地方释放它,
喜欢:
delete []输出;
输出= 0; //或= NULL如果你愿意
返回env-> NewStringUTF(dealerStr);
output = new TCHAR[size];
This store is allocated but never freed. You need code to free it somewhere,
like:
delete [] output;
output = 0; // or = NULL if you prefer
return env->NewStringUTF(dealerStr);
dealerStr来自哪里来自?这应该是''输出''吗?我那么
你应该把''output''作为局部变量
Java_JNIHandler_getDealerOutput()。
我认为你也可能有问题将文本从窗口传递到
NewStringUTF(),该函数需要特定格式的字节,而不是
" just纯文本 (虽然在很多情况下它都适用于纯文本)。我不认为这会导致内存泄漏,只是奇怪。字符串从您的原生方法返回
(或者可能崩溃JVM ;-)
- chris
Where does dealerStr come from ? Is this supposed to be ''output'' ? I so then
you should probably make ''output'' be a local variable of
Java_JNIHandler_getDealerOutput().
I think you also may have problems with passing the text from the window to
NewStringUTF(), 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''t
think that''d be causing memory leaks, though, just "strange" Strings returned
from your native method (or possibly crashing the JVM ;-)
-- chris
>
John说了什么,但考虑使用std :: basic_string< TCHAR>而不是
std :: vector< TCHAR>。 C ++中的基本字符串具有更多类似Java的语法,例如,操作符[]是范围检查的,连接可以用操作符+来完成。
What John said, but consider using a std::basic_string<TCHAR> instead of
std::vector<TCHAR>. The basic string in C++ has more Java-like syntax,
e.g. the operator [ ] is range-checked, and concatenation can be done
with operator +.
但是std :: basic_string的问题是你不能得到一个可写的
指针。因此它不适合传递给SendMessage(win,
WM_GETTEXT ......)。
john
But the problem with std::basic_string is that you cannot get a writable
pointer from it. Therefore it''s not suitable for passing to SendMessage(win,
WM_GETTEXT ...).
john
这篇关于C ++ / JNI内存泄漏,需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!