From 511e48728a4b8f8736fcb2ea28dd1ab7cc699972 Mon Sep 17 00:00:00 2001 From: Diego Perez Date: Thu, 6 Aug 2015 14:39:06 +0100 Subject: Make DelegateManager thread safe We have been seeing crashes on the Theme editor related to an assertion on getDelegate when the object does not exist (http://b.android.com/181951). When debugging, the crash was happening without seeing a previous call to removeJavaReference. This seems to completely remove that crash. Since the DelegateManager can be called at least from two threads (main thread and the finalizer), it should be thread safe. Bug: http://b.android.com/181951 Change-Id: I1b28f863ff198f8592f170a98f9de391b2ac3ea2 --- .../android/layoutlib/bridge/impl/DelegateManager.java | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/impl/DelegateManager.java b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/impl/DelegateManager.java index 47258b6ac72b..baf2e2e11564 100644 --- a/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/impl/DelegateManager.java +++ b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/impl/DelegateManager.java @@ -97,13 +97,13 @@ public final class DelegateManager { * @return the delegate or null if not found. */ @Nullable - public T getDelegate(long native_object) { + public synchronized T getDelegate(long native_object) { if (native_object > 0) { - T delegate = mDelegates.get(native_object); + T delegate = mDelegates.get(native_object); if (Debug.DEBUG) { if (delegate == null) { - System.out.println("Unknown " + mClass.getSimpleName() + " with int " + + System.err.println("Unknown " + mClass.getSimpleName() + " with int " + native_object); } } @@ -119,14 +119,18 @@ public final class DelegateManager { * @param newDelegate the delegate to add * @return a unique native int to identify the delegate */ - public long addNewDelegate(T newDelegate) { + public synchronized long addNewDelegate(T newDelegate) { long native_object = ++mDelegateCounter; + mDelegates.put(native_object, newDelegate); assert !mJavaReferences.contains(newDelegate); mJavaReferences.add(newDelegate); if (Debug.DEBUG) { - System.out.println("New " + mClass.getSimpleName() + " with int " + native_object); + System.out.println( + "New " + mClass.getSimpleName() + " " + + "with int " + + native_object); } return native_object; @@ -136,7 +140,7 @@ public final class DelegateManager { * Removes the main reference on the given delegate. * @param native_object the native integer representing the delegate. */ - public void removeJavaReferenceFor(long native_object) { + public synchronized void removeJavaReferenceFor(long native_object) { T delegate = getDelegate(native_object); if (Debug.DEBUG) { -- cgit v1.2.3-59-g8ed1b