0

I am using a create method for a constructor of a converter.

public void loadData()
{
    byte [] data = new byte [] {......}; // some byte data in here
    var converter = GetDataConverter(data);
}

Now inside the GetDataConverter I need to create a memorystream from the binary data (the converter is 3rd party and takes a stream) If I write the GetDataConverter like this I get an error telling me I didnt' dispose which I understand. I created a MemoryStream and I need to dispose.

public MyDataConverter GetDataConverter(byte [] data)
{
    return new MyDataConverter(new MemoryStream(data));
}

So my solution would be this:

public MyDataConverter GetDataConverter(byte [] data)
{
    using(var ms = new MemoryStream(data))
    {
       return new MyDataConverter(ms);
    }
}

The question is, is my solution correct? Should I be using a 'using' here? isn't the 'using' going to destroy my memory stream once it's out of scope so the converter will have nothing to work on?

I need an answer AND an explanation please, I'm a bit vague on the whole 'using' thing here.

Thanks

9
  • 1
    possible duplicate of C# using statement Commented Jan 15, 2014 at 11:52
  • 2
    ´MyDataConverter´ should implement ´IDisposable´ and take care of disposing the ´MemoryStream´. Commented Jan 15, 2014 at 11:55
  • @vape: That one doesn't really answer my question. I want to know how will the 'using' affect the rest of my converter if I call it around the constructor. Commented Jan 15, 2014 at 11:56
  • 1
    @user3177615 use solution which Alessandro suggests (actually I think it worth being posted as answer) Commented Jan 15, 2014 at 11:57
  • 2
    @user3177615 because it uses disposable resource. When resource is not needed anymore, it is responsibility of resource user to clean up that resource. Rule of thumb is following - if your class uses IDisposable resource, then class should be IDisposable also Commented Jan 15, 2014 at 12:01

2 Answers 2

0

If you have no access to the code of ´MyDataConverter´ and the type doesn't implements ´IDisposable´ you can do:

public void loadData()
{
    byte[] data = new byte[] { 0 }; // some byte data in here

    using (var stream = new MemoryStream(data))
    {
        var converter = new MyDataConverter(stream);

        // do work here...
    }
}

If you have to use this many times and want to reuse your code you can do something like this:

public void loadData()
{
    byte[] data = new byte[] { 0 }; // some byte data in here

    UsingConverter(data, x =>
    {
        // do work here...
    });
}

void UsingConverter(byte[] data, Action<MyDataConverter> action)
{
    using (var stream = new MemoryStream(data))
    {
        var converter = new MyDataConverter(stream);

        action(converter);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

It really depends on the implementation of MyDataConverter. If the MemoryStream is only used inside the constructor to retrieve some data from it, then your solution with using is OK. If, OTOH, MyDataConverter keeps a reference to the MemoryStream to access it later, you must not dispose it here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.