컴파일러 경고(수준 2) CS0728
업데이트: 2007년 11월
오류 메시지
using 또는 lock 문의 인수인 지역 변수 'variable'에 대한 할당이 잘못되었을 수 있습니다. 지역 변수의 원래 값에 대해 Dispose 호출 또는 잠금 해제가 수행됩니다.
Possibly incorrect assignment to local 'variable' which is the argument to a using or lock statement. The Dispose call or unlocking will happen on the original value of the local.
몇 가지 경우에 using 또는 lock 블록에서 리소스가 잠시 동안 누설될 수 있습니다. 다음 예제를 참조하십시오.
thisType f = null;
using (f)
{
f = new thisType();
...
}
이 경우 null과 같은 thisType 변수의 원래 값은 using 블록의 실행이 완료되면 삭제되지만 블록 내에서 만들어진 thisType 개체는 이 시점에서는 삭제되지 않으며 나중에 가비지 수집됩니다.
이 오류를 해결하려면 다음과 같은 형식을 사용하십시오.
using (thisType f = new thisType())
{
...
}
이 경우 새로 할당된 thisType 개체가 삭제됩니다.
예제
다음 코드에서는 CS0728 경고가 발생하는 경우를 보여 줍니다.
// CS0728.cs
using System;
public class ValidBase : IDisposable
{
public void Dispose() { }
}
public class Logger
{
public static void dummy()
{
ValidBase vb = null;
using (vb)
{
vb = null; // CS0728
}
vb = null;
}
public static void Main() { }
}