BenLand100
2011-09-08 08:31:10
"Can you add something to SmartFreeObject that keeps it from crashing or add some way to check if an object is valid?"
The answer? Unequivocally:
"No."
Why? Simple, a valid object is one that JNI returns via SMART when you call some method with the words "Get" and "Object" in it. Valid objects are not going to be -1, -2, or 0 as those are defined as "Error" values by either SMART or JNI. JNI specifies that you SHOULD NOT under any circumstance pass an invalid object to JNI methods because the virtual machine is written to be fast, not tediously error checking every object. If you pass an invalid object (read: invalid memory address) you WILL crash the process. You have the ability to keep track of what's valid or not, I have no way to check validity besides maintaining a list of returned objects and checking to see if they have been freed yet (I'm not going to do this, so don't bother asking), so stop being lazy and write good code. People use JNI all the time without problems, SMART reflection methods are thinly wrapped calls to JNI which are thinly wrapped uses of the java.lang.reflect API. (see the source https://github.com/BenLand100/SMART/). You should have no problems with this if you're doing it right.
tl;dr GOOD PROGRAMMERS know how to manage memory properly and shouldn't ever ask this question.