2

JNR has this thing called the ObjectReferenceManager that makes keeping track of references really convenient. An object is passed to it and a unique id (a Pointer, but it's really just an id afaik) is passed back that can be used to kill the reference later on. While I could easily implement this functionality with a map, a free list, and an integer counter, if JNA has some sort of blessed implementation that looks prettier I'd like to use that instead.

user
  • 4,920
  • 3
  • 25
  • 38
  • IIUC the intent is to be able to pass around references to Java objects in native code (nominally for later retrieval in a callback)? – technomage Oct 09 '14 at 01:15
  • No, just to keep things from getting garbage collected. I *would* like that extra bit of behavior, as it opens up some other doors, but I don't need it. This question is mostly to see if there exists something that I can use from JNA that'd make one of my source files look less haphazardly organized (heck, even a vanilla standard library Java class would work; I'm not an experienced Java person). – user Oct 09 '14 at 04:20
  • Just drop them into a `HashSet` or a `HashMap`. That will keep strong references to the objects (as long as you keep a strong reference to the set or map). Usually the problem in Java is keeping a reference to things *without* preventing them from being GCed. – technomage Oct 09 '14 at 17:19
  • Troof, but I'd still need to keep a key so I can kill the reference later (hence my little bit about the free list and integer counter). Ah well - so much for whimsically perfect ready-made solutions. :-/ – user Oct 10 '14 at 03:36
  • If you're tracking what you need to free then you might as well keep the original reference (as opposed to an integer ID). I was thinking more along the lines of "I'm done using this library, dump all its stuff", in which case you could just clear the set/map which holds all the references. – technomage Oct 10 '14 at 10:19

0 Answers0