question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SqlCommand.Dispose doesn't free managed object

See original GitHub issue

SqlCommand.Dispose contains this code:

// release managed objects
_cachedMetaData = null;

This doesn’t free the cached data, which is supposed to be the purpose of the Dispose(Boolean) overload when working with managed resources that don’t implement IDisposable:

Managed objects that consume large amounts of memory or consume scarce resources. Freeing these objects explicitly in the Dispose method releases them faster than if they were reclaimed non-deterministically by the garbage collector.

By just setting _cachedMetaData to null, the resources are only reclaimed by the garbage collector, by which time the disposed SqlCommand object would also likely not be referenced, meaning that disposing the SqlCommand and setting _cachedMetaData to null is wasted effort.


This issue was observed as part of a discussion about this question: Is SqlCommand.Dispose() required if associated SqlConnection will be disposed?

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:14 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
Clockwork-Musecommented, Apr 4, 2019

… that’s not really any different than adding/removing IDisposable. It’s actually worse, because now you have something that says “yeah, ignore this contract that I claim to need”, and if a type suddenly did have to dispose a resource (say, MemoryStream starts allocating a native array for really large array sizes or something), your consumers now need to dispose, but you previously informed them they didn’t …

A publicly visible attribute like that is likely to be permanent for the same general reason: breaking behavior change if removed.

1reaction
breyedcommented, Apr 4, 2019

That was my point about “could have been corrected in the transition to .NET Core”. Fortunately, there might still a good solution: Create a custom attribute (perhaps DisposeUnusedAttribute) that would inform developers, the IDE, and static code analysis that it’s OK to skip the extra work of adding calls to Dispose. Other classes such a MemoryStream would benefit as well.

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Is SqlCommand.Dispose() required if associated ...
Yes, the safest policy is to always dispose disposable objects - notably if you created it! It's not a good idea for example...
Read more >
Should I Dispose() an SqlCommand object? - C# / C Sharp
So, given all that, yes, you should always call Dispose. It doesn't really matter what it disposes, the implication through the ...
Read more >
IDisposable Interface (System)
Dispose implementation is called by the consumer of a type when the resources owned by an instance are no longer needed, you should...
Read more >
SqlCommand.Dispose() not disposing the SqlParameters in it ...
Therefore, you do not dispose them directly. They are an entirely managed resource, and that means they ar ecleaned up by the garbage...
Read more >
Should I Dispose() an SqlCommand object?
So, given all that, yes, you should always call Dispose. It doesn't really matter what it disposes, the implication through the implementation ( ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found