Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Leak when child dependency is provided to a parent dependency that has a longer lifecycle #188

Closed
eygraber opened this issue Feb 28, 2022 · 0 comments · Fixed by #201
Closed
Labels
bug Something isn't working

Comments

@eygraber
Copy link
Contributor

If a child dependency is provided to a parent dependency, and that parent has a longer lifecycle than the child (e.g. the parent is scoped to an Android application, and the child is scoped to an Android activity), then the child dependency can leak (in the example below, the activity context is leaked):

@Inject
class ResolverWrapper(val resolver: ContentResolver)

@Singleton
@Component abstract class ParentComponent {
  @Provides @Singleton fun providesContentResolver(
    context: Context
  ): ContentResolver = context.contentResolver
}

@Component abstract class ChildComponent(
  @Component val parent: ParentComponent,
  @get:Provides val activityContext: Context
) {
  abstract val wrapper: ResolverWrapper
}
@eygraber eygraber mentioned this issue May 17, 2022
@evant evant added the bug Something isn't working label May 29, 2022
evant added a commit that referenced this issue May 29, 2022
One could leak a dependency by providing a scoped dependency from a
component with a shorter lifetime to one with a longer lifetime. We
detect this by ensuring the injected dependence's scope is always at
least as long as the dependency is it injected into. Unscoped
dependencies are always allowed as they don't have any particular
lifetime.

Fixes #188
evant added a commit that referenced this issue May 29, 2022
One could leak a dependency by providing a scoped dependency from a
component with a shorter lifetime to one with a longer lifetime. We
detect this by ensuring the injected dependence's scope is always at
least as long as the dependency is it injected into. Unscoped
dependencies are always allowed as they don't have any particular
lifetime.

Fixes #188
evant added a commit that referenced this issue May 29, 2022
One could leak a dependency by providing a scoped dependency from a
component with a shorter lifetime to one with a longer lifetime. We
detect this by ensuring the injected dependence's scope is always at
least as long as the dependency is it injected into. Unscoped
dependencies are always allowed as they don't have any particular
lifetime.

Fixes #188
evant added a commit that referenced this issue May 30, 2022
One could leak a dependency by providing a scoped dependency from a
component with a shorter lifetime to one with a longer lifetime. We
detect this by ensuring the injected dependence's scope is always at
least as long as the dependency is it injected into. Unscoped
dependencies are always allowed as they don't have any particular
lifetime.

Fixes #188
evant added a commit that referenced this issue Jul 29, 2022
This is a stricter version of pr #201 for #188 that is easier to
understand. This matches dagger's behavior as well (when using
subcomponents a parent component can't depend on a subcomponent's
dependency).
evant added a commit that referenced this issue Jul 29, 2022
This is a stricter version of pr #201 for #188 that is easier to
understand. This matches dagger's behavior as well (when using
subcomponents a parent component can't depend on a subcomponent's
dependency).
evant added a commit that referenced this issue Jul 29, 2022
This is a stricter version of pr #201 for #188 that is easier to
understand. This matches dagger's behavior as well (when using
subcomponents a parent component can't depend on a subcomponent's
dependency).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants