473,769 Members | 7,810 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 _getDealerOutpu t(JNIEnv *env, jobject obj){

size = SendMessage(
(HWND) textarea, // handle to destination window
WM_GETTEXTLENGT H, // message to send
(WPARAM) 0,
(LPARAM) 0 ) +1;

output = new TCHAR[size];

SendMessage(tex tarea,
WM_GETTEXT,
size,
(LPARAM)(void*) output);

return env->NewStringUTF(d ealerStr);

}
------------------>

I have tried to release the memory allocated when the NewStringUTF is
made with the JNI.h method env -> ReleaseStringUT FChars(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 4891
John Harrison wrote:
"Sambucus" <an************ ***@aetdata.com > wrote in message
news:d3******** *************** ***@posting.goo gle.com...
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_JNIHandl er_getDealerOut put(JNIEnv *env, jobject obj){

size = SendMessage(
(HWND) textarea, // handle to destination window
WM_GETTEXTLEN GTH, // message to send
(WPARAM) 0,
(LPARAM) 0 ) +1;

output = new TCHAR[size];

SendMessage(t extarea,
WM_GETTEXT,
size,
(LPARAM)(void *)output);

return env->NewStringUTF(d ealerStr);

}
------------------>

I have tried to release the memory allocated when the NewStringUTF is
made with the JNI.h method env -> ReleaseStringUT FChars(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(d ealerStr);
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(tex tarea, WM_GETTEXTLENGT H, 0, 0);
std::vector<TCH AR> output(size + 1);
SendMessage(tex tarea, 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_stri ng<TCHAR> instead of
std::vector<TCH AR>. 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(d ealerStr);


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 _getDealerOutpu t().

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_stri ng<TCHAR> instead of
std::vector<TCH AR>. 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_stri ng 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.goo gle.com...
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 _getDealerOutpu t(JNIEnv *env, jobject obj){

size = SendMessage(
(HWND) textarea, // handle to destination window
WM_GETTEXTLENGT H, // message to send
(WPARAM) 0,
(LPARAM) 0 ) +1;

output = new TCHAR[size];

SendMessage(tex tarea,
WM_GETTEXT,
size,
(LPARAM)(void*) output);

return env->NewStringUTF(d ealerStr);

}
------------------>

I have tried to release the memory allocated when the NewStringUTF is
made with the JNI.h method env -> ReleaseStringUT FChars(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(d ealerStr);
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(tex tarea, WM_GETTEXTLENGT H, 0, 0);
std::vector<TCH AR> output(size + 1);
SendMessage(tex tarea, 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_stri ng<TCHAR> instead of
std::vector<T CHAR>. 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_stri ng 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.goo gle.com...
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_JNIHandl er_getDealerOut put(JNIEnv *env, jobject obj){

size = SendMessage(
(HWND) textarea, // handle to destination window
WM_GETTEXTLEN GTH, // message to send
(WPARAM) 0,
(LPARAM) 0 ) +1;

output = new TCHAR[size];

SendMessage(t extarea,
WM_GETTEXT,
size,
(LPARAM)(void *)output);

return env->NewStringUTF(d ealerStr);

}
------------------>

I have tried to release the memory allocated when the NewStringUTF is
made with the JNI.h method env -> ReleaseStringUT FChars(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(d ealerStr);
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(tex tarea, WM_GETTEXTLENGT H, 0, 0);
std::vector<TCH AR> output(size + 1);
SendMessage(tex tarea, 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_stri ng<TCHAR> instead of
std::vector<TCH AR>. 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******@comca st.net> wrote in message
news:Q4******** ************@co mcast.com...
John Harrison wrote:
What John said, but consider using a std::basic_stri ng<TCHAR> instead of
std::vector<T CHAR>. 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_stri ng 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(tex tarea, WM_GETTEXTLENGT H, ...

gets the length of text and

SendMessage(tex tarea, WM_GETTEXT, ...

gets the actual characters.

john
Jul 22 '05 #8
>
What John said, but consider using a std::basic_stri ng<TCHAR> instead of
std::vector<TCH AR>. 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_stri ng 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_stri ng<TCHAR> instead of
std::vector<T CHAR>. 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_stri ng 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

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

Similar topics

2
3735
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 may be nothing more than a relection of my ignorance. If what I describe is not an actual bug, I would be very appreciative if you could briefly explain to me how I can de-allocate memory allocated by a set class, since everything I have tried is...
2
1223
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 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.
7
1778
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 like below: void A::function() { B *newobject = new B; newobject->....; newobject->....; //did some action here
10
2781
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: "At a certain point in the code you may be unsure if a particular block is no longer needed. If you free() this piece of memory, but continue to access it (probably via a second pointer to the same
18
2272
by: Ramasubbu Ramasubramanian XR (AS/EAB) | last post by:
What is memory leakage, could any one explain with sample code
4
2303
by: NewToCPP | last post by:
Is there any debugging mechanism to find out which part of the code is causing memory leak?
0
1370
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 pool. RunScript(pScriptName,pFuncName,...) { PyEval_AcquireLock() threadState = Py_NewInterpreter(); PyThreadState_Swap(threadState);
14
2349
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
2967
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 not delete a. try { a = new int ;
11
2308
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 all and release the memory because it's causing Memory leakage.
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10049
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9998
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8876
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3967
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3567
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.