【问题标题】:Pushwoosh leaking my activityPushwoosh 泄露了我的活动
【发布时间】:2015-11-17 11:09:34
【问题描述】:

我在我的应用程序中使用 Pushwoosh 来接收推送通知。我正在使用最新版本的 Pushwoosh 库 3.1.14。

我有这样的屏幕结构。

Login Activity -> Main Activity with multiple tabs.

所以我正在 MainActivity 中实现我的 pushwoosh 相关逻辑。我想从注销推送中注销,然后返回登录活动。

我的代码如下。我已经过滤掉了与 Pushwoosh 无关的所有其他部分。坦率地说,此代码与 Pushwoosh 文档 here 中的代码完全相同。唯一的区别在于 onLogout() 方法,我尝试从 pushwoosh 注销并返回 LoginActivity。

TabbarActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);

   //Pushwoosh Registration
   registerReceivers();
   PushManager pushManager = PushManager.getInstance(this);
   pushManager.setNotificationFactory(new PushNotificationFactory());
   try {
       pushManager.onStartup(this);
   } catch(Exception e) {}

   //Register for push!
   pushManager.registerForPushNotifications();
   checkMessage(getIntent());
} 


@Override 
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);    
    setIntent(intent);    
    checkMessage(intent);
}

@Override
protected void onResume() {    
    super.onResume();    
    registerReceivers();
}

@Override
protected void onPause() {    
    super.onPause();    
    unregisterReceivers();
}

BroadcastReceiver mBroadcastReceiver = new BaseRegistrationReceiver() {    
   @Override    
   public void onRegisterActionReceive(Context context, Intent intent) {        
       checkMessage(intent);    
   }
};

private BroadcastReceiver mReceiver = new BasePushMessageReceiver() {
   @Override    protected void onMessageReceive(Intent intent) {
     //JSON_DATA_KEY contains JSON payload of push notification.    
   }
};

public void registerReceivers() {
  IntentFilter intentFilter = new IntentFilter(
    getPackageName() + ".action.PUSH_MESSAGE_RECEIVE");
  registerReceiver(mReceiver, intentFilter, 
    getPackageName() +".permission.C2D_MESSAGE", null);    
  registerReceiver(mBroadcastReceiver, new IntentFilter(
    getPackageName() + "." + PushManager.REGISTER_BROAD_CAST_ACTION));
}

public void unregisterReceivers() {
  try {
    unregisterReceiver(mReceiver);    
  } catch (Exception e) {
    e.printStackTrace();    
  }

  try {        
    unregisterReceiver(mBroadcastReceiver);
  } catch (Exception e) {
    e.printStackTrace();   
  }
}

private void checkMessage(Intent intent) {
  if (null != intent) {
    if (intent.hasExtra(PushManager.REGISTER_EVENT)) {
       uploadPushTokenToServer(PushManager.getPushToken(this));
    }
    resetIntentValues();    
  }
}

private void resetIntentValues() {
  Intent mainAppIntent = getIntent();
  if (mainAppIntent.hasExtra(PushManager.PUSH_RECEIVE_EVENT)) {
    mainAppIntent.removeExtra(PushManager.PUSH_RECEIVE_EVENT);    
  } else if (mainAppIntent.hasExtra(PushManager.REGISTER_EVENT)) {
    mainAppIntent.removeExtra(PushManager.REGISTER_EVENT);
  } else if (mainAppIntent.hasExtra(PushManager.UNREGISTER_EVENT)) {
    mainAppIntent.removeExtra(PushManager.UNREGISTER_EVENT);
  } else if (mainAppIntent.hasExtra(PushManager.REGISTER_ERROR_EVENT)) {
    mainAppIntent.removeExtra(PushManager.REGISTER_ERROR_EVENT);
  } else if (mainAppIntent.hasExtra(PushManager.UNREGISTER_ERROR_EVENT)) {
    mainAppIntent.removeExtra(PushManager.UNREGISTER_ERROR_EVENT);
  }
  setIntent(mainAppIntent);
}

//Finally on logout
private void onLogout() {
   //other cleanup

   //pushwoosh
   PushManager.getInstance(this).unregisterForPushNotifications();

   //goback to login activity
}

我从服务器收到推送,没有任何问题。我面临的唯一问题是在我注销并返回 LoginActivity 后,TabbarActivity 仍保留在内存中,而这又会保留许多其他片段和视图。我尝试使用 MAT 进行调试,结果就是这样。

Class Name                                                                      | Ref. Objects | Shallow Heap | Ref. Shallow Heap | Retained Heap
--------------------------------------------------------------------------------------------------------------------------------------------------
com.pushwoosh.internal.request.RequestManager$1 @ 0x12f89ce0  Thread-1737 Thread|            1 |           88 |               360 |           536
'- val$context in.myproject.activities.TabbarActivity         @ 0x12d8ac40      |            1 |          360 |               360 |        18,520
--------------------------------------------------------------------------------------------------------------------------------------------------

我还用 LeakCanary 工具交叉检查了相同的内容,这也表明 Pushwoosh 正在保留我的活动。

所以我的问题是,我怎样才能清理 pushwoosh 以避免我的活动被泄露?

【问题讨论】:

  • 有几个地方可以将this 传递给 Pushwoosh。将每一个替换为getApplicationContext()。不要猜测。只需更换它们。如果方法签名没有采用Context,但实际上采用了Activity,则保留它。如果完成后泄漏仍然存在,那么无论您没有更改为 getApplicationContext() 都是您的问题,这代表 Pushwoosh 中的一个错误,Pushwoosh 需要修复。

标签: android android-activity memory-management memory-leaks pushwoosh


【解决方案1】:

您所引用的文档(通过简要查看它们)给出了一个示例,这些示例并不总是将 api 实现为具有其他活动的功能齐全的应用程序的最佳方式。我了解通过使用 getInstance,您正在尝试使用单例,但怀疑这没有得到很好的管理。

我将控制在您的应用运行期间使用的 PushManager 实例。

问题可能是从范围创建 PushManager 的多个实例,并在类中创建 pushManager 的多个实例,并且可能在程序的生命周期内创建。这会导致泄漏。

我会将 pushManager 设为类变量,而不是使用 PushManager.getInstance 两次,并考虑创建 PushManager 的静态实例以在应用程序期间使用,就像在整个应用程序中使用单个数据库实例一样。

在班级层面:

PushManager pushManager;

并在 oncreate 中初始化

pushManager = PushManager.getInstance(this);


//Finally on logout
private void onLogout() {
    //other cleanup

    //pushwoosh
    // Here the first instance is left dangling.
    // PushManager.getInstance(this).unregisterForPushNotifications();

    pushManager..unregisterForPushNotifications();

    //goback to login activity
}

这样你就清理了一个 pushmanager 实例的资源。

要使用应用范围的静态 PushManager:

static PushManager pushManager;

初始化为:

pushManager = new PushManager(this.getApplicationContext());

When is a Singleton not a Singleton?

【讨论】:

  • 我能理解你的想法。发布此问题后,我对此进行了更多研究。 1) Pushwoosh 示例应用程序完全遵循文档中提到的过程,并且它也泄漏;) 2) 尽管 pushwoosh.getInstance() 将活动上下文作为参数,但我认为它仅用于获取其定义中的应用程序上下文。所以 pushwoosh 在内部使用了应用上下文。
  • 我尝试在任何地方传递 getApplicationContext() 并正确接收通知。但是我在 pushwoosh sdk 中遇到了与唤醒锁相关的随机崩溃。因此,使用 getApplicationContext() 会触发 pushwoosh 库中的其他一些错误。我已联系 pushwoosh 支持并等待他们的回复。
  • 同时,我将赏金奖励给您,因为您首先回答了问题,并且两个答案基本上都在说同样的事情。我暂时不接受答案,因为我还在等待 Pushwoosh 的回复。当我收到他们的回复时,请确保我会在这里更新。
【解决方案2】:

@CommonsWare 的评论很到位。查看 Pushwoosh SDK 的(反编译的)源代码,PushManager.onStartUp() 将提供的上下文直接转发到其RequestManager,然后将其移交给基本上无限运行的Thread。这意味着它会在失效后很长时间内挂在您的活动实例上。

请注意这正是 MAT 试图告诉您的内容(LeakCanary 也可能如此)。

换句话说,在您的应用程序的整个生命周期内,您传递给onStartUp() 的任何内容都会在内部保留一个强引用。因此,请确保您提供的上下文具有适当范围的生命周期。换句话说:这里 only 正确的选项是应用程序上下文。

您可能想向 Pushwoosh 提交错误报告并告知他们问题是:

public void onStartup(Context context) throws Exception {
    Context applicationContext = context.getApplicationContext();
    this.pushRegistrar.checkDevice(applicationContext);
    sendAppOpen(context); // <--- ISSUE
    ...
}

我只能猜测有人在办公室度过了糟糕的一天,忘记将有问题的行改为sendAppOpen(applicationContext)

没有保证所有泄漏都会在此更改后成为历史(我没有深入挖掘源头),但它至少应该解决手头的直接问题。

此外,这一点怎么强调都不过分,一般来说,如果您不知道(或控制)组件的生命周期,请使用应用程序上下文。如果真的需要某个活动,则方法签名将/应该指出这一点。如果它只是询问上下文,请谨慎行事。 (是的,当然,这个经验法则有很多例外,但通常更容易追踪问题,而不是内存泄漏)。

【讨论】:

  • 我尝试在任何地方传递 getApplicationContext() 并正确接收通知。但是我在 pushwoosh sdk 中遇到了与唤醒锁相关的随机崩溃。因此,使用 getApplicationContext() 会触发 pushwoosh 库中的其他一些错误。我已联系 pushwoosh 支持并等待他们的回复。
  • @Krishnabhadra:我很想看看那些随机崩溃的轨迹。我又快速查看了代码,但没有看到任何与上下文相关的明显会生成异常的内容。再说一次,如果我对要查找的内容有更好的了解... ;) 无论如何,请考虑打开一个或多个 tickets here,以便我们(以及其他可能遇到类似问题的人)更容易跟踪正在发生的事情.
  • @MrsEd:没错,CommonsWare 在他的评论中也是如此。我想我的方法与您的方法略有不同,因为我试图查明问题的原因并从那里开始工作。最后,结论几乎是一样的。 :)
  • @MsYvette Pushwoosh 解决了这个问题。请在下面查看我的答案。
【解决方案3】:

嗯,我收到了 Pushwoosh 的回复,说他们已经解决了这个问题。我下载了他们最新的 SDK,瞧,泄漏消失了。似乎用户@MH 发现了罪魁祸首代码。

这是来自新SDK的反编译源代码,

public void onStartup(Context context) throws Exception {
    Context applicationContext = context.getApplicationContext();
    this.pushRegistrar.checkDevice(applicationContext);
    sendAppOpen(applicationContext); // <--- NO ISSUE
    ...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-24
    • 2012-04-04
    • 2012-07-03
    • 1970-01-01
    • 2018-01-26
    • 1970-01-01
    相关资源
    最近更新 更多