need help i writing some info in SD card using XmlWriter but comming exepiton System.OutOfMemoryException’ occurred in MFDpwsExtensions.dll on this position “var msWriter = XmlWriter.Create(stream)” Fez Domino
OutOfMemoryException means you run out of free memory.
Please follow the steps outlined in the following post, so we can help you better:
private static SDStorageDevice mInstance;
//private ArrayList mCache = new ArrayList();
private DateTime mLastWriteTime = DateTime.MinValue;
private FileStream mStream = null;
private string mSDRootDirectory = null;
private string mFileName = null;
CacheClass cc = new CacheClass();
//MassStorageDevice msd = new MassStorageDevice();
PersistentStorage SDPS;
#endregion
#region Properties
public SDStorageDevice() {
mInstance = this;
}
private static SDStorageDevice Instance {
get {
return mInstance;
}
}
#endregion
public static void SDStart() {
mInstance.StartProccess();
#region MyRegion
private void StartProccess() {
if (PersistentStorage.DetectSDCard()) {
if (ItemsToBeWritten()) {
WriteItems();
ResetLastOpenedFile();
MassStorageDevice msd = new MassStorageDevice();
msd.Start();
}
} else {
FEZ_Extensions.Large128x64Display.Print(0, 5, "SD card Not");
}
SDPS = new PersistentStorage("SD");
SDPS.MountFileSystem();
}
public FileStream GetCurrentFile() {
var time = DateTimeService.Time;
mSDRootDirectory = string.Concat(VolumeInfo.GetVolumes()[0].RootDirectory, "\\", time.ToString("yyyy.MM"));
CreateDir(mSDRootDirectory);
var fileName = (string.Concat(mSDRootDirectory, "\\", time.ToString("yyyy.MM.dd"), ".xml"));
if (mFileName != fileName) {
ResetLastOpenedFile();
mFileName = fileName;
mStream = CreateStreamByFileName(fileName);
} else if (mStream == null) {
mStream = CreateStreamByFileName(fileName);
}
return mStream;
}
private void CreateDir(string dirname) {
if (!Directory.Exists(dirname)) {
Directory.CreateDirectory(dirname);
}
}
private FileStream CreateStreamByFileName(string fileName) {
var writer = new FileStream(fileName, FileMode.OpenOrCreate);
return writer;
}
private bool ItemsToBeWritten() {
var result = false;
// check time diff
if (mLastWriteTime != DateTime.MinValue) {
var diff = DateTimeService.Time.Subtract(mLastWriteTime);
//if (diff > MaxDelayBetweenWrites) {
result = true;
//}
}
mLastWriteTime = DateTimeService.Time;
return result;
}
private void WriteItems() {
var stream = GetCurrentFile();
using (var msWriter = XmlWriter.Create(stream)) {
msWriter.WriteStartElement("items");
foreach (StorageItem item in cc.myCache) {
item.WriteXML(msWriter);
}
msWriter.WriteEndElement();
}
}
private void ResetLastOpenedFile() {
try {
if (mStream != null) {
mStream.Flush();
//mStream.Close();
mStream = null;
}
} catch (Exception ex) {
}
}
}
}
this is SD Card source and next code is WriteToXML which called by SD To Write.
using System;
using System.Ext.Xml;
using System.IO;
using System.Text;
using Microsoft.SPOT.Cryptography;
using GHIElectronics.NETMF.FEZ;
using TestBase64;
namespace MyGPS.Model {
public class StorageItem {
private const string ElementName_StorageItem = "item";
private const string ElementName_Data = "data";
private const string ElementName_StringValue = "stringValue";
private const string ElementName_Type = "type";
private const string ElementName_Time = "time";
private const string Element_Impulse = "ImpulseIn";
public string StringValue { get; set; }
public byte[] Data { get; set; }
public DataType Type { get; set; }
public DateTime Time { get; set; }
public float Impulse { get; set; }
public void WriteXML(XmlWriter writer) {
WriteStartElement(writer, ElementName_StorageItem);
WriteElement(writer, ElementName_Data, Data);
WriteElement(writer, ElementName_StringValue, StringValue);
WriteElement(writer, ElementName_Type, Type);
WriteElement(writer, ElementName_Time, Time);
WriteElement(writer, Element_Impulse, Impulse);
WriteEndElement(writer, ElementName_StorageItem);
}
private void WriteEndElement(XmlWriter writer, string name) {
writer.WriteEndElement();
}
private void WriteStartElement(XmlWriter writer, string name) {
writer.WriteStartElement(name);
}
private void WriteElement(XmlWriter writer, string name, object value) {
try {
writer.WriteStartElement(name);
writer.WriteString(value == null ? null : value.ToString());
writer.WriteEndElement();
} catch (Exception ex) {
FEZ_Extensions.Large128x64Display.Print(0, 3, ex.ToString());
}
}
}
}
and coming exception sometimes CreateStreamByFileName and somethimes var msWriter = XmlWriter.Create(stream) here. and i have not idea why this heppening, also This problem is only with SD, With MassStorege its working successfuly, also this source with SD working i dont know why once working and next debbuging dont working :’( :’( sorry i don’t know English language well and what i could i expleined
Try adding a delay after SDPS.MountFileSystem();
tnx it worked