If a class has a property which contains unmanaged resources. How to make sure no memory leak when using the class
Class A
{
B {get; set;}
}
B contains unmanaged resources.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Implement
IDisposableand clean up your unmanaged resources by callingDispose(), preferably placing the call toDisposein afinallystatement so you clean up resources even in the case of an exception.C# has a
usingkeyword that you can employ to make sure that theDisposemethod is called, even if an exception is thrown.EDIT: Incorporated call to GC.SuppressFinalize and finalizer implementation per Ran’s answer