Git Product home page Git Product logo

Comments (5)

GoogleCodeExporter avatar GoogleCodeExporter commented on July 22, 2024
Also, the remarks regarding keeping the stream open are valid all the way back 
to the 2.0 framework.

http://msdn.microsoft.com/en-us/library/93z9ee4x(v=vs.80).aspx

Original comment by [email protected] on 21 Dec 2012 at 9:51

from exiflibrary.

GoogleCodeExporter avatar GoogleCodeExporter commented on July 22, 2024
This issue was closed by revision r98.

Original comment by oozcitak on 21 Dec 2012 at 10:02

  • Changed state: Fixed

from exiflibrary.

GoogleCodeExporter avatar GoogleCodeExporter commented on July 22, 2024
Thanks for the detailed  bug report. I fixed it as you suggested. However, a 
quick test shows that the source stream is not automatically disposed when the 
Image is disposed. It gets disposed during garbage collection when the stream 
finalizer is called. Here is the test code:

public class XStream : System.IO.FileStream
{
    protected override void Dispose(bool disposing)
    {
        base.Dispose(disposing);
        Console.WriteLine("Stream disposed");
    }

    public XStream(string filename)
        : base(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read)
    {
        ;
    }

    ~XStream()
    {
        Console.WriteLine("Stream finalizer called");
    }
}

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        XStream xs=new XStream(@"C:\test.jpg");
        using (System.Drawing.Image img = System.Drawing.Image.FromStream(xs))
        {
            Console.WriteLine("Image created");
        }
        Console.WriteLine("Exiting");
    }
}

I get the following output:

Image created
Exiting
The thread 0x1748 has exited with code 0 (0x0).
Stream finalizer called
Stream disposed

Original comment by oozcitak on 21 Dec 2012 at 10:27

from exiflibrary.

GoogleCodeExporter avatar GoogleCodeExporter commented on July 22, 2024
This is probably not a big deal though; since we have a MemoryStream.

Original comment by oozcitak on 21 Dec 2012 at 10:29

from exiflibrary.

GoogleCodeExporter avatar GoogleCodeExporter commented on July 22, 2024
I Guess you could dispose the stream if you managed the full life-time of the 
Image but as you're an API and you're passing the image back to client code you 
can't know when the lifetime of that object.

I've just had a look through Image.FromStream and it doesn't seem to track the 
Stream object within the framework. It wraps it in a GPStream and calls into 
the native gdipluss.dll . The way that the stream is (not) managed and therefor 
finalized by the GC must be (i guess) by design.

As a side note, the image itself is IDisposible, so that should be disposed 
appropriately which would remove the (last?) reference to the stream, allowing 
it to be collected.

Thanks for the quick fix.

Original comment by [email protected] on 21 Dec 2012 at 10:39

from exiflibrary.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.