这是否意味着如果不实现IDisposable,则不需要手动清除对象?

我面临的问题是以下错误:

System.ComponentModel.Win32Exception(0x80004005):操作成功完成

堆栈跟踪:

at MS.Win32.UnsafeNativeMethods.RegisterClassEx(WNDCLASSEX_D wc_d) at MS.Win32.HwndWrapper..ctor(Int32 classStyle, Int32 style, Int32 exStyle, Int32 x, Int32 y, Int32 width, Int32 height, String name, IntPtr parent, HwndWrapperHook[] hooks) at MS.Win32.MessageOnlyHwndWrapper..ctor() at System.Windows.Threading.Dispatcher..ctor() at System.Windows.Threading.Dispatcher.get_CurrentDispatcher() at System.Windows.Threading.DispatcherObject..ctor() at System.Windows.Media.Imaging.BitmapDecoder..ctor(SafeMILHandle decoderHandle, BitmapDecoder decoder, Uri baseUri, Uri uri, Stream stream, BitmapCreateOptions createOptions, BitmapCacheOption cacheOption, Boolean insertInDecoderCache, Boolean isOriginalWritable, Stream uriStream, UnmanagedMemoryStream unmanagedMemoryStream, SafeFileHandle safeFilehandle) at System.Windows.Media.Imaging.JpegBitmapDecoder..ctor(SafeMILHandle decoderHandle, BitmapDecoder decoder, Uri baseUri, Uri uri, Stream stream, BitmapCreateOptions createOptions, BitmapCacheOption cacheOption, Boolean insertInDecoderCache, Boolean originalWritable, Stream uriStream, UnmanagedMemoryStream unmanagedMemoryStream, SafeFileHandle safeFilehandle) at System.Windows.Media.Imaging.BitmapDecoder.CreateFromUriOrStream(Uri baseUri, Uri uri, Stream stream, BitmapCreateOptions createOptions, BitmapCacheOption cacheOption, RequestCachePolicy uriCachePolicy, Boolean insertInDecoderCache) at System.Windows.Media.Imaging.BitmapDecoder.Create(Stream bitmapStream, BitmapCreateOptions createOptions, BitmapCacheOption cacheOption) at App.Business.WPFImageService.ReadBitmapFrame(Stream stream) at App.Business.WPFImageService.UploadFileToAmazon(String amazonPath, Boolean isBlankImage) 

通过一些研究,我发现当这个过程的Handle数量超过10k时会发生这个问题。 如果我忘了调用.Dispose()方法,就会发生这种情况。

在我的应用程序中,我清楚(100%肯定)处理实现IDisposable每个对象。

虽然我仍然面临这个问题,但现在我不确定在使用后必须清除的每个对象都实现了IDisposable

我在我的应用程序中使用以下类型的对象。

BitmapFrameBitmapFrameDrawingContextRenderTargetBitmapSolidColorBrushBitmapDecoderStreamMemoryStreamFileStreamPngBitmapEncoder

StreamMemoryStreamFileStream实现了IDisposable ,我明显地在它们上面调用Dispose() 。 但是, DrawingContext没有实现IDisposable但是有一个.Close()方法,也被调用。

其他对象没有.Dispose()Flush()或任何可与主题相关的内容。

我被卡住了,为什么我的句柄超过10k(我已经通过添加Handle列从任务托管中检查过)。 在使用WPF / WIC添加一些图像处理function之前,此应用程序没有此问题。

有任何想法吗?

尽量确保你也尝试使用语句来确保你收集垃圾

  using (DrawingContext dc = dGroup.Open()) 

http://msdn.microsoft.com/en-us/library/system.windows.media.drawingcontext(v=vs.110).aspx