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

Identical instance returned from separate koinApplication contexts #2085

Open
C0nstan opened this issue Dec 20, 2024 · 1 comment
Open

Identical instance returned from separate koinApplication contexts #2085

C0nstan opened this issue Dec 20, 2024 · 1 comment
Labels

Comments

@C0nstan
Copy link

C0nstan commented Dec 20, 2024

Describe the bug
When creating multiple koinApplication { ... } instances with the same module, each application should produce its own set of instances. However, I’m observing that two distinct Koin contexts return what appears to be the exact same instance, even though they should be isolated. This occurs even with a simple configuration (e.g., a single module providing a single).

To Reproduce
Steps to reproduce the behavior:

  1. Run the following minimal code snippet (see below) in a clean environment.
  2. Observe that calling koin1.get() and koin2.get() returns the same object reference.

Expected behavior
Each new koinApplication should create fresh instances independently. I would expect foo1 === foo2 to be false, since each application context is isolated.

Koin module and version:
koin-core:3.5.6

Snippet or Sample project to help reproduce

import org.koin.dsl.module
import org.koin.core.context.koinApplication
import kotlin.test.assertNotEquals

class Foo

fun main() {
    val moduleDefinition = module {
        single<Foo> { Foo() }
    }

    val koin1 = koinApplication {
        modules(moduleDefinition)
    }.koin

    val koin2 = koinApplication {
        modules(moduleDefinition)
    }.koin

    val foo1 = koin1.get<Foo>()
    val foo2 = koin2.get<Foo>()

    println("identityHashCode(foo1): ${System.identityHashCode(foo1)}")
    println("identityHashCode(foo2): ${System.identityHashCode(foo2)}")
    println("Are they the same instance? ${foo1 === foo2}")

    // Expect false, but it prints true
    assertNotEquals(foo1, foo2)
}
@arnaudgiuliani arnaudgiuliani added the question Usage question label Jan 10, 2025
@arnaudgiuliani
Copy link
Member

Give it a try on Koin 4.0.x. It should have been addressed 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants