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

Single instance objects holding nested containers are not deleted #60

Open
kimushu opened this issue Nov 2, 2024 · 1 comment
Open

Comments

@kimushu
Copy link

kimushu commented Nov 2, 2024

If the object with .singleInstance() holds a nested container inside, the object will remain undeleted.
I wonder if the internal objects can no longer be deleted by a circular reference by shared pointer?

Reproduction code:

#include <Hypodermic/Hypodermic.h>

#include <iostream>

using namespace Hypodermic;

class Child {
 public:
  Child(std::shared_ptr<Container> parent) {
    std::cout << "Child ctor" << std::endl;
    ContainerBuilder builder;
    m_child = builder.buildNestedContainerFrom(*parent);
  }

  ~Child() {
    std::cout << "Child dtor" << std::endl;
  }

 private:
  std::shared_ptr<Container> m_child;
};

int main() {
  ContainerBuilder builder;
  builder.registerType<Child>().singleInstance();
  auto container = builder.build();
  container->resolve<Child>();
  std::cout << "destroying parent container" << std::endl;
  container.reset();
  std::cout << "program exiting" << std::endl;
  return 0;
}

Expected behavior

Child ctor
destroying parent container
Child dtor
program exiting

Actual behavior

Child ctor
destroying parent container
program exiting
  • (No Child dtor output!)
@kimushu
Copy link
Author

kimushu commented Nov 2, 2024

I found that RegistrationScope for parent container is not deleted even after it has been copied in NestedRegistrationScope.
This patch makes it work as expected, Is this correct?

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

No branches or pull requests

1 participant