473,805 Members | 1,956 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# closing memory stream

2 New Member
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 10118
tlhintoq
3,525 Recognized Expert Specialist
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
horstwalter
2 New Member
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 Recognized Expert Specialist
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

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

Similar topics

0
1499
by: Andrew R. Thomas-Cramer | last post by:
I'm using XMLTextWriter to write to a socket stream. I need to write the opening root tag immediately for receipt by the client; elements beneath it will come later. m_writer.WriteStartDocument(); m_writer.WriteStartElement( "stream", "stream", "mynamespace" ); m_writer.Flush(); What I want is this:
5
7876
by: Tomaz Koritnik | last post by:
Hi I have many short HTML files stored in a binary stream storage to display descriptions for various items in application. HTML would be display inside application using some .NET control or COM control (like Microsoft WebBrowser). For each description there is one HTML file and along description text, it contains links to related information. Clicking related information would for example open new form in application and display some...
3
2642
by: Jason Chu | last post by:
I've written a file uploading part of my application using the IHttpModule. So now, I don't have the memory problem of uploading something big. Problem: I can't find which function I have to override, so that I can catch the event when while user's uploading a file, then suddenly closes the browser. In that case, couple things happens: 1) ASPNET will start eating up the uploading file on memory instead of on disk (so picture a...
3
1908
by: mohit.jha | last post by:
Iam using streamwriter class of .net to write in a file. The location of file is a network drive. Now if for some reason the network is not there and I execute sw.close I get a exception ( that's ok). But later when network comes and I try to delete this file, it says that the file is being used by a process. How to release the file handle in such a
8
10479
by: Marc Gravell | last post by:
I want to write a method that will accept a stream as a parameter, and which will write xml to the stream (based in reality on database results) using the XmlTextWriter class. However, this insists on closing my stream, and I can't convince it not to. A much-simplified example is below; basically, as soon as the writer is disposed (marked **** below) the base stream gets closed - which is a pain if I was still using it ;-p The base...
14
17232
by: cage | last post by:
hello can i write a eof to a file descriptor without closing it? like: fd.write(EOF) or something grts, ruben
10
2344
by: John Kraft | last post by:
Hello all, I'm experiencing some, imo, strange behavior with the StreamReader object I am using in the code below. Summary is that I am downloading a file from a website and saving it to disk for further parsing. I know, I could use the WebClient and it would be easier, but I don't have the flexibility I want with it. This code appears to work exactly the way I want unless the user cancels the the background operation. In that...
14
3371
by: =?Utf-8?B?UHVjY2E=?= | last post by:
Hi, I'm using VS2005 and .net 2.0. I'm creating an application that has 3 forms. I want allow users to move forward and backward with the forms and retain the data users have entered. I thought I'll make the inactive forms invisible but this is creating a memory corruption problem when user close the form2 or form3 and not the formMain. My main form has a Next button which makes the main form invisible and starts a new form which I'll...
22
3096
by: Zytan | last post by:
I have public methods in a form. The main form calls them, to update that form's display. This form is like a real-time view of data that is changing. But, the form may not exist (it is created / destroyed at user request). I can check form != null to prevent incorrect access. But, the form could disappear immediately after the check, before the method is run. Or someone could click 'close' when it's in the middle of an update. ...
0
9716
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10607
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10359
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7645
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6875
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5541
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5677
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4317
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.