dispose

Safe to call managed resource from Finalizer? (if i check null)

懵懂的女人 提交于 2019-12-10 18:38:42
问题 Is it not safe to call: component.Dispose(); (if i check null) from the Finalizer if i alter the code to this: ~MyResource() { Dispose(); } public void Dispose() { // Check to see if Dispose has already been called. if(!this.disposed) { if(component != null) component.Dispose(); // !!! // CloseHandle(handle); handle = IntPtr.Zero; disposed = true; } GC.SuppressFinalize(this); } I know this works - but is it safe? (from the example below?) Code example: (before alter code) http://msdn

Order of disposal for base classes

 ̄綄美尐妖づ 提交于 2019-12-10 16:25:48
问题 When a derived class overrides Dispose should the base class Dipose be called first before the derived class disposes of any local resources? The reason I ask is because someone on our team is saying yes for every scenario, I'm trying to work out if this is good idea or just 'cargo cult' programming, I don't have a strong view either way. Before: public override void Dispose() { base.Dispose(); if (!_isDisposed) { _isDisposed = true; if (_foo != null) { _foo.Dispose(); } } } After: public

WPF - AvalonDock - Closing Document

可紊 提交于 2019-12-10 15:31:44
问题 I use AvalonDock with MVVM in a WPF project. When I hit the "X" (Close button of the tab) my document closes but stays in memory. It seems that it is only hidden. It is not removed from my Model.Documents collection. If I add DockingManager_DocumentClosing and try to remove my document from the collection, I receive an Exception in the following method of Xceed.Wpf.AvalonDock.Layout.LayoutContent because parentAsContainer is null. /// <summary> /// Close the content /// </summary> ///

Win32.DestroyIcon vs. Icon.Dispose

不问归期 提交于 2019-12-10 14:57:03
问题 I have this line of code: System.Drawing.Icon icon = System.Drawing.Icon.FromHandle(shinfo.hIcon); A few lines later, after icon is used I have the line: Win32.DestroyIcon(shinfo.hIcon); However when running a static analysis on my code it says there is a potential for Resource_Leak from icon. I am wondering will it make any difference if I call the dispose method: icon.Dispose(); rather than the Win32.DestroyIcon() that is being used right now. Is there any difference between them? I am just

How is IDisposable implemented on FileStream in .Net 1.1

心已入冬 提交于 2019-12-10 14:43:42
问题 This might seem like a noddy question, but I was looking at this because I heard someone claiming that you must call Close() on a FileStream, even if it is in a using block (and they have code where Close() is being called right at the end of the block). I know that Close() is meant to call Dispose(), but I thought I'd look deeper since this is .Net 1.1 code, and the bulk of my experience has been with 2.0 on. One thing that struck me was that the MSDN documentation for FileStream has Dispose

How to unsubscribe an anonymous function in Dispose method of a class?

六月ゝ 毕业季﹏ 提交于 2019-12-10 14:22:11
问题 I have a Class A...in it's constructor...I am assigning an anonymous function to Object_B's eventHandler. How do I remove (unsubscribe) that from Dispose method of class A ? Any help would be appreciated ! Thanks Public Class A { public A() { B_Object.DataLoaded += (sender, e) => { Line 1 Line 2 Line 3 Line 4 }; } Public override void Dispose() { // How do I unsubscribe the above subscribed anonymous function ? } } 回答1: You can't, basically. Either move it into a method, or use a member

Call to MemoryStream.GetBuffer() succeeds even after MemoryStream.Close(); Why?

跟風遠走 提交于 2019-12-10 13:18:13
问题 I have found the following construct in some open-source code: var mstream = new MemoryStream(); // ... write some data to mstream mstream.Close(); byte[] b = mstream.GetBuffer(); I thought this code would have "unexpected" behavior and maybe throw an exception, since the call to Close should effectively be a call to Dispose according to the MSDN documentation. However, as far as I have been able to tell from experimenting, the call to GetBuffer() always succeeds and returns a valid result,

Are all disposable objects instantiated within a using block disposed?

喜欢而已 提交于 2019-12-10 12:40:56
问题 This is a question I have asked myself many times in the past as I nested using statements 5 deep. Reading the docs and finding no mention either way regarding other disposables instantiated within the block I decided it was a good Q for SO archives. Consider this: using (var conn = new SqlConnection()) { var conn2 = new SqlConnection(); } // is conn2 disposed? 回答1: No they are not. Only the set of variables explicitly listed in the using clause will be automatically disposed. 回答2: Obviously

Webbrowser is not disposing itself

社会主义新天地 提交于 2019-12-10 09:53:49
问题 I am creating a WPF application. I created a usercontrol and put a back button and webbrowser. The webbrowser is for showing some Flash objects and works without a problem. However, when I click back, the program returns to previous user control but I can still hear some sound coming from webbrowser. Here is my method: void btnClose_Click(object sender, RoutedEventArgs e) { Content contentPage = new Content(); webBrowser1.Dispose(); this.Content = contentPage; } How can I dispose webbrowser?

.NET 3.5 Dispose Registry Key

非 Y 不嫁゛ 提交于 2019-12-10 09:26:31
问题 I have the following code: RegistryKey installKey = Registry.LocalMachine.OpenSubKey(installKey); I am running a static analysis tool on my code and it is giving me a defect saying that I am returning from the medthod without disposing installKey . I know you can call Dispose() on RegistryKey in .NET 4.0 or later but my code runs on .NET 3.5. Does anybody know the best way to Dispose this RegistryKey and keep my static analysis tool happy? 回答1: You should wrap your code within a using block,