Answer the question
In order to leave comments, you need to log in
Dispose pattern from MS?
public class ComplexResourceHolder : IDisposable {
private IntPtr buffer; // unmanaged memory buffer
private SafeHandle resource; // disposable handle to a resource
public ComplexResourceHolder(){
this.buffer = ... // allocates memory
this.resource = ... // allocates the resource
}
protected virtual void Dispose(bool disposing){
ReleaseBuffer(buffer); // release unmanaged memory
if (disposing){ // release other disposable objects
if (resource!= null) resource.Dispose();
}
}
~ ComplexResourceHolder(){
Dispose(false);
}
public void Dispose(){
Dispose(true);
GC.SuppressFinalize(this);
}
}
Answer the question
In order to leave comments, you need to log in
If we are talking about a small buffer, then there is really not much sense. But if we are talking about several tens of megabytes of memory, then the OS will start swapping before it starts a full garbage collection. Yes, and this pattern was invented primarily for blocking resources. Such as files, open ports, window handles, threads, and so on.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question