Dispose methods Warning, Certainty 75, for DisposableFieldsShouldBeDisposed

Why I get this Warning, Certainty 75, for DisposableFieldsShouldBeDisposed?

publicstructVirtualMemoryPtr :IDisposable
{
privatereadonlyIntPtr allocatedPointer;
privatereadonlyIntPtr processHandle;
privatereadonlyUIntPtr memorySize;
privatebool disposed;// wird von der Runtime auf false initialisiert

public VirtualMemoryPtr(int memorySize)
{
processHandle =
UnsafeNativeMethods.GetCurrentProcess();
this.memorySize = (UIntPtr)(uint)memorySize;
allocatedPointer =
MemoryManager.AllocExecutionBlock((uint)memorySize, processHandle);
disposed =
false;
}
...
...
publicvoid Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

privatevoid Dispose(bool disposing)
{
if(!disposed)
{
if(disposing)
{
// Release managed resources.
}
// Free the unmanaged resource ...
MemoryManager.VirtualFreeEx(ProcessHandle, AllocatedPointer, memorySize);
disposed =
true;
}
}
}

publicclassNativeAssembly :IDisposable
{
privateVirtualMemoryPtr nativeCodePtr;

public NativeAssembly(byte[] code)
{
if(code ==null){thrownewArgumentNullException("code","NullArgument");}
nativeCodePtr =newVirtualMemoryPtr(code.Length);
Marshal.Copy(code, 0, nativeCodePtr, code.Length);
}

...

privatebool disposed;
~NativeAssembly()
{
Dispose(false);
}

publicvoid Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

protectedvirtualvoid Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
{
// Release managed resources.
}
nativeCodePtr.Dispose();
disposed =
true;
}
}
}

Is this kind of implementation wrong? What I'm doing wrong? Any idea?

thx
hahnl

[3842 byte] By [hahnl] at [2007-12-24]
# 1

You should dispose all IDisposable fields in your Dispose method, nativeCodePtr is a disposable managed resource, so you should call nativeCodePtr.Dispose when disposing = true. NativeAssembly.Dispose(bool disposing) should be:

protected virtual void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
{
// release managed resources
if (nativeCodePtr != null)
{
nativeCodePtr.Dispose();
nativeCodePtr = null;
}
}
// release unmanaged resources
}
disposed = true;

}

JesúsLópez at 2007-10-8 > top of Msdn Tech,Visual Studio Team System,Visual Studio Code Analysis and Code Metrics...
# 2

Thanks Jesús López for your reply, but the warning still exists!!!

Please, pay attention that nativeCodePtr = null; will not work, because nativeCodePtr is a struct.

I've implemented a lot of IDisposables, but all working fine without any warning by FxCop.

Maybe the warning is a bug inside FxCop 1.35 (interlink class <=> struct)?

Any other Idea?

Thanks in advance

hahnl


hahnl at 2007-10-8 > top of Msdn Tech,Visual Studio Team System,Visual Studio Code Analysis and Code Metrics...
# 3

hahnl,

I can't reproduce this with the code above. What's the actually warning state and what line is it pointing to?

On a side note, you would be better making VirtualMemoryPtr a class and putting the finalizer on that or better still, deriving from the new SafeHandle class. For more information, see: http://blogs.msdn.com/shawnfa/archive/2004/08/12/213808.aspx.

Regards

David

DavidM.Kean-MSFT at 2007-10-8 > top of Msdn Tech,Visual Studio Team System,Visual Studio Code Analysis and Code Metrics...
# 4

David,

thank you for the SafeHandle suggestion. I'm deriving now from the new SafeHandle class.

regrads

hahnl

hahnl at 2007-10-8 > top of Msdn Tech,Visual Studio Team System,Visual Studio Code Analysis and Code Metrics...

Visual Studio Team System

Site Classified