What Happens If You Don't Dispose?
Elton Stoneman
geekswithblogs.net/eltonstoneman
@EltonStoneman
What Happens If You Don't Dispose?
Finding IDisposable and Fixing IDisposable
IDisposable issues domain-driven issues
design
Do Not Place Anything
in This Space
(Add watermark during
editing)
Note: Warning will not appear
during Slide Show view.
What Happens If You Don't Dispose
b b
6 6 Do Not Place Anything
in This Space
(Add watermark during
editing)
Note: Warning will not appear
during Slide Show view.
Diagnosing IDisposable Issues
Design time
Run time
Do Not Place Anything
in This Space
(Add watermark during
editing)
Note: Warning will not appear
during Slide Show view.
Fixing the Word Counting App
b
Do Not Place Anything
in This Space
(Add watermark during
editing)
Note: Warning will not appear
during Slide Show view.
Demo 1: Finding Problems at Run-Time
Goal Walkthrough Walkthrough
Identify potential Check for defects Check for memory
run-time issues running the app issues profiling
from not the app
disposing
resources
Do Not Place Anything
in This Space
(Add watermark during
editing)
Note: Warning will not appear
during Slide Show view.
Demo 1: Finding Problems at Run-Time
Do Not Place Anything
in This Space
(Add watermark during
editing)
Note: Warning will not appear
during Slide Show view.
Demo 1: Finding Problems at Run-Time
b
Do Not Place Anything
in This Space
(Add watermark during
editing)
Note: Warning will not appear
during Slide Show view.
Demo 1: Finding Problems at Run-Time
70000
63000 63000
60000
50000
40000
30000
20000
10000
15000
392
0
Startup First Run Multiple Runs GC.Collect
- original app Do Not Place Anything
in This Space
(Add watermark during
editing)
Note: Warning will not appear
during Slide Show view.
Demo 1: Finding Problems at Run-Time
80000
74000 74000
71000
70000
63000 63000
60000
50000
40000
30000
20000
10000
6000 15000
0 392
Startup First Run Multiple Runs GC.Collect
- original app - domain-driven app Do Not Place Anything
in This Space
(Add watermark during
editing)
Note: Warning will not appear
during Slide Show view.
Demo 2: Finding Problems at Design-Time
Goal Walkthrough Walkthrough
Identify potential Check for issues Fix issues and
design-time using static code check
issues from not analysis & manual performance with
disposing checks profiler
Do Not Place Anything
in This Space
(Add watermark during
editing)
Note: Warning will not appear
during Slide Show view.
Demo 2: Finding Problems at Design-Time
Do Not Place Anything
in This Space
(Add watermark during
editing)
Note: Warning will not appear
during Slide Show view.
Demo 2: Finding Problems at Design-Time
Static code analysis
Not enabled by default
"Recommended" rules
Do not include CA2000
Correctly set up
Find and fix issues quickly
But not all issues
Do Not Place Anything
in This Space
(Add watermark during
editing)
Note: Warning will not appear
during Slide Show view.
Demo 2: Finding Problems at Design-Time
public void CreateFeedResults(...)
{
var sqlConnection = new SqlConnection(...);
sqlConnection.Open();
var command = sqlConnection.CreateCommand();
command.CommandType = CommandType.Text;
command.CommandText = "...",
command.ExecuteNonQuery();
}
CA2000 warning
Do Not Place Anything
in This Space
(Add watermark during
editing)
Note: Warning will not appear
during Slide Show view.
Demo 2: Finding Problems at Design-Time
70000
63000 63000
60000
50000
40000
30000
20000
10000
15000
392
0
Startup First Run Multiple Runs GC.Collect
- original app Do Not Place Anything
in This Space
(Add watermark during
editing)
Note: Warning will not appear
during Slide Show view.
Demo 2: Finding Problems at Design-Time
70000
63000 63000
60000
50000
40000
30000
20000
15000
10000 12600
11000 12600
392
0 394
Startup First Run Multiple Runs GC.Collect
- original app - best practice version Do Not Place Anything
in This Space
(Add watermark during
editing)
Note: Warning will not appear
during Slide Show view.
Best Practice #6
Enable Code Analysis with
CA2000 enabled – but don’t
rely on it
Do Not Place Anything
in This Space
(Add watermark during
editing)
Note: Warning will not appear
during Slide Show view.
Best Practice #6
✮
Do Not Place Anything
in This Space
(Add watermark during
editing)
Note: Warning will not appear
during Slide Show view.
Discovering IDisposables
Check class hierarchy
Go to definition
L
Code map
Trial-and-error
Try using{} block
Intellisense
Look for .Dispose()
Do Not Place Anything
in This Space
(Add watermark during
editing)
Note: Warning will not appear
during Slide Show view.
Demo 3: Making IDisposable Discoverable
Goal Walkthrough Walkthrough
Implement Fix issues in Implementing
IDisposable to aid domain-driven IDisposable and
discovery version of app other interfaces
Do Not Place Anything
in This Space
(Add watermark during
editing)
Note: Warning will not appear
during Slide Show view.
Demo 3: Making IDisposable Discoverable
Do Not Place Anything
in This Space
(Add watermark during
editing)
Note: Warning will not appear
during Slide Show view.
Demo 3: Making IDisposable Discoverable
80000
74000 74000
71000
70000
60000
50000
40000
30000
20000
10000
6000
0
Startup First Run Multiple Runs GC.Collect
- domain-driven app Do Not Place Anything
in This Space
(Add watermark during
editing)
Note: Warning will not appear
during Slide Show view.
Demo 3: Making IDisposable Discoverable
80000
74000 74000
71000
70000
68000 69000 69000
60000
50000
40000
30000
20000
10000
6000
6000
0
Startup First Run Multiple Runs GC.Collect
- domain-driven app - best-practice version Do Not Place Anything
in This Space
(Add watermark during
editing)
Note: Warning will not appear
during Slide Show view.
Best Practice #7
If you implement an interface
and use IDisposable fields,
extend your interface from
IDisposable
Do Not Place Anything
in This Space
(Add watermark during
editing)
Note: Warning will not appear
during Slide Show view.
Best Practice #7
✮
public class BookFeedRepository : IBookFeedRepository
{
private BookFeedContext _context;
//...
}
public interface IBookFeedRepository : IDisposable
{
//...
}
Do Not Place Anything
in This Space
(Add watermark during
editing)
Note: Warning will not appear
during Slide Show view.
Best Practice #8
If you implement IDisposable,
don’t implement it explicitly
Do Not Place Anything
in This Space
(Add watermark during
editing)
Note: Warning will not appear
during Slide Show view.
Best Practice #8
public class DifficultToDiscover : IDisposable
{
void IDisposable.Dispose()
{
//...
}
}
Do Not Place Anything
in This Space
(Add watermark during
editing)
Note: Warning will not appear
during Slide Show view.
Best Practice #8
public class DifficultToDiscover : IDisposable
{
✮
void IDisposable.Dispose()
{
//...
}
}
public class EasyToDiscover : IDisposable
{
public void Dispose()
{
//...
}
}
Do Not Place Anything
in This Space
(Add watermark during
editing)
Note: Warning will not appear
during Slide Show view.
Summary
What happens if you don't dispose?
File locks & event handlers
Starving SQL connection pool & object leaks
Finding problems from not disposing
Run-time
Test team & profiling
Design-time
Summary
Static analysis & class knowledge
Fixing the demo solution
Applying the 8 best practices
Do Not Place Anything
in This Space
(Add watermark during
editing)
Note: Warning will not appear
during Slide Show view.