-
Notifications
You must be signed in to change notification settings - Fork 3
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
Does transient instances support disposing? #14
Comments
Hi @jinyus Good point! I will fix the code example. Thanks for pointing it out A PR to fix the doco would be welcome |
I was looking through the code while creating the PR and realized that there is nothing wrong with the docs because the services are treated as singletons when scoped; so the dispose method will be called. There is a conundrum though, because factory singletons cannot have a dispose method: ioc_container/lib/ioc_container.dart Lines 24 to 27 in 313e32d
So there is a memory leak where singletons created in a scope will never be disposed of. Let me know if you're ok with allowing singletons to have a dispose method and I will submit the PR. |
Sorry, yes, stored services do need dispose. It's not a memory leak. That's by design. If you create a scope with disposable services, you need to dispose of them when you're finished. |
re: memory leak eg: final builder = IocContainerBuilder()
..addSingletonService(AuthenticationService())
final container = builder.toContainer();
final scope = container.scoped();
final authService = scope<AuthenticationService>();
print(authService.login('user', 'password'));
await scope.dispose(); // AuthenticationService is not disposed here |
You can
You supply the dispose methods when you create the original container
Get Outlook for Android<https://aka.ms/AAb9ysg>
…________________________________
From: jinyus ***@***.***>
Sent: Sunday, January 7, 2024 12:11:04 PM
To: MelbourneDeveloper/ioc_container ***@***.***>
Cc: Christian Findlay ***@***.***>; Comment ***@***.***>
Subject: Re: [MelbourneDeveloper/ioc_container] Does transient instances support disposing? (Issue #14)
re: memory leak
I mean that factory singletons are never disposed in a scope because you cannot supply a dispose method when registering them. My PR fixes this.
—
Reply to this email directly, view it on GitHub<#14 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AD7MRS4SO3GM3PJFYKWRKRTYNHY2RAVCNFSM6AAAAABBPQRHDSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQNZZHEYDENZVGQ>.
You are receiving this because you commented.Message ID: ***@***.***>
|
Here is the code for addSingletonService: ioc_container/lib/ioc_container.dart Lines 153 to 158 in 313e32d
It doesn't allow you to supply a dispose method. So my code above would result in a memleak. Unless I'm mistaken. |
Yes, but currently there is no way to supply a dispose method for eg: ///Add a singleton service to the container.
void addSingletonService<T>(T service) => addServiceDefinition(
ServiceDefinition<T>(
(container) => service,
isSingleton: true,
),
);
///1️⃣ Add a singleton factory to the container. The container
///will only call this once throughout the lifespan of the container
void addSingleton<T>(
T Function(
IocContainer container,
) factory,
) =>
addServiceDefinition<T>(
ServiceDefinition<T>(
(container) => factory(container),
isSingleton: true,
),
);
|
@jinyus ah yes! I see your point. I've have a look at this and see if there are any issues Thanks 🙏🏼 |
Correct me if I'm wrong... I don't think this is supported but this snippet in your docs implies that it is.
Based on the dispose extension, only scoped singletons are disposed. Therefore the
dispose
function in the above example will never be called.ioc_container/lib/ioc_container.dart
Lines 221 to 234 in 313e32d
The text was updated successfully, but these errors were encountered: