473,322 Members | 1,719 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

C++ / JNI memory leakage, help needed

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
Jul 22 '05 #1
32 4818
John Harrison wrote:
"Sambucus" <an***************@aetdata.com> wrote in message
news:d3**************************@posting.google.c om...
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.

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.


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 +.
Jul 22 '05 #2
Sambucus wrote:
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);


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

Jul 22 '05 #3
>
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 +.


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
Jul 22 '05 #4

"Sambucus" <an***************@aetdata.com> wrote in message
news:d3**************************@posting.google.c om...
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.


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.

john
Jul 22 '05 #5
John Harrison wrote:
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 +.

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 ...).


Thanks for clarifying, John. I don't know about SendMessage, and there
was no prototype in the OP. I see now that the string was given no
initializer, so it will need to be written.

Why use a vector, though, instead of an automatic array?
Jul 22 '05 #6
John Harrison wrote:
"Sambucus" <an***************@aetdata.com> wrote in message
news:d3**************************@posting.google.c om...
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.

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.


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 +.
Jul 22 '05 #7

"Jeff Schwab" <je******@comcast.net> wrote in message
news:Q4********************@comcast.com...
John Harrison wrote:
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 +.

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 ...).


Thanks for clarifying, John. I don't know about SendMessage, and there
was no prototype in the OP. I see now that the string was given no
initializer, so it will need to be written.

Why use a vector, though, instead of an automatic array?


Because the size isn't known at compile time. Some compilers support arrays
whose size is not known until runtime but its not standard C++.

This is all Windows detail but

SendMessage(textarea, WM_GETTEXTLENGTH, ...

gets the length of text and

SendMessage(textarea, WM_GETTEXT, ...

gets the actual characters.

john
Jul 22 '05 #8
>
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 +.


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
Jul 22 '05 #9
John Harrison wrote:
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 +.

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 ...).


Thanks for clarifying, John. I don't know about SendMessage, and there
was no prototype in the OP. I see now that the string was given no
initializer, so it will need to be written.

Why use a vector, though, instead of an automatic array?
Jul 22 '05 #10

"Jeff Schwab" <je******@comcast.net> wrote in message
news:Q4********************@comcast.com...
John Harrison wrote:
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 +.

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 ...).


Thanks for clarifying, John. I don't know about SendMessage, and there
was no prototype in the OP. I see now that the string was given no
initializer, so it will need to be written.

Why use a vector, though, instead of an automatic array?


Because the size isn't known at compile time. Some compilers support arrays
whose size is not known until runtime but its not standard C++.

This is all Windows detail but

SendMessage(textarea, WM_GETTEXTLENGTH, ...

gets the length of text and

SendMessage(textarea, WM_GETTEXT, ...

gets the actual characters.

john
Jul 22 '05 #11
Chris Uppal wrote:
Sambucus wrote:

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


This isn't java related but alwaise set your variables to NULL, since
some platforms could exist that NULL is not defined as 0x0.
Jul 22 '05 #12
Chris Uppal wrote:
Sambucus wrote:

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


This isn't java related but alwaise set your variables to NULL, since
some platforms could exist that NULL is not defined as 0x0.
Jul 22 '05 #13
On Sat, 03 Apr 2004 17:07:10 +0000, Yoyoma_2 wrote:
This isn't java related but alwaise set your variables to NULL, since
some platforms could exist that NULL is not defined as 0x0.


I'm sure I've seen someone else on this group before say "Always set your
variables to 0 rather than to NULL, some platforms may exist where NULL
isn't defined as 0".

James

Jul 22 '05 #14
On Sat, 03 Apr 2004 17:07:10 +0000, Yoyoma_2 wrote:
This isn't java related but alwaise set your variables to NULL, since
some platforms could exist that NULL is not defined as 0x0.


I'm sure I've seen someone else on this group before say "Always set your
variables to 0 rather than to NULL, some platforms may exist where NULL
isn't defined as 0".

James

Jul 22 '05 #15
James Gregory wrote:
On Sat, 03 Apr 2004 17:07:10 +0000, Yoyoma_2 wrote:

This isn't java related but alwaise set your variables to NULL, since
some platforms could exist that NULL is not defined as 0x0.

I'm sure I've seen someone else on this group before say "Always set your
variables to 0 rather than to NULL, some platforms may exist where NULL
isn't defined as 0".

James


And they were right.

Always use 0. The address represented by 0 is guaranteed in C++ to be
the "null" value, whatever bit pattern that happens to be.
Jul 22 '05 #16
James Gregory wrote:
On Sat, 03 Apr 2004 17:07:10 +0000, Yoyoma_2 wrote:

This isn't java related but alwaise set your variables to NULL, since
some platforms could exist that NULL is not defined as 0x0.

I'm sure I've seen someone else on this group before say "Always set your
variables to 0 rather than to NULL, some platforms may exist where NULL
isn't defined as 0".

James


And they were right.

Always use 0. The address represented by 0 is guaranteed in C++ to be
the "null" value, whatever bit pattern that happens to be.
Jul 22 '05 #17
James Gregory wrote:
On Sat, 03 Apr 2004 17:07:10 +0000, Yoyoma_2 wrote:

This isn't java related but alwaise set your variables to NULL, since
some platforms could exist that NULL is not defined as 0x0.

I'm sure I've seen someone else on this group before say "Always set your
variables to 0 rather than to NULL, some platforms may exist where NULL
isn't defined as 0".


Which group are you talking above (the list is pretty extensive).

If you alwaise test on NULL then you are fine. What happends if you
need to write to the 0x0 memory area in an albeit wierd platform. Using
the NULL definition will eliviate that. Its also good practice. To me
this is the same thing as doing sizeof( int ) instead of just putting
"4". Cross-platform issues. This is what i've been tought but i am not
professing it as necessarly right.

James

Jul 22 '05 #18
James Gregory wrote:
On Sat, 03 Apr 2004 17:07:10 +0000, Yoyoma_2 wrote:

This isn't java related but alwaise set your variables to NULL, since
some platforms could exist that NULL is not defined as 0x0.

I'm sure I've seen someone else on this group before say "Always set your
variables to 0 rather than to NULL, some platforms may exist where NULL
isn't defined as 0".


Which group are you talking above (the list is pretty extensive).

If you alwaise test on NULL then you are fine. What happends if you
need to write to the 0x0 memory area in an albeit wierd platform. Using
the NULL definition will eliviate that. Its also good practice. To me
this is the same thing as doing sizeof( int ) instead of just putting
"4". Cross-platform issues. This is what i've been tought but i am not
professing it as necessarly right.

James

Jul 22 '05 #19
John Harrison wrote:
"Sambucus" <an***************@aetdata.com> wrote in message
news:d3**************************@posting.google.c om...
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.

finally is not part of the C++ language

Try this

jstring res = env->NewStringUTF(dealerStr);
delete[] output;


It's been a while since I last used JNI, but I think that strings that
you create using NewStringUTF must be deleted (I don't remember the
syntax for this).

Check Sun's web site for more details on NewStringUTF...

JLR
Jul 22 '05 #20
John Harrison wrote:
"Sambucus" <an***************@aetdata.com> wrote in message
news:d3**************************@posting.google.c om...
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.

finally is not part of the C++ language

Try this

jstring res = env->NewStringUTF(dealerStr);
delete[] output;


It's been a while since I last used JNI, but I think that strings that
you create using NewStringUTF must be deleted (I don't remember the
syntax for this).

Check Sun's web site for more details on NewStringUTF...

JLR
Jul 22 '05 #21
Yoyoma_2 wrote:
James Gregory wrote:
[...]
If you alwaise test on NULL then you are fine. What happends if you
need to write to the 0x0 memory area in an albeit wierd platform.


You are wrong. The value 0 when used as a pointer has special defined
semantics in both C and C++. Check the standards for the details.

I'd write more except that:

(a) this is cross-posted into areas where nobody is even slightly interested in
the oddities of the C/C++ standards.

(b) it's a few years since I last attempted public pedantry on these topics, so
I'm a little rusty...

-- chris
Jul 22 '05 #22
Yoyoma_2 wrote:
James Gregory wrote:
[...]
If you alwaise test on NULL then you are fine. What happends if you
need to write to the 0x0 memory area in an albeit wierd platform.


You are wrong. The value 0 when used as a pointer has special defined
semantics in both C and C++. Check the standards for the details.

I'd write more except that:

(a) this is cross-posted into areas where nobody is even slightly interested in
the oddities of the C/C++ standards.

(b) it's a few years since I last attempted public pedantry on these topics, so
I'm a little rusty...

-- chris
Jul 22 '05 #23
James Gregory wrote:
On Sat, 03 Apr 2004 17:07:10 +0000, Yoyoma_2 wrote:

This isn't java related but alwaise set your variables to NULL, since
some platforms could exist that NULL is not defined as 0x0.

I'm sure I've seen someone else on this group before say "Always set your
variables to 0 rather than to NULL, some platforms may exist where NULL
isn't defined as 0".


NULL must be a constant expression equal to 0. There is absolutely no
difference between using NULL and 0.

4.10 Pointer conversions [conv.ptr]

1 A null pointer constant is an integral constant expression
(_expr.const_) rvalue of integer type that evaluates to zero.

18.1 Types [lib.support.types]

[...]

4 The macro NULL is an implementation-defined C++ null pointer constant
in this International Standard (_conv.ptr_).180)

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Jul 22 '05 #24
James Gregory wrote:
On Sat, 03 Apr 2004 17:07:10 +0000, Yoyoma_2 wrote:

This isn't java related but alwaise set your variables to NULL, since
some platforms could exist that NULL is not defined as 0x0.

I'm sure I've seen someone else on this group before say "Always set your
variables to 0 rather than to NULL, some platforms may exist where NULL
isn't defined as 0".


NULL must be a constant expression equal to 0. There is absolutely no
difference between using NULL and 0.

4.10 Pointer conversions [conv.ptr]

1 A null pointer constant is an integral constant expression
(_expr.const_) rvalue of integer type that evaluates to zero.

18.1 Types [lib.support.types]

[...]

4 The macro NULL is an implementation-defined C++ null pointer constant
in this International Standard (_conv.ptr_).180)

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Jul 22 '05 #25
>
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.

john


You are certainly corect about me beeing a Java programer and not C++
;)
I tried the first approach and it worked fine, thank you very much
John!¨

However i had tried to use something like this:

return env->NewStringUTF(output);

}
__finally
{
output= NULL;
delete [] output;
size=NULL;

....and it did not work, i guess it has to do with the __finally not
beeing part of c++? but why don't I get compilation a error?

//Andreas
Jul 22 '05 #26
>
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.

john


You are certainly corect about me beeing a Java programer and not C++
;)
I tried the first approach and it worked fine, thank you very much
John!¨

However i had tried to use something like this:

return env->NewStringUTF(output);

}
__finally
{
output= NULL;
delete [] output;
size=NULL;

....and it did not work, i guess it has to do with the __finally not
beeing part of c++? but why don't I get compilation a error?

//Andreas
Jul 22 '05 #27
> It's been a while since I last used JNI, but I think that strings that
you create using NewStringUTF must be deleted (I don't remember the
syntax for this).

Check Sun's web site for more details on NewStringUTF...

JLR


Well i stumbled on info regarding this appearantly by Sun undocumented
memory leakage in JNI when trying to solve my problem.

However this is only when you are sending a java String to the C dll.
In the C code you have to get a C string from the Java UTF chars...
then you must use "env -> ReleaseStringUTFChars(str,output)" to
release it
Jul 22 '05 #28
> It's been a while since I last used JNI, but I think that strings that
you create using NewStringUTF must be deleted (I don't remember the
syntax for this).

Check Sun's web site for more details on NewStringUTF...

JLR


Well i stumbled on info regarding this appearantly by Sun undocumented
memory leakage in JNI when trying to solve my problem.

However this is only when you are sending a java String to the C dll.
In the C code you have to get a C string from the Java UTF chars...
then you must use "env -> ReleaseStringUTFChars(str,output)" to
release it
Jul 22 '05 #29
lev
function c_str() returns const <CHAR_TYPE> *
"John Harrison" <jo*************@hotmail.com> wrote in message
news:c4*************@ID-196037.news.uni-berlin.de...

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 +.
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

Jul 22 '05 #30
lev
function c_str() returns const <CHAR_TYPE> *
"John Harrison" <jo*************@hotmail.com> wrote in message
news:c4*************@ID-196037.news.uni-berlin.de...

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 +.
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

Jul 22 '05 #31

"lev" <lt*********@brandsoft.com> wrote in message
news:1gZbc.182893$Cb.1698658@attbi_s51...
function c_str() returns const <CHAR_TYPE> *


which is not a *writable* pointer

john
Jul 22 '05 #32

"lev" <lt*********@brandsoft.com> wrote in message
news:1gZbc.182893$Cb.1698658@attbi_s51...
function c_str() returns const <CHAR_TYPE> *


which is not a *writable* pointer

john
Jul 22 '05 #33

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: frustrated | last post by:
Before I begin, I must first make the following disclaimer: Although I have considerable programming experience, I do not consider myself by any means to be an expert C++ programmer. The following...
2
by: Sambucus | last post by:
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...
7
by: andylcx | last post by:
hi all: I have a code like below, is there any serious memory leakage in my code. I am confusion now but no idea how to fix it. In the member function of class A, I create a new object of class B...
10
by: s.subbarayan | last post by:
Dear all, I happen to come across this exciting inspiring article regarding memory leaks in this website: http://www.embedded.com/story/OEG20020222S0026 In this article the author mentions:...
18
by: Ramasubbu Ramasubramanian XR (AS/EAB) | last post by:
What is memory leakage, could any one explain with sample code
4
by: NewToCPP | last post by:
Is there any debugging mechanism to find out which part of the code is causing memory leak?
0
by: kiran kumar | last post by:
Hi All, I am working on embedded python on C these days. I feel there is a memory leakage in this code. I have used our own memory pool and all the python code will use the heap from this memory...
14
by: madhawi | last post by:
i want to know that on what situation memory leakage happan and what is the solution to solve the problem of memory leakage.
27
by: George2 | last post by:
Hello everyone, Should I delete memory pointed by pointer a if there is bad_alloc when allocating memory in memory pointed by pointer b? I am not sure whether there will be memory leak if I do...
11
by: prpradip | last post by:
I have an ImageList (_imageList). In _imageList I have put large numbers of Icons. Now what I need is to get Handle of all Icons that I put in _imageList, so that I can destroy (DestoryIcon) them...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.