473,406 Members | 2,281 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,406 software developers and data experts.

I need a tool to find IDisposables missing Dispose/using

Are there any tools available to track down IDisposable objects that
don't have a call to Dispose? I'm thinking this can be done with
static code analysis. If the object is created inside a method, it
should be created with a using statement. If it's a member of a class,
that class should have a Dispose function or call Dispose on the
object somewhere in there.

Sep 7 '07 #1
2 2626
On Sep 7, 11:07 am, not_a_commie <notacom...@gmail.comwrote:
Are there any tools available to track down IDisposable objects that
don't have a call to Dispose? I'm thinking this can be done with
static code analysis. If the object is created inside a method, it
should be created with a using statement. If it's a member of a class,
that class should have a Dispose function or call Dispose on the
object somewhere in there.
FxCop can detect quite a few of them.

Sep 7 '07 #2

One mechanism I like is to add code in the finalizer to log an error
whenever a disposable object is finalized ('cause finalizer is
suppressed when dispose is called).

HTH,

Sam

------------------------------------------------------------
We're hiring! B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.


using System;
using System.Diagnostics;

namespace Common
{
public abstract class DisposableBase : IDisposable
{
private bool _isDisposed;
#if DEBUG
/// <summary>
/// Contains the stack when the object was allocated.
/// </summary>
private StackTrace _allocStack;
#endif

public event EventHandler Disposed;

protected DisposableBase()
{
#if DEBUG
_allocStack = new StackTrace();
#endif
}

#if DEBUG
~DisposableBase()
{
DisposeImpl(false);
}
#endif

public void Dispose()
{
DisposeImpl(true);
GC.SuppressFinalize(this);
}

private void DisposeImpl(bool disposing)
{
bool wasDisposed = _isDisposed;
_isDisposed = true;

try
{
#if DEBUG
if (!disposing)
{
Log.Debug(this, "Disposable object not disposed.\r\n\r\n" +
_allocStack);
}
#endif
Dispose(disposing);
if (disposing && !wasDisposed && Disposed != null)
{
Disposed(this, EventArgs.Empty);
}
}
catch (Exception ex)
{
Log.Fatal(this, "Error thrown during dispose.", ex);
}
}
protected abstract void Dispose(bool disposing);

}
}


On Fri, 07 Sep 2007 08:07:34 -0700, not_a_commie
<no********@gmail.comwrote:
>Are there any tools available to track down IDisposable objects that
don't have a call to Dispose? I'm thinking this can be done with
static code analysis. If the object is created inside a method, it
should be created with a using statement. If it's a member of a class,
that class should have a Dispose function or call Dispose on the
object somewhere in there.
Sep 7 '07 #3

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

Similar topics

55
by: Alex | last post by:
Hello people, The following is not a troll but a serious request. I found myself in a position where I have to present a Pro/Con list to management and architects in our company with regard to...
5
by: MFC | last post by:
Ok, after three C# books, (C# How to Program, Programming in the Key of C#, and C# Weekend Crash Course) and three weeks, I believe I have tried everything to make a certain form function...
6
by: news | last post by:
I need some concise, easy to grok examples of what .Net (preferably C#.Net) has over just using old ASP or PHP with JavaScript. See, I'm a PHP guy, and while I started server-side scripting with...
0
by: Henrik_the_boss | last post by:
Hello all. I have a couple of aspx pages. When something fails in them, I would like them to be able to log to either a database, a logfile, or the application log. All code is in C# I run...
25
by: Neo Geshel | last post by:
This works: <form> <asp:TextBox id="name" /> <%= name.ClientID %> </form> But this DOES NOT work: <form>
3
by: Chris Botha | last post by:
I have a Windows app (it will be promoted to a Service once this problem is sorted out) that cyclically, every minute, connects to five SQL Servers over the WAN, extract data and insert the data in...
3
by: Zach | last post by:
I'm sure this comes up often, but I have a situation where I have at least 4 objects that all implement IDisposable. It gets very tedious having to write finally blocks that Dispose all of them in...
20
by: mike | last post by:
I help manage a large web site, one that has over 600 html pages... It's a reference site for ham radio folks and as an example, one page indexes over 1.8 gb of on-line PDF documents. The site...
13
by: Carl Johansson | last post by:
Being quite new to C#, I may have misunderstood this. If so please bear with me! As far as I can understand, any instances of a class that implements the IDisposable interface must call the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.