【发布时间】:2015-07-18 20:55:33
【问题描述】:
我使用 HttpContext 来检索即将到来的 HTTP 请求的当前用户名,但是在运行覆盖率分析时,它报告了资源泄漏。
public class UsersController:ApiController
{
private string userName;
public UsersController()
{
if (HttpContext.Current != null)
{
userName = HttpContext.Current.User.Identity.Name;
}
}
//I defined customized identity
public class MyIdentity : IIdentity
{
private string name;
public string AuthenticationType
{
get { return "Custom"; }
}
public bool IsAuthenticated
{
get { return true; }
}
public string Name { get; set; }
}
在 Coverity 报告中,它说 2. alloc_fn:分配方法Identity.get返回一个新资源。 (虚拟调用解析为 System.Security.Claims.ClaimsPrincipal.Identity.get。) 3. noescape:资源System.Web.HttpContext.Current.User.Identity没有关闭或者保存在Name.get中。 (虚拟调用解析为 Org.Abc.HttpModules.MyIdentity.Name.get。)
CID 51307:资源泄漏 (RESOURCE_LEAK) 4.leaked_resource:System.Web.HttpContext.Current.User.Identity创建的资源保存或关闭失败会泄露。
【问题讨论】:
-
您找到解决问题的方法了吗?
-
@AbdulAhadMonty 我直接联系了覆盖支持,他们认为这是误报,所以我的实施没有问题:)
标签: asp.net c#-4.0 asp.net-web-api memory-leaks coverity