idisposable

How to fix a CA2000 IDisposable C# compiler warning, when using a global cache

此生再无相见时 提交于 2019-12-29 08:40:06
问题 CA2000 is a warning regarding the IDisposable interface: CA2000 : Microsoft.Reliability : In method 'ImportProcessor.GetContext(string)', call System.IDisposable.Dispose on object 'c' before all references to it are out of scope. My method is used to store a cache of context like so: public class RegionContext : IDisposable { /* Implement Dispose() here */ } private Dictionary<string, RegionContext> contextCache = new ..... (); public RegionContext GetContext(string regionCode) {

C# WebPages : manage Exit events or quit with no user action server side

最后都变了- 提交于 2019-12-24 19:07:13
问题 What is the better solution to manage Exit or Quit events when user exit to other pages with no action?. This event need to be raised only one time and be usefull to delete all temporary files or datas not stored after the abandon of this page. Is a perfect to use as cleaning method server side. Thanks. 回答1: The web is stateless so there shouldn't really be anything to clean up. However, sometimes you may want to kill the session when the user tries to close browser. I guess you could catch

Proper Object Disposal In C++/CLI

自古美人都是妖i 提交于 2019-12-24 10:00:18
问题 Consider the following class: public ref class Workspace { protected: Form^ WorkspaceUI; SplitContainer^ WorkspaceSplitter; AvalonEditTextEditor^ TextEditor; ScriptOffsetViewer^ OffsetViewer; SimpleTextViewer^ PreprocessedTextViewer; ListView^ MessageList; ListView^ FindList; ListView^ BookmarkList; ListView^ VariableIndexList; TextBox^ VariableIndexEditBox; Label^ SpoilerText; ToolStrip^ WorkspaceMainToolBar; ToolStripButton^ ToolBarNewScript; ToolStripButton^ ToolBarOpenScript;

Do I need to call .Dispose() on the StandardAnalyzer, or does .Dispose() on the IndexWriter dispose its descendants?

耗尽温柔 提交于 2019-12-24 05:49:52
问题 Initializing an IndexWriter in Lucene.Net looks like this: var analyzer = new Lucene.Net.Analysis.Standard.StandardAnalyzer(version); var indexWriterConfig = new Lucene.Net.Index.IndexWriterConfig(version, analyzer); Index = new Lucene.Net.Index.IndexWriter(luceneDir, indexWriterConfig); That is, you can't instantiate an IndexWriter without an Analyzer. So, I would expect that calling .Dispose() on the IndexWriter would dispose its children, including the Analyzer. However browsing the code I

Best practice to avoid multiple disposals with the `using` keyword in C#

拈花ヽ惹草 提交于 2019-12-24 03:05:11
问题 When a variable is IDisposable, we have the using keyword to manage the disposal. But what if we return the value in a method, should we have using twice? StringContent stringToStringContent(string str) { using (StringContent content = new StringContent(str)) { return content; } } void logStringContent() { using (StringContent content = stringToStringContent("test")) { Debug.WriteLine(content.ToString()); return; } } In this example above, I only have 1 new but I have 2 using for the same

Writing the correct IDisposable implementation for classes with COM objects

回眸只為那壹抹淺笑 提交于 2019-12-23 23:33:38
问题 I have a class that uses a .NET wrapper for a COM object and well it is giving me that infamous RCW error, so in my investigation I found that if I take out the Dispose() method from the finalizer of this class it will fix the RCW error so something is wrong for example the object is getting disposed but the registered events are still hanging around ... But just removing the Dispose() can't be the answer because then who is going to release the memory ? ( I ran a Memory profiler and

Disposing an IDisposable object stored in a public static field

邮差的信 提交于 2019-12-23 17:24:34
问题 If a class has an instance field that implements IDisposable then the containing class implements IDisposable and class that fields Dispose method from within its Dispose method. public class A : IDisposable { public System.Drawing.Font font = new Font("Arial", 10.0f); public void Dispose() { font.Dispose() } } (I know I didn't do the dispose pattern correctly, but for sample code should be good enough) If the field is a static field though where should the call to the field's Dispose be?

How are IDisposable objects passed to base constructor arguments handled in C# if initialization fails?

天大地大妈咪最大 提交于 2019-12-23 13:20:29
问题 If I pass an IDisposable object to a base class constructor using the base(...) construct, I trigger a silly error in FxCop about letting loose of an IDisposable . The warning is occasionally useful elsewhere, so I don't want to disable it, but I realize I don't know the exact semantics I should be using. Is it the base constructor's responsibility to wrap its body in a try/catch to ensure the IDisposable thing is disposed of properly in case of exception? Related questions: CA2000 passing

When can't you use SafeHandle over Finalizer/IDisposable?

安稳与你 提交于 2019-12-23 13:06:47
问题 When seeing about the whole finalizer/IDisposable issue, it is usual to see that, at the end, after all the long description, there will be something to the meaning of "LOL what I said was actually useless, you should use SafeHandle instead bye!" So I am wondering in what case does SafeHandle not fit, such that you have to resort to the finalizer/IDisposable old way? 回答1: Clearly, when the unmanaged resource you are wrapping is not acquired through a handle. Which is rare but not unheard of.

Testing Finalizers and IDisposable

我们两清 提交于 2019-12-23 10:19:53
问题 The question is how can I test the fact that object disposes resources when finalise is called. The code for the class: public class TestClass : IDisposable { public bool HasBeenDisposed {get; private set; } public void Dispose() { HasBeenDisposed = true; } ~TestClass() { Dispose(); } } Please note that I don't care about the correct implementation of Dispose/Finalize just now as I want to find the way to test it first. At this stage it is enough to assume the HasBeenDisposed will be set to