boost-ext / di

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

how to destcruct dependencies? #422

Closed qwertzui11 closed 5 years ago

qwertzui11 commented 5 years ago

Hi! awesome library!

Problem

I got a simple dependecy tree

class A{};
class B {
  B(A& a) {}
};
void create() {
  namespace di = boost::di;
  const auto injector = di::make_injector();
  auto b = injector.create<B>();
}

Everything works great. But, according to AddressSanitizer I got a memory leak at the end of the function create.

My question

How can I clean up the dependencies. How can I destruct the instance of A? Do I have to refactor the parameters in the constructor of A to std::shared_ptr to address this issue?

Specifications

qwertzui11 commented 5 years ago

I guess di cleans up when the program ends. But how may I trigger it manually. eg at the end of an unit test?

qwertzui11 commented 5 years ago

The extension di::extension::shared_config fixed the issue. issue #383 describes my problem. I didn't expect that the default behavior is a shared instance over all injectors.