What does “Access to disposed closure” mean?

独自空忆成欢 提交于 2019-12-01 16:38:30

问题


I have the following code:

public void DequeueRecipe(AuthIdentity identity, params Guid[] recipeIds)
{
   using (var session = GetSession())
   {
      var recipes = (from r in recipeIds select new Models.Recipes {RecipeId = r}).ToArray();

      var dbRecipes = session.QueryOver<Models.QueuedRecipes>()
         .Where(Expression.Eq("UserId", identity.UserId))
         .Where(Expression.InG("Recipe", recipes))
         .List<Models.QueuedRecipes>();

      using (ITransaction transaction = session.BeginTransaction())
      {
         dbRecipes.ForEach(r => session.Delete(r)); // <-- Warning
         transaction.Commit();
      }
   }
}

reSharper is giving me the warning:

Access to disposed closure

On the line:

dbRecipes.ForEach(r => session.Delete(r));

(The session variable is underlined as the culprit).

While it's true the ForEach method takes a lamba expression which creates a closure around the variable session, I don't see a way where it would possibly be disposed when this code is executed. Perhaps reSharper thinks ForEach might execute some sort of task in parallel, or save that Action<> for a later time, thus technically it might be disposed while the anonymous function is still reachable in memory.

Am I safe the ignore this warning? Is there a way I can reformat my code to prevent this warning from appearing? Is there indeed a danger presented by this code?

I'm used to reSharper being smarter than me, so I'd like to understand exactly what's going on.


回答1:


Because session is wrapped in a using statement, and the LINQ execution could be defered until it is enumerated.

Resharper is warning that this could result in an exception, because by the time dbRecipes is enumerated, the session could have have been disposed.

I'll be honest: I'm not sure the above code could ever fail in the way warned about.



来源:https://stackoverflow.com/questions/21057117/what-does-access-to-disposed-closure-mean

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!