aspose file tools*
The moose likes Java in General and the fly likes JNI - native methods allocating memory Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "JNI - native methods allocating memory" Watch "JNI - native methods allocating memory" New topic
Author

JNI - native methods allocating memory

Frank Hale
Ranch Hand

Joined: Mar 25, 2000
Posts: 230
Okay I am having a problem freeing memory that a native method allocates.
Let me explain this function. Okay GetLongHandle() returns a java int which contains the pointer to an XEvent union. I cast the handle received by GetLongHandle() to (XEvent*) and try to free the memory with free() which was previously allocated by malloc(). This crashes the VM and leaves me with a 100 meg core file. Is there any other way I can free this memory because I know for a fact I allocated it with malloc().
Here is the native method which tries to free the memory:

Can somebody give me some insight to what I am doing wrong?
For what its worth here is the native method which allocates the memory:

Thanks,
Frank
Apu Nahasapeemapetilon
Ranch Hand

Joined: Sep 06, 2000
Posts: 51
Why are you using java for native calls. Just write a native program to do what you want,
Use java for non-native functionality.
Frank Hale
Ranch Hand

Joined: Mar 25, 2000
Posts: 230
Since your reply sounds very offensive in that you automatically assume that I know nothing about Java. You also assume that just because I am programming in Java I should always want to write 100% Pure Java code. However your wrong, there is this standard interface called the JNI (Java Native Interface) which lets people like me (albeit a bad person for mixing Java and Native code) access external libraries which are written in another language. One of my biggest dislikes is when people respond to questions I've asked with non answers. Put yourself in my shoes and read your response to my question. Does this answer the question I asked? No, thats because you replied to my question with a non answer. If I wanted to write a native program would I have asked this question in the first place?
And for the record, I'm using Java to make native calls because I want to access a C library from Java.
Apu Nahasapeemapetilon
Ranch Hand

Joined: Sep 06, 2000
Posts: 51
Why you find my answer offensive, I will leave to you, as it is of little concern to me.
As far as giving you a non-answer, you do not seem to understand my answer.
I am well aware of JNI and have actually used it (now I bad person, too). But the fact of the matter is JNI was implemented for people to implement java improperly. So why are you implementing java improperly?
So, let's see if I understand your problem. You are generating a native memory leak through JNI calls (that crashes JVM)and you kinda sorta want to know why?
Let's review my answer

Why are you using java for native calls. Just write a native program to do what you want,
Use java for non-native functionality.

Granted my punctuation is off, but the question "Why are you using java for native calls?" is valid and central to your problem. Now consider your drivel about non-answers, why do you reply without answering this question?
Wait, your native method allocate memory, but does not seem to ever free this memory.
Forget it, Apu will waste no more time on this....
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
There are a lot of considerations that go into what language to use in a project. I can easily imagine a situation where you want to use Java as much as possible (and maybe a lot of code is already written in that language and you don't want to have to rewrite it, or lose the other benefits Java provides) but you also need to integrate some functionality that someone has already implemented in C. It may be more elegant or "proper" to some to rewrite all the code in one language or another, but sometimes it just isn't practical.
I'm sorry I don't have a good answer to Frank's question offhand, and little time at the moment to look into it. Perhaps one of our other posters will be able to provide a helpful insight. Good luck.


"I'm not back." - Bill Harding, Twister
Frank Hale
Ranch Hand

Joined: Mar 25, 2000
Posts: 230
Actually I have already figured out what the problem was. My original question is quite old. I wasn't correctly passing the pointer to java. I think I was trying to store it in a long or something and the conversion was fudging up when I was sending it back to my C code. Anyway I've since fixed the problem.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JNI - native methods allocating memory