memorystream

Is a memory leak created if a MemoryStream in .NET is not closed?

﹥>﹥吖頭↗ 提交于 2019-11-26 10:29:30
I have the following code: MemoryStream foo(){ MemoryStream ms = new MemoryStream(); // write stuff to ms return ms; } void bar(){ MemoryStream ms2 = foo(); // do stuff with ms2 return; } Is there any chance that the MemoryStream that I've allocated will somehow fail to be disposed of later? I've got a peer review insisting that I manually close this, and I can't find the information to tell if he has a valid point or not. If something is Disposable, you should always Dispose it. You should be using a using statement in your bar() method to make sure ms2 gets Disposed. It will eventually get

Converting TMemoryStream to 'String' in Delphi 2009

社会主义新天地 提交于 2019-11-26 08:08:46
问题 We had the following code prior to Delphi 2009: function MemoryStreamToString(M : TMemoryStream): String; var NewCapacity: Longint; begin if (M.Size = > 0) or (M.Memory = nil) then Result:= \'\' else begin if TMemoryStreamProtected(M).Capacity = M.Size then begin NewCapacity:= M.Size+1; TMemoryStreamProtected(M).Realloc(NewCapacity); end; NullString(M.Memory^)[M.Size]:= #0; Result:= StrPas(M.Memory); end; end; How might we convert this code to support Unicode now with Delphi 2009? 回答1: The

Save and load MemoryStream to/from a file

元气小坏坏 提交于 2019-11-26 06:26:00
I am serializing an structure into a MemoryStream and I want to save and load the serialized structure. So, How to Save a MemoryStream into a file and also load it back from file? You may use MemoryStream.WriteTo or Stream.CopyTo (supported in framework version 4.5.2, 4.5.1, 4.5, 4) methods to write content of memory stream to another stream. memoryStream.WriteTo(fileStream); Update: fileStream.CopyTo(memoryStream); memoryStream.CopyTo(fileStream); Assuming that MemoryStream name is ms . This code writes down MemoryStream to a file: using (FileStream file = new FileStream("file.bin", FileMode

How do you get a string from a MemoryStream?

落爺英雄遲暮 提交于 2019-11-26 03:47:30
问题 If I am given a MemoryStream that I know has been populated with a String , how do I get a String back out? 回答1: This sample shows how to read and write a string to a MemoryStream. Imports System.IO Module Module1 Sub Main() ' We don't need to dispose any of the MemoryStream ' because it is a managed object. However, just for ' good practice, we'll close the MemoryStream. Using ms As New MemoryStream Dim sw As New StreamWriter(ms) sw.WriteLine("Hello World") ' The string is currently stored

Is a memory leak created if a MemoryStream in .NET is not closed?

我的未来我决定 提交于 2019-11-26 01:59:22
问题 I have the following code: MemoryStream foo(){ MemoryStream ms = new MemoryStream(); // write stuff to ms return ms; } void bar(){ MemoryStream ms2 = foo(); // do stuff with ms2 return; } Is there any chance that the MemoryStream that I\'ve allocated will somehow fail to be disposed of later? I\'ve got a peer review insisting that I manually close this, and I can\'t find the information to tell if he has a valid point or not. 回答1: If something is Disposable, you should always Dispose it. You

Attach a file from MemoryStream to a MailMessage in C#

给你一囗甜甜゛ 提交于 2019-11-26 01:58:15
问题 I am writing a program to attach a file to email. Currently I am saving file using FileStream into disk, and then I use System.Net.Mail.MailMessage.Attachments.Add( new System.Net.Mail.Attachment(\"file name\")); I do not want to store file in disk, I want to store file in memory and from memory stream pass this to Attachment . 回答1: Here is the sample code. System.IO.MemoryStream ms = new System.IO.MemoryStream(); System.IO.StreamWriter writer = new System.IO.StreamWriter(ms); writer.Write(