Service() 的上下文会导致“此字段泄漏上下文对象”吗?

你好CW

我读过文章

此字段泄漏上下文对象

这个字段泄露了一个上下文对象

通过构造函数将上下文从主活动传递到 Java 类时,获取“此字段泄漏上下文对象”

代码private var mService: RecordService? = null显示警告“此字段泄漏上下文对象”为什么?

我认为HomeViewModel对象销毁后对象会被销毁Activity,并且mService也会被释放,为什么Android Studio会显示“此字段泄漏上下文对象”

class HomeViewModel(private val mApplication: Application, private val mDBVoiceRepository: DBVoiceRepository) : AndroidViewModel(mApplication) {

    private var mService: RecordService? = null

    private val serviceConnection = object : ServiceConnection {
        override fun onServiceConnected(className: ComponentName, iBinder: IBinder) {
            val binder = iBinder as RecordService.MyBinder
            mService = binder.service
           
        }      
    }

   fun bindService() {
        Intent(mApplication , RecordService::class.java).also { intent ->
            mApplication.bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE)
        }
    }

    fun unbindService() {
        Intent(mApplication, RecordService::class.java).also { intent ->
            mApplication.unbindService(serviceConnection)
            mService?.stopForeground(true)
            mService?.stopSelf()
        }
    }
    ...
}



class RecordService : Service() {
    override fun onBind(intent: Intent): IBinder {
        return MyBinder()
    }

    inner class MyBinder : Binder() {
        val service: RecordService
            get() = this@RecordService
    }

    ...
}

添加的内容

致 AlphaOne:谢谢!

当我使用以下代码时,我仍然收到警告“此字段泄漏上下文对象”,如果需要调用RecordServiceHomeViewModel怎么办?

    class HomeViewModel(private val mApplication: Application, 
                        private val mDBVoiceRepository: DBVoiceRepository,
                        private var mService: RecordService?) : 
             AndroidViewModel(mApplication) {
    
    
    }

还有,下面的代码是 的源代码AndroidViewModel,看来Android并没有为这个类做额外的工作AndroidViewModel,似乎是android阻止了显示警告信息。

public class AndroidViewModel extends ViewModel {
    @SuppressLint("StaticFieldLeak")
    private Application mApplication;

    public AndroidViewModel(@NonNull Application application) {
        mApplication = application;
    }

    /**
     * Return the application.
     */
    @SuppressWarnings({"TypeParameterUnusedInFormals", "unchecked"})
    @NonNull
    public <T extends Application> T getApplication() {
        return (T) mApplication;
    }
}
阿尔法一号

来自官方文档:

ViewModel 绝不能引用视图、生命周期或任何可能持有对活动上下文的引用的类。

由于服务有自己的生命周期并由操作系统管理,因此您不应将其引用保留在 ViewModel 中。

配置更改时,ViewModel 被保留,所以仍然可能有对Service 的引用,会导致内存泄漏。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用Hilt注入上下文:此字段泄漏上下文对象

“此字段泄漏上下文对象”警告 Hilt Injection

该字段泄漏上下文对象

此类泄漏上下文对象吗?

Azure App Service(Mobile)上的db上下文失败

具有活动上下文的处理程序类会导致内存泄漏吗?

我在泄漏上下文吗?

Servlet.service()进行的servlet [调度]在上下文中与路径[/ ***]扔例外

Java在没有HttpServletRequest的Spring Service中获取上下文URL

getSystemService(SENSOR_SERVICE)遇到问题-已实现上下文

jQuery“此”上下文

带有泄漏上下文的静态字段

如何防止静态字段中的上下文类导致的内存泄漏

Android:由于活动和上下文存储为对象属性而导致内存泄漏?

将上下文传递给android活动中的helper类会泄漏吗?

事件上下文与 LitElement 泄漏

重度:Servlet.service()进行的servlet [ProfileServlet]与路径[/作业]扔例外java.lang.NumberFormatException上下文:空

严重:路径为[/ DEV-JDBC]的上下文中的Servlet [SqlGatewayServlet]的Servlet.service()抛出异常

C#中WinForms Apps和Windows Service上下文之间的一致ApplicationData路径

严重:路径为[/ portal]的上下文中Servlet [package]的Servlet.service()抛出异常java.lang.NullPointerException

如何将Service Fabric服务上下文注入asp.net核心中间件?

Xtext ScopeProvider:访问上下文对象会导致错误

在 getDefaultSharedPreferences 上传递的上下文会影响结果吗?

原型与“此”上下文混淆

此上下文的正确请求类型?

mousemove事件,访问此上下文

揭示原型“此”上下文

反应-上下文

上下文菜单