boost-ext / di

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

Extension shared_factory holds dangling reference to factory #460

Open andreasdamm opened 4 years ago

andreasdamm commented 4 years ago

Expected Behavior

auto Bind(int i)
{
    return di::make_injector(
            di::bind<int>.to(ext::shared_factory<int>([i](const auto &){
                return make_shared<int>(i);
            })));
}

int main()
{
    auto inj = Bind(123);
    auto i = inj.create<int>();

    assert(i == 123);

    return 0;
}

Actual Behavior

Assertion fails

Steps to Reproduce the Problem

  1. Create a binding using shared_factory with a lambda that has a capture and returns it as its result
  2. Create a value using that binding
  3. The created value will not be the same as the one captured as the capture storage will have been deleted already

Specifications