when we use root container to create a instance,we may cause a memory leak, how about this?

24 views
Skip to first unread message

王冯俊

unread,
Jul 26, 2017, 9:30:54 PM7/26/17
to Autofac
i use a child scope to create instance,
but use out of the scope.
is that ok?

Config config=null;
using(var scope=container.BeginXX())
{
config=scope.Resolve<Config>();
}

config.GetDatabaseConnectionString();

============================

leak memory in this way? or any problems with those code above?

thanks for help,thank you all

Kendall Bennett

unread,
Jul 26, 2017, 10:23:55 PM7/26/17
to aut...@googlegroups.com
That is not going to work because a lot of stuff could well be cleaned up and disposed of. You should only ever use items you resolve from a scope without the scope it was created within.

--
You received this message because you are subscribed to the Google Groups "Autofac" group.
To unsubscribe from this group and stop receiving emails from it, send an email to autofac+u...@googlegroups.com.
To post to this group, send email to aut...@googlegroups.com.
Visit this group at https://groups.google.com/group/autofac.
For more options, visit https://groups.google.com/d/optout.

Kendall Bennett

unread,
Jul 26, 2017, 10:27:50 PM7/26/17
to aut...@googlegroups.com
I should probably say that this *will* work in practice but you will likely get stung one day by very nasty bugs related to items that are disposed of and you later try to use them, especially things like database connections etc.

Regards,

Kendall Bennett
CEO & Co-Founder
424 Otterson Drive, Chico, California 95928

王冯俊

unread,
Jul 27, 2017, 4:42:35 AM7/27/17
to Autofac
i see  thank you very much

在 2017年7月27日星期四 UTC+8上午10:27:50,Kendall Bennett写道:
Reply all
Reply to author
Forward
0 new messages