@@ -2065,31 +2065,31 @@ private void cancelSelectDialog() {
20652065 */
20662066 @ Override
20672067 public void destroy () {
2068- destroyImpl ();
2069- }
2070-
2071- private void destroyImpl () {
2072- ViewRootImpl viewRoot = mWebView .getViewRootImpl ();
2073- if (viewRoot != null ) {
2068+ if (mWebView .getViewRootImpl () != null ) {
20742069 Log .e (LOGTAG , "Error: WebView.destroy() called while still attached!" );
20752070 }
2071+ ensureFunctorDetached ();
2072+ destroyJava ();
2073+ destroyNative ();
2074+ }
20762075
2076+ private void ensureFunctorDetached () {
20772077 if (mWebView .isHardwareAccelerated ()) {
20782078 int drawGLFunction = nativeGetDrawGLFunction (mNativeClass );
2079+ ViewRootImpl viewRoot = mWebView .getViewRootImpl ();
20792080 if (drawGLFunction != 0 && viewRoot != null ) {
2080- // functor should have been detached in onDetachedFromWindow, do
2081- // additionally here for safety
20822081 viewRoot .detachFunctor (drawGLFunction );
20832082 }
20842083 }
2084+ }
20852085
2086+ private void destroyJava () {
20862087 mCallbackProxy .blockMessages ();
20872088 clearHelpers ();
20882089 if (mListBoxDialog != null ) {
20892090 mListBoxDialog .dismiss ();
20902091 mListBoxDialog = null ;
20912092 }
2092- if (mNativeClass != 0 ) nativeStopGL ();
20932093 if (mWebViewCore != null ) {
20942094 // Tell WebViewCore to destroy itself
20952095 synchronized (this ) {
@@ -2100,12 +2100,36 @@ private void destroyImpl() {
21002100 // Remove any pending messages that might not be serviced yet.
21012101 mPrivateHandler .removeCallbacksAndMessages (null );
21022102 }
2103- if (mNativeClass != 0 ) {
2104- nativeDestroy ();
2105- mNativeClass = 0 ;
2103+ }
2104+
2105+ private void destroyNative () {
2106+ if (mNativeClass == 0 ) return ;
2107+ int nptr = mNativeClass ;
2108+ mNativeClass = 0 ;
2109+ if (Thread .currentThread () == mPrivateHandler .getLooper ().getThread ()) {
2110+ // We are on the main thread and can safely delete
2111+ nativeDestroy (nptr );
2112+ } else {
2113+ mPrivateHandler .post (new DestroyNativeRunnable (nptr ));
21062114 }
21072115 }
21082116
2117+ private static class DestroyNativeRunnable implements Runnable {
2118+
2119+ private int mNativePtr ;
2120+
2121+ public DestroyNativeRunnable (int nativePtr ) {
2122+ mNativePtr = nativePtr ;
2123+ }
2124+
2125+ @ Override
2126+ public void run () {
2127+ // nativeDestroy also does a stopGL()
2128+ nativeDestroy (mNativePtr );
2129+ }
2130+
2131+ }
2132+
21092133 /**
21102134 * See {@link WebView#enablePlatformNotifications()}
21112135 */
@@ -4099,14 +4123,7 @@ public void refreshPlugins(boolean reloadOpenPages) {
40994123 @ Override
41004124 protected void finalize () throws Throwable {
41014125 try {
4102- if (mNativeClass != 0 ) {
4103- mPrivateHandler .post (new Runnable () {
4104- @ Override
4105- public void run () {
4106- destroy ();
4107- }
4108- });
4109- }
4126+ destroy ();
41104127 } finally {
41114128 super .finalize ();
41124129 }
@@ -5313,13 +5330,7 @@ public void onDetachedFromWindow() {
53135330
53145331 updateHwAccelerated ();
53155332
5316- if (mWebView .isHardwareAccelerated ()) {
5317- int drawGLFunction = nativeGetDrawGLFunction (mNativeClass );
5318- ViewRootImpl viewRoot = mWebView .getViewRootImpl ();
5319- if (drawGLFunction != 0 && viewRoot != null ) {
5320- viewRoot .detachFunctor (drawGLFunction );
5321- }
5322- }
5333+ ensureFunctorDetached ();
53235334 }
53245335
53255336 @ Override
@@ -8508,7 +8519,7 @@ public static void setShouldMonitorWebCoreThread() {
85088519
85098520 private native void nativeCreate (int ptr , String drawableDir , boolean isHighEndGfx );
85108521 private native void nativeDebugDump ();
8511- private native void nativeDestroy ();
8522+ private static native void nativeDestroy (int ptr );
85128523
85138524 private native void nativeDraw (Canvas canvas , RectF visibleRect ,
85148525 int color , int extra );
@@ -8527,7 +8538,7 @@ private native boolean nativeSetBaseLayer(int nativeInstance,
85278538 private native int nativeGetBaseLayer (int nativeInstance );
85288539 private native void nativeCopyBaseContentToPicture (Picture pict );
85298540 private native boolean nativeHasContent ();
8530- private native void nativeStopGL ();
8541+ private native void nativeStopGL (int ptr );
85318542 private native void nativeDiscardAllTextures ();
85328543 private native void nativeTileProfilingStart ();
85338544 private native float nativeTileProfilingStop ();
0 commit comments