xodder
xodder

Reputation: 53

Why is my overridden Dispose not called?

I'm currently working with the TextWriter class from .NET Compact Framework 3.5 to write log entries to a file. Thats a very common and simply task and there is no problem with that. But using the TextWriter and calling Dispose when all data has been written causes the file kept open.

My approach was to initialize a FileStream, then a StreamWriter using the created instance of the FileStream. Then i created a synchronized TextWriter using the static method Synchronized.

To investigate why my the file is not accessible by another process after the data has been written and flushed i derived a class from StreamWriter with an override of the Dispose method just to see if it is called. Running the code showed up the overriden Dispose method is not called and thats what confuses me.

The calling code is:

var fileStream = new FileStream("\\NAND_FLASH\\test.file", FileMode.OpenOrCreate);
var streamWriter = new ExtendedWriter(fileStream, Encoding.UTF8);

TextWriter textWriter = TextWriter.Synchronized(streamWriter);
textWriter.Dispose();

And the derived class:

internal class ExtendedWriter : StreamWriter
{
    public ExtendedWriter(Stream stream) : base(stream)
    {
    }

    public ExtendedWriter(Stream stream, Encoding encoding) : base(stream, encoding)
    {
    }

    public ExtendedWriter(Stream stream, Encoding encoding, int bufferSize) : base(stream, encoding, bufferSize)
    {
    }

    public ExtendedWriter(string path) : base(path)
    {
    }

    public ExtendedWriter(string path, bool append) : base(path, append)
    {
    }

    public ExtendedWriter(string path, bool append, Encoding encoding) : base(path, append, encoding)
    {
    }

    public ExtendedWriter(string path, bool append, Encoding encoding, int bufferSize) : base(path, append, encoding, bufferSize)
    {
    }

    protected override void Dispose(bool disposing)
    {
        Trace.WriteLine("Extended writer dispose!");
        base.Dispose(disposing);
    }
}

Can someone explain this behavior to me?

Edit: Updated question text, i missed to mention that i am working with the .NET Compact Framework 3.5

Upvotes: 3

Views: 1558

Answers (2)

ctacke
ctacke

Reputation: 67168

I just used Reflector to look at the TextWriter in the CF 3.5 BCL.

Here's the Dispose, which you're calling:

public void Dispose()
{
    this.Dispose(true);
    GC.SuppressFinalize(this);
}

So this is calling the parameterized Dispose, which looks like this:

protected virtual void Dispose(bool disposing)
{
}

So there's the explanation as to why your derived StreamWriter isn't getting Disposed - the TextWriter doesn't call Dispose on its contained StreamWriter. You'll have to Dispose your object yourself.

Upvotes: 2

user153923
user153923

Reputation:

I do not think everything does what it is supposed to whenever it implements the IDisposable interface.

For example, using a Microsoft Access database connection (OleDb) will never close the connection if you open it in a using clause.

My guess is this you found another control that does not do everything it is supposed to do.

Upvotes: 1

Related Questions