469,303 Members | 1,828 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,303 developers. It's quick & easy.

C# closing memory stream

I do create an image of a form:

Expand|Select|Wrap|Line Numbers
  1.  
  2. public static Image GetImage(byte[] data) {
  3.             if (data == null) return null;
  4.             MemoryStream ms = new MemoryStream(data);
  5.             Image img = Image.FromStream(ms);
  6.             // closing the stream later crashes the application
  7.             // ms.Close(); // => memory leak
  8.             return img;
  9.         }
  10.  
My problem is, closing the stream later results in an OUT OF MEMORY exception when the image is re-drawn (and the resource where data comes from is disposed).

data[] "comes" from a driver (.dll), the issue happens when the wrapping driver object is disposed. So it looks that the byte array is somehow cleaned up (maybe it is unmanaged - but how could that be?)

I have copied the data before creating the stream (idea - if the original data are released, it does not matter) -> no chance.

Only not closing the stream "helps"

Has someone an idea what could be the reason....?
Aug 11 '09 #1
3 9293
tlhintoq
3,525 Expert 2GB
After you create the Image img, clone it into a new image, then return that.
You should be able to close the ms before returning. Not doing so would be bad.

Expand|Select|Wrap|Line Numbers
  1. Image img = Image.FromStream(ms);
  2. Image ReturnMe = (Image)img.Clone();
Aug 11 '09 #2
Thanks a lot for the response. I have tried two approaches

1. Cloning the images
2. Even cloning the bytes before opening the stream

Expand|Select|Wrap|Line Numbers
  1.             byte[] newData = (byte[])data.Clone();
  2.             MemoryStream ms = new MemoryStream(newData);
  3.             Image img = Image.FromStream(ms);
  4.             // closing the stream later crashes the application
  5.             ms.Close(); // => memory leak
  6.             return img;
  7.  
In both cases it crashed when the original object is Disposed - at least it seems to happen at this moment.

Totally confused, since the byte clone should completely unlink the original data....
Aug 11 '09 #3
tlhintoq
3,525 Expert 2GB
Cloning a byte array will not cause a crash.
Cloning an image will not cause a crash.
You have something weird going on with the MemoryStream.
Concentrate on cloning the image.
Perhaps, after you clone it, you should set the first Image img to null so it is not linked to the ms

Expand|Select|Wrap|Line Numbers
  1. Image img = Image.FromStream(ms);
  2. Image ReturnMe = (Image)img.Clone();
  3. img = null;
  4. ms.close();
  5.  
Aug 11 '09 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Andrew R. Thomas-Cramer | last post: by
5 posts views Thread by Tomaz Koritnik | last post: by
3 posts views Thread by Jason Chu | last post: by
3 posts views Thread by mohit.jha | last post: by
14 posts views Thread by cage | last post: by
10 posts views Thread by John Kraft | last post: by
14 posts views Thread by =?Utf-8?B?UHVjY2E=?= | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Geralt96 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.