-2

I recently had some some memory leak issues with a .NET native crypto service provider because it's relied on unmanaged code. The only other thing I came across on SO was this post, but it didn't get in to too much detail let alone provide a good list of culprits.

Yes, if it implements IDisposable it probably uses some unmanaged resources somewhere, but I'd like to put a list together of specific classes that are commonly used instead of simply looking for Dispose in Intellisense.

The ones that I came across that caused me trouble were:

  • AesCryptoServiceProvider
  • ICryptoTransform

What others that rely on unmanaged resources are people aware of? Are there any especially insidious ones out there that seem like they would be completely managed but are not? Thanks in advance.

Community
  • 1
  • 1
kmarks2
  • 4,755
  • 10
  • 48
  • 77
  • 1
    This is not the place for such questions. This is too open ended (and will result in way too many answers) for our Q&A format. – Oded Mar 12 '12 at 17:16
  • Apologies. I figured if I could put together a not to terribly huge list of some common but unexpected cases where there was underlying unmanaged resrouces it could be useful to people. – kmarks2 Mar 12 '12 at 17:20
  • Undoubtedly it would be useful. StackOverflow is just not the place for it. – Oded Mar 12 '12 at 17:21

2 Answers2

5

The list of these classes would be huge. Much of the framework wraps native Windows API calls, so many portions contain wrappers around native resources.

This would include most Stream implementations, anything wrapping a wait handle, most networking/socket implementations, as well as most UI related classes (especially anything wrapping an HWND).

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • Yes it would be huge. I'm interested in common ones people come across with some amount of regularity. Also I'm curious about any classes that unexpectedly use unmanaged resources. – kmarks2 Mar 12 '12 at 17:14
  • 1
    @kmarks2 A search for IDisposable would probably be a better way to find it. Not that all IDisposable implementations wrap resources, but it'd be a good starting point. I gave you some larger categories that do this, though. – Reed Copsey Mar 12 '12 at 17:15
1

Trying to put together an exhaustive list seems pointless, basically you will want all types that implement IDisposable.

But as an example of an insidious one, people often forget that System.DirectoryServices.SearchResultCollection class cannot release all of its unmanaged resources when it is garbage collected. To prevent a memory leak, you must call the Dispose method when the SearchResultCollection object is no longer needed.

Joe
  • 122,218
  • 32
  • 205
  • 338