5454import java .util .Iterator ;
5555import java .util .List ;
5656import java .util .Map ;
57- import java .util .WeakHashMap ;
57+ import java .util .concurrent . ConcurrentHashMap ;
5858import java .util .logging .Level ;
5959import java .util .logging .Logger ;
6060import java .util .regex .*;
@@ -105,8 +105,7 @@ public class BridJ {
105105 static final Map <File , NativeLibrary > librariesByFile = new HashMap <File , NativeLibrary >();
106106 private static NativeEntities orphanEntities = new NativeEntities ();
107107 static final Map <Class <?>, BridJRuntime > classRuntimes = new HashMap <Class <?>, BridJRuntime >();
108- static final Map <Long , NativeObject > strongNativeObjects = new HashMap <Long , NativeObject >(),
109- weakNativeObjects = new WeakHashMap <Long , NativeObject >();
108+ static final Map <Long , NativeObject > strongNativeObjects = new HashMap <Long , NativeObject >();
110109
111110 public static long sizeOf (Type type ) {
112111 Class c = Utils .getClass (type );
@@ -140,25 +139,6 @@ public static long sizeOf(Type type) {
140139 throw new RuntimeException ("Unable to compute size of type " + Utils .toString (type ));
141140 }
142141
143- static synchronized void registerNativeObject (NativeObject ob ) {
144- weakNativeObjects .put (Pointer .getAddress (ob , null ), ob );
145- }
146- /// Caller should display message such as "target was GC'ed. You might need to add a BridJ.protectFromGC(NativeObject), BridJ.unprotectFromGC(NativeObject)
147-
148- static synchronized NativeObject getNativeObject (long peer ) {
149- NativeObject ob = weakNativeObjects .get (peer );
150- if (ob == null ) {
151- ob = strongNativeObjects .get (peer );
152- }
153- return ob ;
154- }
155-
156- static synchronized void unregisterNativeObject (NativeObject ob ) {
157- long peer = Pointer .getAddress (ob , null );
158- weakNativeObjects .remove (peer );
159- strongNativeObjects .remove (peer );
160- }
161-
162142 /**
163143 * Keep a hard reference to a native object to avoid its garbage
164144 * collection.<br>
@@ -167,7 +147,6 @@ static synchronized void unregisterNativeObject(NativeObject ob) {
167147 */
168148 public static synchronized <T extends NativeObject > T protectFromGC (T ob ) {
169149 long peer = Pointer .getAddress (ob , null );
170- weakNativeObjects .remove (peer );
171150 strongNativeObjects .put (peer , ob );
172151 return ob ;
173152 }
@@ -178,14 +157,15 @@ public static synchronized <T extends NativeObject> T protectFromGC(T ob) {
178157 */
179158 public static synchronized <T extends NativeObject > T unprotectFromGC (T ob ) {
180159 long peer = Pointer .getAddress (ob , null );
181- if (strongNativeObjects .remove (peer ) != null ) {
182- weakNativeObjects .put (peer , ob );
160+ NativeObject removed = strongNativeObjects .remove (peer );
161+ if (removed != ob ) {
162+ throw new IllegalStateException ("Unprotected object " + removed + " instead of " + ob + " for address " + peer );
183163 }
184164 return ob ;
185165 }
186166
187167 public static void delete (NativeObject nativeObject ) {
188- unregisterNativeObject ( nativeObject );
168+ BridJ . setJavaObjectFromNativePeer ( Pointer . getAddress ( nativeObject , null ), null );
189169 Pointer .getPointer (nativeObject , null ).release ();
190170 }
191171
@@ -253,18 +233,21 @@ public static boolean isCastingNativeObjectInCurrentThread() {
253233 public static boolean isCastingNativeObjectReturnTypeInCurrentThread () {
254234 return currentlyCastingNativeObject .get ().peek () == CastingType .CastingNativeObjectReturnType ;
255235 }
256- private static WeakHashMap <Long , NativeObject > knownNativeObjects = new WeakHashMap <Long , NativeObject >();
236+
237+
238+ private static final ConcurrentHashMap <Long , NativeObject > registeredObjects =
239+ new ConcurrentHashMap <Long , NativeObject >();
257240
258241 public static synchronized <O extends NativeObject > void setJavaObjectFromNativePeer (long peer , O object ) {
259242 if (object == null ) {
260- knownNativeObjects .remove (peer );
243+ registeredObjects .remove (peer );
261244 } else {
262- knownNativeObjects .put (peer , object );
245+ registeredObjects .put (peer , object );
263246 }
264247 }
265248
266249 public static synchronized Object getJavaObjectFromNativePeer (long peer ) {
267- return knownNativeObjects .get (peer );
250+ return registeredObjects .get (peer );
268251 }
269252
270253 private static <O extends NativeObject > O createNativeObjectFromPointer (Pointer <? super O > pointer , Type type , CastingType castingType ) {
@@ -628,7 +611,6 @@ public static synchronized NativeLibrary getNativeLibrary(AnnotatedElement type)
628611 */
629612 public synchronized static void releaseAll () {
630613 strongNativeObjects .clear ();
631- weakNativeObjects .clear ();
632614 gc ();
633615
634616 for (NativeLibrary lib : librariesByFile .values ()) {
@@ -856,6 +838,9 @@ public static File getNativeLibraryFile(String libraryName) {
856838 try {
857839 synchronized (nativeLibraryFiles ) {
858840 File nativeLibraryFile = nativeLibraryFiles .get (libraryName );
841+ if (debug ) {
842+ info ("Library named '" + libraryName + "' is associated to file '" + nativeLibraryFiles + "'" , null );
843+ }
859844 if (nativeLibraryFile == null ) {
860845 nativeLibraryFiles .put (libraryName , nativeLibraryFile = findNativeLibraryFile (libraryName ));
861846 }
@@ -931,6 +916,10 @@ static File findNativeLibraryFile(String libraryName) throws IOException {
931916 }
932917 }
933918 List <String > possibleFileNames = getPossibleFileNames (name );
919+ if (debug ) {
920+ info ("Possible file names for library '" + libraryName + "' with name '" + name + "': " + possibleFileNames , null );
921+ }
922+
934923 for (String path : paths ) {
935924 File pathFile = path == null ? null : new File (path );
936925 File f = new File (name );
0 commit comments