boost-ext / di

C++14 Dependency Injection Library
https://boost-ext.github.io/di
1.14k stars 138 forks source link

Singleton scope creates global singleton instead of per injector #459

Closed andreasdamm closed 4 years ago

andreasdamm commented 4 years ago

Expected Behavior

auto Bind()
{
    return di::make_injector(
            di::bind<int>.in(di::singleton));
}

int main()
{
    auto inj1 = Bind();
    auto inj2 = Bind();

    auto i1 = inj1.create<shared_ptr<int>>();
    auto i2 = inj2.create<shared_ptr<int>>();

    auto different = i1.get() != i2.get();
    assert(different);

    return 0;
}

Actual Behavior

Assertion fails.

Steps to Reproduce the Problem

  1. Create two injectors of the same type containing a singleton scoped binding
  2. Have each injector create a shared_ptr of that singleton object
  3. Both shared_ptr's will point to same address

Specifications

krzysztof-jusiak commented 4 years ago

Duplicate -> https://github.com/boost-experimental/di/issues/431