2016-08-30 105 views
1

即時通訊使用泄漏金絲雀檢測內存泄漏,我必須說這是非常好的庫檢測潛在的內存泄漏下面是由它產生的泄漏報告。我無法找到如何刪除這個匿名實現。匿名實現導致內存泄漏

GoProFragment滲漏:

  • GC ROOT java.util.Timer中的$ TimerImpl。
  • 引用MainActivity $ 5,本$ 0(java.util.TimerTask中的匿名子類)
  • 引用MainActivity.mHelper
  • 引用IabHelper.mPurchaseListener
  • 引用GoProFragment $ 2,本$ 0(匿名執行IabHelper $ OnIabPurchaseFinishedListener的)
  • GoProFragment例如
**GoProFragment.java** 
public class GoProFragment extends BaseFragment { 

    @Bind(R.id.btn_subscription_yearly) 
    Button btnsubScriptionYearly; 
    @Bind(R.id.btn_subscription_monthly) 
    Button monthly; 
    private ProgressDialog progressDialog; 
    String product_price_one, product_price_two, product_price_three; 
    @Bind(R.id.subscription_type) 
    TextView subScriptionType; 
    @Bind(R.id.graph_paid_feature) 
    ImageView imageView; 
    Bitmap icon; 


    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.pro_layout, container, false); 
     ButterKnife.bind(this, view); 
     setHasOptionsMenu(true); 
     activity.preferences.setDisplayUpgrade(false); 
     icon = BitmapFactory.decodeResource(activity.getResources(), R.drawable.subscription_img_two); 
     imageView.setImageBitmap(icon); 
     subScriptionType.setText(activity.preferences.getSubscriptionType()); 
     String[] sku = { Util.SKU_CHALLENGE, Util.SKU_SUBSCRIPTION, Util.SKU_YEAR_SUBSCRIPTION}; 
     if(isNetworkAvailable(activity)) { 
      activity.mHelper.queryInventoryAsync(true, Arrays.asList(sku), mGotInventoryListener); 
      showProgressDialog(); 
     } 
     if (activity.preferences.isHasSubscription()) { 
      /*btnsubScriptionYearly.setVisibility(View.GONE); 
      monthly.setVisibility(View.GONE);*/ 
     } 
     return view; 
    } 
    IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() { 
     @Override 
     public void onQueryInventoryFinished(IabResult result, Inventory inventory) { 
      dismissProgressDialog(); 
      product_price_one = inventory.getSkuDetails(Util.SKU_SUBSCRIPTION).getPrice(); 
      product_price_two = inventory.getSkuDetails(Util.SKU_YEAR_SUBSCRIPTION).getPrice(); 
      product_price_three = inventory.getSkuDetails(Util.SKU_CHALLENGE).getPrice(); 
      monthly.setVisibility(View.VISIBLE); 
      btnsubScriptionYearly.setVisibility(View.VISIBLE); 
      monthly.setText(activity.getString(R.string.monthly_subscription_text, product_price_one)); 
      btnsubScriptionYearly.setText(activity.getString(R.string.yearly_subscription_text, product_price_two)); 

     } 

    }; 

    @OnClick(R.id.btn_subscription_monthly) 
    public void onSubscription() { 
     if (Validator.isNotNull(activity.mHelper)) { 
      if (!activity.mHelper.subscriptionsSupported()) { 
       Util.showToast(activity, getString(R.string.subscription_not_supported)); 
       return; 
      } 
      if (!activity.mHelper.getAsyncInProgress()) { 
       activity.mHelper.launchPurchaseFlow(activity, 
         Util.SKU_SUBSCRIPTION, IabHelper.ITEM_TYPE_SUBS, 
         Util.RC_REQUEST, mPurchaseFinishedListener, Util.PAYLOAD); 
      } 
     } 
    } 

    @OnClick(R.id.btn_subscription_yearly) 
    public void onYearSubscription() { 
     if (Validator.isNotNull(activity.mHelper)) { 
      if (!activity.mHelper.subscriptionsSupported()) { 
       Util.showToast(activity, getString(R.string.subscription_not_supported)); 
       return; 
      } 
      if (!activity.mHelper.getAsyncInProgress()) { 
       activity.mHelper.launchPurchaseFlow(activity, 
         Util.SKU_YEAR_SUBSCRIPTION, IabHelper.ITEM_TYPE_SUBS, 
         Util.RC_REQUEST, mPurchaseFinishedListener, Util.PAYLOAD); 
      } 
     } 
    } 

    private boolean isNetworkAvailable(Context context) { 
     final ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
     if (connectivity != null) { 
      NetworkInfo[] info = connectivity.getAllNetworkInfo(); 
      if (info != null) 
       for (int i = 0; i < info.length; i++) 
        if (info[i].getState() == NetworkInfo.State.CONNECTED) { 
         return true; 
        } 
     } 

     return false; 
    } 
    IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() { 
     public void onIabPurchaseFinished(IabResult result, Purchase purchase) { 

      // if we were disposed of in the meantime, quit. 
      if (activity.mHelper == null) { 
       return; 
      } 

      if (result.isFailure()) { 
       return; 
      } 
      if (! Util.verifyDeveloperPayload(purchase)) { 
       return; 
      } 
      if (purchase.getSku().equals(Util.SKU_SUBSCRIPTION)) { 
       // bought the infinite gas subscription 
       setPremium(purchase, Calendar.MONTH); 
      } else if (purchase.getSku().equals(Util.SKU_YEAR_SUBSCRIPTION)) { 
       // bought the infinite gas subscription 
       setPremium(purchase, Calendar.YEAR); 
      } 
     } 
    }; 
    public void setPremium(Purchase purchase, int duration) { 
     if (Util.verifyDeveloperPayload(purchase)) { 
      activity.preferences.setHasSubscription(true); 
      if (Validator.isNotNull(activity.adView)) { 
       activity.adView.setVisibility(View.GONE); 
      } 
      Util.showToast(activity, getString(R.string.upgraded_successfully)); 
      Calendar calendar = Calendar.getInstance(); 
      calendar.setTime(new Date(purchase.getPurchaseTime())); 
      calendar.add(duration, 1); 
      activity.preferences.getUserInfo().setSubscriptionEndOn(calendar.getTime()); 
      if(purchase.getSku().equals(Util.SKU_SUBSCRIPTION)) 
      { 
       activity.preferences.setSubscriptionType(activity.getResources().getString(R.string.monthly)); 
      } 
      else { 
       activity.preferences.setSubscriptionType(activity.getResources().getString(R.string.yearly)); 
      } 
      activity.preferences.getUserInfo().setPremium(true); 
      //subscriptionEndOn.setText(dateFormat.format(activity.preferences.getUserInfo().getSubscriptionEndOn())); 
     } 
    } 

    private void showProgressDialog() { 
     if (!activity.isFinishing() && progressDialog == null) { 
      progressDialog = new ProgressDialog(activity,R.style.CustomProgressDialog); 
      progressDialog.setCancelable(false); 
      progressDialog.show(); 

     } 

    } 

    /** 
    * dismiss Progress Dialog. 
    */ 
    private void dismissProgressDialog() { 
     if (!activity.isFinishing() && progressDialog != null && progressDialog.isShowing()) { 
      progressDialog.dismiss(); 
      progressDialog=null; 
     } 
    } 

    @Override 
    public void onDetach() { 
     if(Validator.isNotNull(icon)){ 
      icon.recycle(); 
     } 
     super.onDetach(); 
    } 
+0

那麼你的問題是什麼? –

+0

@ bub如何解決此漏洞 –

回答

1

我不認爲這是一個問題。您可以在片段中保持狀態,但您應該在onCreate回撥中調用setRetainInstance(true);

this article

另外,您可以設置在onDetach回調領域mGotInventoryListener = null;

+0

@阿德里安科斯特我試過了,但它仍然泄漏 –