Questions tagged [idisposable]

IDisposable is an interface within the Microsoft .NET Framework's Base Class Library (BCL). It is intended to provide a generic, deterministic method of releasing unmanaged resources within .NET application code.

Filter by
Sorted by
Tagged with
1 vote
1 answer
79 views

C# Dependency between classes and IDisposable

Suppose I have 2 classes, A and B. Class A is responsible for "finding/generating" objects of B, so it has some methods that create and return instances of B. Class B has methods that find ...
Ramon Thomaz's user avatar
3 votes
1 answer
48 views

IAsyncDisposable with Using statement in VB.NET

I have a class with a resource that ideally should be disposed of using an async method, and I'm trying to use a Using statement to do so. Luckily, .NET added the IAsyncDisposable interface as well as ...
Keith Stein's user avatar
  • 6,257
0 votes
1 answer
37 views

Can creating and returning a disposable object from a static class method cause memory leaks?

I've noticed a windows service taking a lot of memory and am trying to get to the bottom of the issue. I noticed that we have a static class containing a static method which is in charge of creating ...
Ponsietta's user avatar
  • 315
0 votes
0 answers
9 views

Kinect v2 FrameArrived event suddenly stops being fired : am I leaking a IDisposable?

I'm facing a bug in my C# + Kinect v2 API application. At rare times, the Kinect2 API event BodyFrameReader.FrameArrived (which informs that a new frame is available) suddenly stops being fired, so my ...
Anselme's user avatar
0 votes
1 answer
16 views

Should i use IDisposable interface for my test project?

I'm trying to learn unit tests using NUnit in .net and I must know to dispose somehow. I'm trying to implement in my worker class like Dispose Method and etc. I didn't want to implement IDisposable ...
bgraokmush's user avatar
2 votes
2 answers
73 views

How to correctly dispose of instances that are hold within a registered C# service that does not implement IDisposable?

Assume I have the following C# class ClassA: public class ClassA { private readonly ClassB _b; public ClassA(ClassB b) { _b = b; } } This class holds a reference to an ...
dan-kli's user avatar
  • 578
0 votes
1 answer
29 views

IDisposable implementation: do the default recommendations apply when using Blazor wasm?

I'm wondering if the default rules specified on the Implement a Dispose method still apply when writing classes/components for Blazor (specifically on the SuppressFinalizemethod call, which is nowhere ...
Luis Abreu's user avatar
  • 4,028
1 vote
1 answer
59 views

Disposable object is already disposed when the inner scope is not yet finished

I want to write an IEnumerable extension to execute a Select in parallel, but with a maximum number of parallel instances. Furthermore I want this extension to have a return value of IEnumerable<...
Eckii24's user avatar
  • 86
1 vote
1 answer
752 views

How to dispose a service implementing IAsyncDisposable using Dependency Injection in .NET 7

I am having a service that uses the IAsyncDisposable interface, because it holds an object that has the interface itself. public class AddressImporter : IAddressImporter { private readonly ...
TeaJ's user avatar
  • 33
0 votes
0 answers
121 views

Basic Disposable Pattern in F#

According to https://learn.microsoft.com/en-us/dotnet/standard/design-guidelines/dispose-pattern#basic-dispose-pattern The Basic Disposable Pattern requires a protected virtual void Dispose(bool) ...
Franco Tiveron's user avatar
2 votes
4 answers
87 views

Are there ever cases where you don't want to dispose of an IDisposable object that will go out of scope?

I originally come from a C++ background where going out of scope means calling of a destructor and in that destructor is where you clean up everything. The destructor isn't a thing called later at ...
Stanton's user avatar
  • 904
1 vote
1 answer
133 views

Will a JsonDocument be automatically disposed if it is passed in as a parameter to a controller action?

I am setting up an API to accept notifications from a web hook that does not provide a uniform shape for the objects. I know of a few common properties that will help me correctly route the ...
Benjamin U.'s user avatar
3 votes
3 answers
417 views

When is a `using var` disposed? Is it out-of-scope as soon as possible or at the end of the block?

https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-8.0/using#using-declaration The lifetime of a using local will extend to the end of the scope in which it is ...
geekley's user avatar
  • 1,261
0 votes
0 answers
327 views

Coverity reporting resource leaks that look like false positives

I'm getting a lot of resource leak issues identified by Coverity in a .NET project. Here is an example: The serializer is an instance of GraphQLSerializer and is configured as a singleton (in Startup....
Marius Bancila's user avatar
0 votes
0 answers
66 views

Assuring that Objects are Disposed before Finalization in C#

I have trouble implementing a C# wrapper for rcl which has a struct named rcl_context_s encapsulating non-global state for some rcl_node_s structs. There are functions for finalizing these structs ...
Deric's user avatar
  • 180
0 votes
2 answers
106 views

Should I make sure an object is unusable after Dispose was called?

I have a class BleScanner that wraps an internal BluetoothLEAdvertisementWatcher. It also implements IDisposable to make sure that the watcher is stopped when the scanner gets disposed of. public ...
LWChris's user avatar
  • 3,330
1 vote
1 answer
128 views

IDisposable objects with using statements inside an if block get disposed prematurely when they're needed by later code

Consider the following code: using var form = new MultipartFormDataContent(); if (uploadFile) { using var fileStream = file.OpenReadStream(); using var fileContent = new StreamContent(...
thankyoussd's user avatar
  • 1,885
2 votes
1 answer
198 views

xUnit DataAttribute with disposable data

I am writing some integration tests where we have multiple hardware devices connected to a computer, and we run the test for each device separately. I made a custom DataAttribute that scans for the ...
Rick de Water's user avatar
1 vote
3 answers
346 views

Properly dispose derived class

I am trying to implement the IDisposable pattern on a derived class, and it's not working as expecting to work, Suppose I have two classes and I want to call the Dispose method of the derived class: ...
Mselmi Ali's user avatar
  • 1,139
0 votes
1 answer
316 views

DefaultObjectPool do not dispose objects

c# documentation says: When DefaultObjectPoolProvider is used and T implements IDisposable: Items that are not returned to the pool will be disposed. I've try some test app to check this out. Looks ...
Timur Lemeshko's user avatar
-1 votes
1 answer
243 views

Set all properties to null is a valid way to dispose an object? [duplicate]

Using statement requires the object type to directly implement IDisposable. Looking for the easiest to do so, came up with this snippet. public class Foo : IDisposable { public string? ...
James Jonatah's user avatar
1 vote
0 answers
148 views

Disposing of COM object out of process

I am creating an app in c# that instantiates a COM object of the commercial software StressCheck. I instantiate this in a wrapper class that implements the IDisposable interface. I dispose of it by ...
Khaoz-07's user avatar
1 vote
1 answer
102 views

How to ensure disposal of an IDisposable returned anonymously into a consuming method?

I'm creating an instance of HttpRequestMessage and return it to be used through the HTTP client. HttpClient Client { get; init } HttpRequestMessage GenerateRequest() { ... } Then, I'm using ...
Konrad Viltersten's user avatar
1 vote
0 answers
188 views

Image not showing after disposing stream in Xamarin Forms

I have an Image that is shown using ImageSource.FromStream method. For that I use Xamarin.MediaGallery plugin. Everything works fine and image is shown properly. The problem arises after I call stream'...
Junaid Pathan's user avatar
2 votes
2 answers
128 views

C# Initialize Disposable in inherited constructor

Hey so I have a base class coming from a 3rd party dll, which is dependent on a disposable. Context: IDisposable public class BaseValidator { public BaseValidator(Context context) {} } We're ...
johnny 5's user avatar
  • 20k
2 votes
0 answers
56 views

PowerShell - 100% ensure IDisposable is disposed with async operations

I've been wracking my brains (such as they are) for a solution and can't suss it. Let's say we have some code that starts a download with a [HttpResponseMessage] object. You get a task from $response....
Cainan Barbie's user avatar
0 votes
0 answers
78 views

Can singleton contains disposable property?

I have interface as below: public interface ISecretKeysStore { Task<ECDiffieHellman> GetECDiffieHellman(); } And it's implementation: public class InMemorySecretKeysStore : ISecretKeysStore ...
Szyszka947's user avatar
2 votes
1 answer
239 views

When is Dispose() of a class implementing IEnumerator<T> called automatically by Linq?

I'm studying IEnumerable<T> and IEnumerator<T>. I wrote the code referring to this example. Here's a part of the example code in the link that uses it: var stringsFound = from line in ...
isakgo_'s user avatar
  • 740
2 votes
1 answer
155 views

Can a class implementing IDisposable be a managed resource?

I'm studying the standard Dispose pattern. I'm looking at the example on MSDN and I have a question. Looking at the example, there are the following parts. ... // Other managed resource this class ...
isakgo_'s user avatar
  • 740
1 vote
1 answer
51 views

using pattern: specific action on exception [duplicate]

I'm trying to refresh my memory on C# and using pattern. Is there a possibility to have some control flow be executed automatically on exception? For instance: class Test : IDisposable { public ...
lezebulon's user avatar
  • 7,605
0 votes
2 answers
316 views

Disposable custom controls in Xamarin.Forms/MAUI

Many tutorials and guides suggest that when you create a custom control in Xamarin.Forms or .NET MAUI with a ContentView (or extending another view), it should implement IDisposable interface when ...
Riccardo Minato's user avatar
0 votes
1 answer
94 views

can I use C# IDisposable to automate job when exit the scope?

Can I use IDisposable to do automate job when triggers in exiting the scope? This case, I am using IDisposable to only do some job in the end of method, not 'disposing' resources. This is one of my ...
leffe's user avatar
  • 57
5 votes
3 answers
2k views

Implementing both IDisposable and IAsyncDisposable

Say I have a non-sealed class that does not deal with any unmanaged resources. I need to make a single async call during its disposing stage to do some clean up. There are no other managed resources ...
millie's user avatar
  • 2,642
1 vote
0 answers
158 views

LinqToDB DataConnection not disposed upon exiting using block

This issue has been perplexing me for quite a while. I'm trying to dispose of a connection to a database, but it keeps leaking no matter how I try to dispose of it. Worse yet, it's supposed to dispose ...
Johnny Cache's user avatar
  • 1,033
3 votes
1 answer
69 views

using keyword with private member assignment to manage IDisposable

I was reading Stephen Cleary's blog post about cancellation, and I saw something I haven't seen before in the following code snippet. Constructor() => CancelButton.Enabled = false; private ...
brent.reynolds's user avatar
0 votes
1 answer
536 views

Why is there no compiler or analyzer warning when an IDisposable object is not disposed within a method?

Example code: internal class Program { static void Main(string[] args) { Foo foo = new Foo(); } } public class Foo : IDisposable { public void Dispose() { // Not ...
redcalx's user avatar
  • 8,217
0 votes
0 answers
34 views

How can I establish what is causing the disposal of the `HttpListenerRequest` inside an `IOwinContext.Request`

I have a C# API using OWIN. In one of my OwinMiddleware components I read some properties off context.Request after the Controller processes the request, and sometimes this blows up with an "...
Brondahl's user avatar
  • 7,422
2 votes
1 answer
70 views

How to connect "using" keyword with Deconstruct method

im wondering if we can join using statement and deconstruct operation. To be more visual look at below sample: using System; public class Foo : IDisposable { public IDisposable Bar { get; set; } ...
Paweł Kanarek's user avatar
0 votes
1 answer
93 views

Is it correct to not dispose of SqlCommand like the example in Microsoft documentation?

The Microsoft Documentation for the SqlCommand class gives the following example code which does not dispose of the SqlCommand. SqlCommand inherits from DbCommand which implements IDisposable. Common ...
Aaron Anodide's user avatar
0 votes
0 answers
40 views

Using IDisposable in expression-bodied member [duplicate]

ShellFile implements IDisposable. public string LocalizedName => new ShellFile(Executable).FileDescription; I was unable to find anything on the internet about using disposable objects in ...
Scover's user avatar
  • 93
2 votes
2 answers
89 views

Discard async result when result implements IDisposable

using var notNeeded = await foo(); foo returns an object which implements IDisposable but that object can be discarded immediately. What is the idomatic way to write this line? using var notNeeded = ...
CookedCthulhu's user avatar
0 votes
1 answer
72 views

Does GC.SupressFinalizer() prevent GC from collecting the managed resources?

If Finalizer (destructor) is implemented in a class and GC.SupressFinalizer() is called from the overriden Dispose() method will the Garbage Collector still take care of any managed resources the ...
Milos Potic's user avatar
1 vote
0 answers
211 views

Blazor release managed and unmanaged resource

I am creating a server side Blazor application and want to release managed and unmanaged resource correctly. I am beginner of Blazor and not sure if what I did is correct or not. Please give me some ...
Frank's user avatar
  • 21
1 vote
1 answer
171 views

Make sure a subscription to an EventHandler is called at Dispose

I'm subscribing to an EventHandler in the constructor and unsubscribing it in the Dispose method. It's important for me to make sure this event is unsubscribed at Dispose, i.e. Dispose is called even ...
nop's user avatar
  • 4,751
0 votes
1 answer
201 views

Dispose a AutoResetEvent after signaling it

Let's suppose we have a class MyQueue defined as below: public class MyQueue { private bool _stop; private readonly AutoResetEvent _onQueued = new AutoResetEvent(false); private readonly ...
user2896152's user avatar
1 vote
1 answer
66 views

IDisposable questions about specific case

Question #1: StartAsync handles the disposal of _clientWebSocket and _tokenSource. So do I really need to dispose these in Dispose() as well? I think I should keep _semaphore.Dispose() only in the ...
nop's user avatar
  • 4,751
0 votes
0 answers
168 views

How do IDisposable and IAsyncDisposable both work together?

The class below is not sealed, which means it is considered inheritable. I took IDisposable/IAsyncDisposable implementation from here and I'm trying to understand why the .Dispose calls are duplicated ...
nop's user avatar
  • 4,751
2 votes
2 answers
332 views

How to get the result from IObservable subscription without a blocking call?

I'm trying to get filenames within a bucket of my MinIO server using the ListObjectsAsync method. Here is the relevant code: public async Task<string> GetFileName(string userID, string datasetID)...
Roland Deschain's user avatar
0 votes
0 answers
47 views

C# Combining using statement with IAsyncReader

I'm looking to combine an async fill operation with disposable. class G { SqlConnection connection SqlCommand command; IAyncResult Begin(string connectionString) { this....
heyNow's user avatar
  • 866
1 vote
1 answer
143 views

IDisposable Pattern - Is Dispose() invoked when an object is stored in a Thread local data?

All, I have a class FileLogger that implements IDisposable pattern. The instance of this class is stored in current thread's local data: LocalDataStoreSlot s; s = Thread.GetNamedDataSlot("logger&...
ActiveX's user avatar
  • 1,062

1
2 3 4 5
29