2017-08-24 46 views
2

我在我的应用程序中使用Facebook SDK v4.25.0进行登录。当我点击登录按钮并打开Facebook登录webview,现在我回来按回到登录,然后再按关闭应用程序。由于登录按钮导致内存泄漏。请帮我弄清楚这一点。提前致谢。如何解决Facebook登录按钮中的内存泄漏?

我使用下面的代码使用Facebook登录功能:

rlFacebook.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       callbackManager = CallbackManager.Factory.create(); 
       loginButton = new LoginButton(mActivity); 

       loginButton.setReadPermissions(Collections.singletonList("public_profile, email")); 
       loginButton.performClick(); 
       loginButton.setPressed(true); 
       loginButton.invalidate(); 
       loginButton.registerCallback(callbackManager, mCallBack); 
       loginButton.setPressed(false); 
       loginButton.invalidate(); 
      } 
     }); 

这里是我的注销代码的onStop方法:

@Override 
protected void onStop() { 
    super.onStop(); 

    if(callbackManager != null) 
     loginButton.unregisterCallback(callbackManager); 

    if(loginButton != null) 
     loginButton.invalidate(); 
} 

以下是我LeakCanary跟踪结果:

In com.xyz:1.0:1. 
* com.xyz.ui.Login has leaked: 
* GC ROOT static android.support.v4.content.LocalBroadcastManager.mInstance 
* references android.support.v4.content.LocalBroadcastManager.mReceivers 
* references java.util.HashMap.table 
* references array java.util.HashMap$HashMapEntry[].[5] 
* references java.util.HashMap$HashMapEntry.key 
* references com.facebook.AccessTokenTracker$CurrentAccessTokenBroadcastReceiver.this$0 
* references com.facebook.login.widget.LoginButton$2.this$0 (anonymous subclass of com.facebook.AccessTokenTracker) 
* references com.facebook.login.widget.LoginButton.mContext 
* leaks com.xyz.ui.Login instance 

* Retaining: 139 KB. 
* Reference Key: 2026fe1b-3d96-4e51-acd2-bc08a4ebee45 
* Device: motorola motorola Moto G Play harpia 
* Android Version: 6.0.1 API: 23 LeakCanary: 1.5.1 1be44b3 
* Durations: watch=636036ms, gc=141ms, heap dump=4181ms, analysis=301644ms 

* Details: 
* Class android.support.v4.content.LocalBroadcastManager 
| static DEBUG = false 
| static $staticOverhead = byte[40]@316392961 (0x12dbc601) 
| static MSG_EXEC_PENDING_BROADCASTS = 1 
| static mLock = [email protected] (0x12f5ecd0) 
| static mInstance = [email protected] (0x12ee06c0) 
| static TAG = [email protected] (0x12c5af40) 
* Instance of android.support.v4.content.LocalBroadcastManager 
| static DEBUG = false 
| static $staticOverhead = byte[40]@316392961 (0x12dbc601) 
| static MSG_EXEC_PENDING_BROADCASTS = 1 
| static mLock = [email protected] (0x12f5ecd0) 
| static mInstance = [email protected] (0x12ee06c0) 
| static TAG = [email protected] (0x12c5af40) 
| mActions = [email protected] (0x12ed4580) 
| mAppContext = [email protected] (0x12e5aae0) 
| mHandler = [email protected] (0x12ee06e0) 
| mPendingBroadcasts = [email protected] (0x12ee0700) 
| mReceivers = [email protected] (0x12ed45b0) 
| shadow$_klass_ = android.support.v4.content.LocalBroadcastManager 
| shadow$_monitor_ = 0 
* Instance of java.util.HashMap 
| static MINIMUM_CAPACITY = 4 
| static serialPersistentFields = java.io.ObjectStreamField[1]@1881581904 (0x7026a950) 
| static EMPTY_TABLE = java.util.HashMap$HashMapEntry[2]@1881560712 (0x70265688) 
| static serialVersionUID = 362498820763181265 
| static $staticOverhead = byte[48]@1882175441 (0x702fb7d1) 
| static MAXIMUM_CAPACITY = 1073741824 
| static DEFAULT_LOAD_FACTOR = 0.75 
| entryForNullKey = null 
| entrySet = null 
| keySet = null 
| modCount = 10 
| size = 4 
| table = java.util.HashMap$HashMapEntry[8]@317539808 (0x12ed45e0) 
| threshold = 6 
| values = null 
| keySet = null 
| valuesCollection = null 
| shadow$_klass_ = java.util.HashMap 
| shadow$_monitor_ = 0 
* Array of java.util.HashMap$HashMapEntry[] 
| [0] = null 
| [1] = [email protected] (0x12ee0720) 
| [2] = [email protected] (0x12ee0740) 
| [3] = null 
| [4] = null 
| [5] = [email protected] (0x12ee0760) 
| [6] = null 
| [7] = null 
* Instance of java.util.HashMap$HashMapEntry 
| hash = 501765157 
| key = com.[email protected]317589376 (0x12ee0780) 
| next = [email protected] (0x12ee07a0) 
| value = [email protected] (0x12ee07c0) 
| shadow$_klass_ = java.util.HashMap$HashMapEntry 
| shadow$_monitor_ = 0 
* Instance of com.facebook.AccessTokenTracker$CurrentAccessTokenBroadcastReceiver 
| this$0 = [email protected] (0x12ee0860) 
| mDebugUnregister = false 
| mPendingResult = null 
| shadow$_klass_ = com.facebook.AccessTokenTracker$CurrentAccessTokenBroadcastReceiver 
| shadow$_monitor_ = -2087220166 
* Instance of com.facebook.login.widget.LoginButton$2 
| this$0 = [email protected] (0x12de9800) 
| broadcastManager = [email protected] (0x12ee06c0) 
| isTracking = true 
| receiver = com.[email protected]317589376 (0x12ee0780) 
| shadow$_klass_ = com.facebook.login.widget.LoginButton$2 
| shadow$_monitor_ = 0 
* Instance of com.facebook.login.widget.LoginButton 
| static $staticOverhead = byte[8]@315707393 (0x12d15001) 
| static TAG = [email protected] (0x12f61ca0) 
| accessTokenTracker = [email protected] (0x12ee0860) 
| confirmLogout = true 
| loginLogoutEventName = [email protected] (0x12c89f40) 
| loginManager = [email protected] (0x12ee1150) 
| loginText = null 
| logoutText = null 
| properties = [email protected]89632 (0x12ee0880) 
| toolTipChecked = false 
| toolTipDisplayTime = 6000 
| toolTipMode = [email protected] (0x12ee08a0) 
| toolTipPopup = null 
| toolTipStyle = [email protected] (0x12ee1160) 
| analyticsButtonCreatedEventName = [email protected] (0x12c89f00) 
| analyticsButtonTappedEventName = [email protected] (0x12c98b80) 
| externalOnClickListener = null 
| internalOnClickListener = [email protected]20 (0x12ee1170) 
| overrideCompoundPadding = false 
| overrideCompoundPaddingLeft = 0 
| overrideCompoundPaddingRight = 0 
| parentFragment = null 
| mAllowTransformationLengthChange = false 
| mAutoLinkMask = 0 
| mBoring = null 
| mBreakStrategy = 0 
| mBufferType = [email protected] (0x7026dd00) 
| mChangeWatcher = null 
| mCharWrapper = null 
| mCurHintTextColor = 1627389952 
| mCurTextColor = -2 
| mCurrentSpellCheckerLocaleCache = null 
| mCursorDrawableRes = 17303275 
| mDeferScroll = -1 
| mDesiredHeightAtMeasure = -1 
| mDispatchTemporaryDetach = false 
| mDrawables = [email protected] (0x12e5db80) 
| mEditableFactory = [email protected] (0x702a92c8) 
| mEditor = null 
| mEllipsize = null 
| mFilters = android.text.InputFilter[0]@1947019624 (0x740d2968) 
| mFirstTouch = false 
| mFreezesText = false 
| mGravity = 17 
| mHighlightColor = 1714105645 
| mHighlightPaint = [email protected] (0x12e62e80) 
| mHighlightPath = null 
| mHighlightPathBogus = true 
| mHint = null 
| mHintBoring = null 
| mHintLayout = null 
| mHintTextColor = [email protected] (0x12ed4670) 
| mHorizontallyScrolling = false 
| mHyphenationFrequency = 0 
| mIncludePad = true 
| mLastLayoutDirection = -1 
| mLastScroll = 0 
| mLastTouchUpTime = 0 
| mLayout = null 
| mLinkTextColor = [email protected] (0x12ed46a0) 
| mLinksClickable = true 
| mListeners = null 
| mLocaleChanged = false 
| mMarquee = null 
| mMarqueeFadeMode = 0 
| mMarqueeRepeatLimit = 3 
| mMaxMode = 1 
| mMaxWidth = 2147483647 
| mMaxWidthMode = 2 
| mMaximum = 2147483647 
| mMinMode = 1 
| mMinWidth = 0 
| mMinWidthMode = 2 
| mMinimum = 0 
| mMovement = null 
| mOldMaxMode = 1 
| mOldMaximum = 2147483647 
| mPreDrawListenerDetached = false 
| mPreDrawRegistered = false 
| mPreventDefaultMovement = false 
| mRestartMarquee = false 
| mSavedHintLayout = null 
| mSavedLayout = null 
| mSavedMarqueeModeLayout = null 
| mScroller = null 
| mShadowColor = 0 
| mShadowDx = 0.0 
| mShadowDy = 0.0 
| mShadowRadius = 0.0 
| mSingleLine = false 
| mSpacingAdd = 0.0 
| mSpacingMult = 1.0 
| mSpannableFactory = [email protected] (0x702aaf10) 
| mTempRect = null 
| mTemporaryDetach = false 
| mText = [email protected] (0x12e48b80) 
| mTextColor = [email protected] (0x12ed46d0) 
| mTextDir = null 
| mTextEditSuggestionItemLayout = 17367278 
| mTextPaint = [email protected] (0x12ee50b0) 
| mTextSelectHandleLeftRes = 17303280 
| mTextSelectHandleRes = 17303283 
| mTextSelectHandleRightRes = 17303286 
| mTransformation = null 
| mTransformed = [email protected] (0x12e48b80) 
| mUserSetTextScaleX = false 
| mAccessibilityCursorPosition = -1 
| mAccessibilityDelegate = null 
| mAccessibilityTraversalAfterId = -1 
| mAccessibilityTraversalBeforeId = -1 
| mAccessibilityViewId = -1 
| mAnimator = null 
| mAttachInfo = null 
| mAttributes = null 
| mBackground = [email protected] (0x12e62ee0) 
| mBackgroundRenderNode = null 
| mBackgroundResource = 2130837629 
| mBackgroundSizeChanged = true 
| mBackgroundTint = null 
| mBottom = 0 
| mCachingFailed = false 
| mClipBounds = null 
| mContentDescription = null 
| mContext = [email protected] (0x12ee6200) 
| mCurrentAnimation = null 
| mDrawableState = int[2]@317589696 (0x12ee08c0) 
| mDrawingCache = null 
| mDrawingCacheBackgroundColor = 0 
| mFloatingTreeObserver = null 
| mForegroundInfo = null 
| mGhostView = null 
| mHasPerformedLongPress = false 
| mID = -1 
| mIgnoreNextUpEvent = false 
| mInContextButtonPress = false 
| mInputEventConsistencyVerifier = null 
| mKeyedTags = null 
| mLabelForId = -1 
| mLastIsOpaque = false 
| mLayerPaint = null 
| mLayerType = 0 
| mLayoutInsets = null 
| mLayoutParams = null 
| mLeft = 0 
| mLeftPaddingDefined = true 
| mListenerInfo = [email protected] (0x12e58e20) 
| mMatchIdPredicate = null 
| mMatchLabelForPredicate = null 
| mMeasureCache = null 
| mMeasuredHeight = 0 
| mMeasuredWidth = 0 
| mMinHeight = 0 
| mMinWidth = 0 
| mNestedScrollingParent = null 
| mNextFocusDownId = -1 
| mNextFocusForwardId = -1 
| mNextFocusLeftId = -1 
| mNextFocusRightId = -1 
| mNextFocusUpId = -1 
| mOldHeightMeasureSpec = -2147483648 
| mOldWidthMeasureSpec = -2147483648 
| mOutlineProvider = [email protected] (0x7026dbf0) 
| mOverScrollMode = 1 
| mOverlay = null 
| mPaddingBottom = 10 
| mPaddingLeft = 12 
| mPaddingRight = 12 
| mPaddingTop = 10 
| mParent = null 
| mPendingCheckForLongPress = null 
| mPendingCheckForTap = null 
| mPerformClick = null 
| mPrivateFlags = -2128605184 
| mPrivateFlags2 = 1188872 
| mPrivateFlags3 = 0 
| mRecreateDisplayList = false 
| mRenderNode = [email protected] (0x12ee08e0) 
| mResources = [email protected] (0x12e4dc40) 
| mRight = 0 
| mRightPaddingDefined = true 
| mScrollCache = null 
| mScrollIndicatorDrawable = null 
| mScrollX = 0 
| mScrollY = 0 
| mSendViewScrolledAccessibilityEvent = null 
| mSendViewStateChangedAccessibilityEvent = null 
| mSendingHoverAccessibilityEvents = false 
| mStartActivityRequestWho = null 
| mStateListAnimator = null 
| mSystemUiVisibility = 0 
| mTag = null 
| mTempNestedScrollConsumed = null 
| mTop = 0 
| mTouchDelegate = null 
| mTouchSlop = 16 
| mTransformationInfo = null 
| mTransientStateCount = 0 
| mTransitionName = null 
| mUnscaledDrawingCache = null 
| mUnsetPressedState = null 
| mUserPaddingBottom = 10 
| mUserPaddingEnd = -2147483648 
| mUserPaddingLeft = 12 
| mUserPaddingLeftInitial = 12 
| mUserPaddingRight = 12 
| mUserPaddingRightInitial = 12 
| mUserPaddingStart = -2147483648 
| mVerticalScrollFactor = 0.0 
| mVerticalScrollbarPosition = 0 
| mViewFlags = 402669569 
| mWindowAttachCount = 0 
| shadow$_klass_ = com.facebook.login.widget.LoginButton 
| shadow$_monitor_ = 0 
* Instance of com.xyz.ui.Login 
| static $staticOverhead = byte[16]@316633089 (0x12df7001) 
| static serialVersionUID = 8377811411653900550 
| static $change = null 
| callbackManager = [email protected] (0x12ee1190) 
| etEmail = [email protected] (0x12de9c00) 
| etPassword = [email protected] (0x12dea000) 
| isButtonChecked = false 
| loginButton = [email protected] (0x12de9800) 
| mActivity = [email protected] (0x12ee6200) 
| mCallBack = [email protected] (0x12ee11a0) 
| socialType = 0 
| bmpUser = null 
| dialogShown = false 
| imgCaptureImage = null 
| imgExtra = [email protected] (0x12e0d600) 
| imgMenu = [email protected] (0x12e0d800) 
| imgRefresh = [email protected] (0x12e0da00) 
| imgUser = null 
| mActionBarAutoHideEnabled = false 
| mActionBarToolbar = [email protected] (0x12dea400) 
| mActivity = [email protected] (0x12ee6200) 
| mDeferredOnDrawerClosedRunnable = null 
| mDrawerLayout = null 
| mHandler = [email protected] (0x12ee0900) 
| mNavDrawerItemViews = null 
| mNavDrawerItems = [email protected] (0x12ee0920) 
| mNormalStatusBarColor = -16777216 
| mThemedStatusBarColor = -16777216 
| objInputMethodManager = null 
| resEntity = null 
| rlExtra = [email protected] (0x12dea800) 
| selectedImagePath = [email protected] (0x6ff357e8) 
| txtNotificationBg = [email protected] (0x12deac00) 
| txtSave = [email protected] (0x12deb000) 
| txtTitle = [email protected] (0x12deb400) 
| viewExtra = [email protected] (0x12c9c620) 
| mDelegate = [email protected] (0x12e824f0) 
| mEatKeyUpEvent = false 
| mResources = null 
| mThemeId = 2131427473 
| mCreated = true 
| mFragments = [email protected] (0x12ee11b0) 
| mHandler = [email protected] (0x12ee0940) 
| mNextCandidateRequestIndex = 0 
| mOptionsMenuInvalidated = false 
| mPendingFragmentActivityResults = [email protected] (0x12ee0960) 
| mReallyStopped = true 
| mRequestedPermissionsFromFragment = false 
| mResumed = false 
| mRetaining = false 
| mStopped = true 
| mStartedActivityFromFragment = false 
| mStartedIntentSenderFromFragment = false 
| mExtraDataMap = [email protected] (0x12ee0980) 
| mActionBar = null 
| mActionModeTypeStarting = 0 
| mActivityInfo = [email protected] (0x12e5dc00) 
| mActivityTransitionState = [email protected] (0x12e6df00) 
| mApplication = [email protected] (0x12e5aae0) 
| mCalled = true 
| mChangeCanvasToTranslucent = false 
| mChangingConfigurations = false 
| mComponent = [email protected] (0x12ee11c0) 
| mConfigChangeFlags = 0 
| mCurrentConfig = [email protected] (0x12ee5120) 
| mDecor = null 
| mDefaultKeyMode = 0 
| mDefaultKeySsb = null 
| mDestroyed = true 
| mDoReportFullyDrawn = false 
| mEmbeddedID = null 
| mEnableDefaultActionBarUp = false 
| mEnterTransitionListener = [email protected] (0x7026e540) 
| mExitTransitionListener = [email protected] (0x7026e540) 
| mFinished = true 
| mFragments = [email protected] (0x12ee11d0) 
| mHandler = [email protected] (0x12ee09a0) 
| mHasCurrentPermissionsRequest = false 
| mIdent = 67478760 
| mInstanceTracker = [email protected] (0x12ee11e0) 
| mInstrumentation = [email protected] (0x12e58e70) 
| mIntent = [email protected] (0x12e6df40) 
| mLastNonConfigurationInstances = null 
| mMainThread = [email protected] (0x12c03100) 
| mManagedCursors = [email protected] (0x12ee09c0) 
| mManagedDialogs = null 
| mMenuInflater = null 
| mParent = null 
| mReferrer = [email protected] (0x12e6df80) 
| mResultCode = 0 
| mResultData = null 
| mResumed = false 
| mSearchEvent = null 
| mSearchManager = null 
| mStartedActivity = false 
| mStopped = true 
| mTemporaryPause = false 
| mTitle = [email protected] (0x12e551c0) 
| mTitleColor = 0 
| mTitleReady = true 
| mToken = [email protected] (0x12ee09e0) 
| mTranslucentCallback = null 
| mUiThread = [email protected] (0x73fce2a0) 
| mVisibleBehind = false 
| mVisibleFromClient = true 
| mVisibleFromServer = true 
| mVoiceInteractor = null 
| mWindow = [email protected] (0x12c485a0) 
| mWindowAdded = true 
| mWindowManager = [email protected] (0x12ee0a00) 
| mInflater = [email protected] (0x12ed4700) 
| mOverrideConfiguration = null 
| mResources = [email protected] (0x12e4dc40) 
| mTheme = [email protected] (0x12ee0a20) 
| mThemeResource = 2131427473 
| mBase = [email protected] (0x12e5dc80) 
| shadow$_klass_ = com.xyz.ui.Login 
| shadow$_monitor_ = -2015956359 
* Excluded Refs: 
| Field: android.view.inputmethod.InputMethodManager.mNextServedView 
| Field: android.view.inputmethod.InputMethodManager.mServedView 
| Field: android.view.inputmethod.InputMethodManager.mServedInputConnection 
| Field: android.view.inputmethod.InputMethodManager.mCurRootView 
| Field: android.os.UserManager.mContext 
| Field: android.net.ConnectivityManager.sInstance 
| Field: android.view.Choreographer$FrameDisplayEventReceiver.mMessageQueue (always) 
| Thread:FinalizerWatchdogDaemon (always) 
| Thread:main (always) 
| Thread:LeakCanary-Heap-Dump (always) 
| Class:java.lang.ref.WeakReference (always) 
| Class:java.lang.ref.SoftReference (always) 
| Class:java.lang.ref.PhantomReference (always) 
| Class:java.lang.ref.Finalizer (always) 
| Class:java.lang.ref.FinalizerReference (always) 
+0

你不应该注销mCallback? –

+0

@TimCastelijns请检查我编辑的问题。 – Philliphe

回答

0

我有同样的问题,但我决定做一个克隆,因为我无法修复漏洞

XML 
<Button android:id="@+id/facebook_button" 
      android:drawableStart="@drawable/com_facebook_button_icon_white" 
      android:background="@color/com_facebook_blue" 
      android:text=" Continue with Facebook" 
      android:textStyle="bold" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:gravity="center" 
      android:layout_gravity="center_horizontal" 
      android:layout_marginBottom="8dp" 
      android:layout_marginEnd="8dp" 
      android:layout_marginStart="8dp" 
      android:layout_marginTop="8dp" /> 

然后使用LoginManager来处理回调。 登录用户与:

LoginManager.getInstance() 
      .logInWithReadPermissions(this, Arrays.asList("public_profile")); 
OnClickListener

。 希望它有帮助! :)