473,288 Members | 1,705 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,288 software developers and data experts.

fxcop - Dispose pattern analysis

When I implement Dispose pattern in object implementing IDisposable, current
fxcop recommends:

Ensure that Wrapper.Dispose():Void is declared as public and sealed.

However, if I do as it asks, compiler complains that sealed can't be used
because method is not overrideable.

I believe sealed isn't applicable here at all. And this rule shouldn't be
public virtual in this case.

Am I right or do I miss something important?

Jun 10 '07 #1
3 4359
Typo:

This rule should be public virtual. - not shouldn't be.
"AlexS" <sa***********@SPAMrogers.comPLEASEwrote in message
news:OL*************@TK2MSFTNGP06.phx.gbl...
When I implement Dispose pattern in object implementing IDisposable,
current fxcop recommends:

Ensure that Wrapper.Dispose():Void is declared as public and sealed.

However, if I do as it asks, compiler complains that sealed can't be used
because method is not overrideable.

I believe sealed isn't applicable here at all. And this rule shouldn't be
public virtual in this case.

Am I right or do I miss something important?

Jun 10 '07 #2
Hello,

the correct implementation is as follows:

public class Test: IDisposable
{
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
if (disposing)
{
// Dispose any nested instances
}
// free unmanaged resources, do not access nested instances any
more, as they could be freed by the GC already
}
}

If your class has a destructor, simply call Dispose(false) from it.

Best regards,
Henning Krause

"AlexS" <sa***********@SPAMrogers.comPLEASEwrote in message
news:OL*************@TK2MSFTNGP06.phx.gbl...
When I implement Dispose pattern in object implementing IDisposable,
current fxcop recommends:

Ensure that Wrapper.Dispose():Void is declared as public and sealed.

However, if I do as it asks, compiler complains that sealed can't be used
because method is not overrideable.

I believe sealed isn't applicable here at all. And this rule shouldn't be
public virtual in this case.

Am I right or do I miss something important?
Jun 10 '07 #3
Thanks, Henning, fxcop likes that.

Why MS recommends "public Dispose" implementation and not "public virtual
Dispose"?

When subclassing I can override Dispose and extend its functionality using
"new Dispose" declaration. So, why to insist on omitting virtual?

Question is more academic, I believe, however, fxcop is mixing me up
sometimes with it's Dispose pattern analysis.

Another question - related - is: I am subclassing class implementing
IDisposable. Forms designer generates Dispose(bool) in this case overriding
base implementation. I don't want to touch designer code as it could be
regenerated and maybe my changes will be cancelled. So, I implement "public
new Dispose" in subclass. And fxcop recommends then this:

Ensure that TableTab.Dispose():Void is declared as public and sealed.

Which is impossible as Dispose is not an override - it is "public new". And
base implementation doesn't specify "virtual".

So, what fxcop means in this case?

Alex

"Henning Krause [MVP - Exchange]" <ne***************@this.infinitec.de>
wrote in message news:e8**************@TK2MSFTNGP03.phx.gbl...
Hello,

the correct implementation is as follows:

public class Test: IDisposable
{
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
if (disposing)
{
// Dispose any nested instances
}
// free unmanaged resources, do not access nested instances any
more, as they could be freed by the GC already
}
}

If your class has a destructor, simply call Dispose(false) from it.

Best regards,
Henning Krause

"AlexS" <sa***********@SPAMrogers.comPLEASEwrote in message
news:OL*************@TK2MSFTNGP06.phx.gbl...
>When I implement Dispose pattern in object implementing IDisposable,
current fxcop recommends:

Ensure that Wrapper.Dispose():Void is declared as public and sealed.

However, if I do as it asks, compiler complains that sealed can't be used
because method is not overrideable.

I believe sealed isn't applicable here at all. And this rule shouldn't be
public virtual in this case.

Am I right or do I miss something important?


Jun 10 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Ken Durden | last post by:
I am in search of a comprehensive methodology of using these two object cleanup approaches to get rid of a number of bugs, unpleasantries, and cleanup-ordering issues we currently have in our...
0
by: Chua Wen Ching | last post by:
Hi there, I am super confuse on this recommendation by FxCop. 1) http://www.gotdotnet.com/team/fxcop/docs/rules/UsageRules/OperatorEqualsOverridesRequireEqualsOverridel.html First of all,...
1
by: Sharon | last post by:
In the report of the FxCop analysis: CriticalError, Certainty 95, for "TypesThatOwnDisposableFieldsShouldBeDisposable" { Target : "Receiver" (IntrospectionTargetType) Resolution : "Implement...
3
by: Rasmus | last post by:
I VS 2005 beta 2 i have a solution with - a number of classes - a website - a httphandler - a http module I want to run fxcop on my class files - but cant find out how to enable it. I've...
2
by: orekinbck | last post by:
Hi There I am inheriting from DateTimePicker class to create a DateTimePicker control with a configurable back colour. I got the original code from http://dotnet.mvps.org/ then converted it to...
4
by: | last post by:
Environment: Microsoft Visual Studio 2005 Professional Edition Microsoft Visual Source Safe 2005 FxCop 1.32 Project: ASP.NET 2.0 C# Project 5 Developers We are trying to implement FxCop...
0
by: Trey Nash | last post by:
Hi all, I am currently building my project with all code analysis turned on. From what I can tell, it looks to be employing FxCop and PREfast. The problem is, I am getting the following...
5
by: Peter Ritchie [C# MVP] | last post by:
I've purposely been ignoring a CA2122 warning in some C++ interop code I've been working on for quite some time. I've just recently had the cycles to investigate the warning. The warning message...
6
by: John Wright | last post by:
I ran FxCop against a program and was pleased with the security review except I get the following error: Do not indirectly expose methods How would I fix this code so this error goes away. I...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.