Tags down


Why is MemoryStream not disposed?

By : davlap
Date : July 29 2020, 02:00 PM
I hope this helps . Disposed and eligible for garbage collection mean two different things. In particular, your stream is eligible for garbage collection not because it has been disposed, but because you have no outstanding references to that object at the point you call GC.Collect.
But, as @alexeilevenkov points out, the Debug version of the GC is not as aggressive during the Mark phase, holding references alive while a reference is within function scope (allowing you to inspect the stream reference until the function ends). Eligible for garbage collection means that, the object may get garbage collected. However, it's up to the GC to decide when things are actally collected.
code :

Share : facebook icon twitter icon

Does a MemoryStream get disposed of automatically when returning it as an ActionResult?

By : Hayashi Kiyoshi
Date : March 29 2020, 07:55 AM
Any of those help
Does a MemoryStream get disposed of automatically when returning it as an ActionResult?
code :
protected override void WriteFile(HttpResponseBase response) {
    // grab chunks of data and write to the output stream
    Stream outputStream = response.OutputStream;
    using (FileStream) {
        byte[] buffer = new byte[_bufferSize];

        while (true) {
            int bytesRead = FileStream.Read(buffer, 0, _bufferSize);
            if (bytesRead == 0) {
                // no more data

            outputStream.Write(buffer, 0, bytesRead);
public class DummyStream : MemoryStream
    protected override void Dispose(bool disposing)
        Trace.WriteLine("Do I get disposed?");

Image PropertyItems and disposed MemoryStream

By : Sumit Sulakhe
Date : March 29 2020, 07:55 AM
I wish this helpful for you Disposing a MemoryStream is in general a fairly useless thing to do. It doesn't have any disposable resources itself, it is just memory and that's already managed by the garbage collector. It only matters if you've used the BeginRead/Write() methods and they are not yet completed, something you just never do.
It does however set the CanRead() property to false. And that's quite lethal to the Bitmap object you loaded from the MemoryStream.

Does a memorystream get disposed when returning from within a using block

By : user1977869
Date : March 29 2020, 07:55 AM
will be helpful for those in need The code may work because Dispose on a MemoryStream doesn't really do anything (the memory is reclaimed by GC), but I certainly wouldn't depend on it always doing so for all .NET implementations - unless the only function you're calling is GetBuffer(), which is guaranteed to work after closing the stream. It's probably safer to remove the 'using' in this case. The "read-only" refers to the inability to re-assign the reference to another object. BTW if you look at https://github.com/dotnet/corefx/blob/master/src/Common/src/CoreLib/System/IO/MemoryStream.cs the comments in Dispose() indicate that the developers deliberately intended for the object buffer to still available after disposing, but there's no guarantee reading from it as a stream will work (and writing will almost certainly fail).

MemoryStream must be explicitely be disposed?

By : Honda VN
Date : March 29 2020, 07:55 AM
like below fixes the issue In general, all disposable objects must always be disposed.
However, MemoryStream doesn't actually need to be disposed, since it doesn't have any unmanaged resources. (It's just a byte[] and an int)

MemoryStream: System.ObjectDisposedException : Cannot access a disposed object

By : user468950
Date : March 29 2020, 07:55 AM
Hope that helps Without a good Minimal, Complete, and Verifiable code example, and particularly details about the SqlZml class, it will be impossible to diagnose your problem perfectly. But…
You are disposing memoryStream. It seems that the SqlXml constructor does not read the stream immediately, but rather waits until you attempt to actually use the SqlXml object. Since you've already disposed the memoryStream object you passed to the SqlXml constructor, you get the exception when SqlXml then tries to read from that stream.
Related Posts Related Posts :
  • Xamarin Forms - Url not displaying in Webview
  • Time zones and day light savings time in Azure functions
  • i got CS0029 C# Cannot implicitly convert type 'int' to 'bool'
  • Unable to find department name from IUserPeopleCollectionPage type Dataa MS Graph API
  • How to process data from a file in parallel in several threads and write them into another file while keeping the origin
  • How to check which animation is playing
  • Entity Framework Core generating -ve integer values for Primary Key column on a table row add
  • What is the best way to find out if a string is a roman number?
  • C# BinaryWriter not writing to file
  • Multiply relationships ef core
  • How to use generic Queue where the type also has a generic paramater?
  • POST with API in C#
  • How do I inherit from a base class and add another property?
  • Rename refactoring in VS Code
  • What is Difference Between Console.WriteLine($"welcome{name}") and Console.WriteLine("welcome {0}",n
  • Useless assignment - Sonar warning
  • How to approach microfrontends in ASP.NET?
  • How to implement dynamic values for *case* in c#?
  • Visual Studio 2019 on Linux
  • What can I use instead of a (Switch) here?
  • What is the use of private constructor in abstract class in c#?
  • Why my gameObjects are not dragged and snap?
  • Deserialize JSON to List
  • When a ".Net Standard" project is built - what does that mean?
  • Performance difference between Linq Select and ToList
  • Cannot Convert From Double To String c#
  • RS-232 erratic behavior - Follow up
  • Multiple Textbox Validations
  • Access properties of a virtual class in partial class
  • How to post a message to google chat room using C#? (**Error**: Request had insufficient authentication scopes)
  • How to get all USA timezone IDs using nodatime
  • How to pass object of unknown type to function
  • Is it OK to inherit an empty Interface?
  • If I implement IEquatable<T>, will I lose the option to compare by reference?
  • Authorize with both ASP.NET core MVC/Razor site AND a WebAPI
  • If Condition Simplification
  • ASP Net Core Web API: Client side GroupBy is not supported
  • C# I have a DLL file and I need to make a class that inherits from a class that's in the DLL file?
  • How can I cause a derived method to be called during an inherited base constructor?
  • How to subtract between two integer List in C#?
  • Using SerialPort variable through MainWindow
  • The most efficient way (performance/time wise) to find a highest value in a List with continuously changing values?
  • How to Deserialize Json string to Type without creating class
  • How to enforce this one-to-many constraint in C#?
  • MVC: Unable to update one of my model's properties
  • Task.WhenAll with LINQ select
  • I am creating the controller in asp dot net core but Giving Error
  • How to define a constraint in Ortools to set a limit of distinct values
  • How to check for Carriage Return of each element end in string array and add if not exists?
  • Multithreading | Thread was being aborted
  • Why would you use TransactionScope for Read-Only Database Calls in NET C#?
  • How to branch code based on class data type
  • Is there a way to linking two data values, simpler than Dictionary?
  • How to display a file in a folder without showing physical path?
  • Are binary tree nodes passed by reference?
  • Convert float to decimal, wrong value
  • Can someone tell me why (return DayName;) causes an ERROR cause it's an unassigned local variable?
  • Prohibit access to the private variable
  • c# How do I correctly compile code for distribution?
  • C# Passing a reference to a generic method to another method
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org