Failed allocation

I have the following code…

/*
 * Sparkfun 9DoF AHRS Razor IMU Driver
 *	Coded by Chris Seto August 2010
 *	<chris@ chrisseto.com> 
 *	
 * Note: This driver requires a slight modifcation to the IMU firmware. 
 * Please use the output.pde file that comes bundled with this driver to include 
 * the mod in your IMU's firmware.
 *	
 * Use this code for whatever you want. Modify it, redistribute it, I don't care.
 * I do ask that you please keep this header intact, however.
 * If you modfy the driver, please include your contribution below:
 * 
 * Chris Seto: Inital release (1.0)
 * 
 * 
 * */

using System.IO.Ports;

namespace Razor_9DoF_API
{
	class Razor_9DoF
	{
		/// <summary>
		/// The Serial Port handle for the Razor
		/// </summary>
		private SerialPort _Razor;

		/// <summary>
		/// Yaw degrees (0- 360) 
		/// </summary>
		public double Yaw
		{
			get
			{
				return map((long)raws[0], -180, 180, 0, 360);
			}
		}

		/// <summary>
		/// Keep the values here until we process them
		/// </summary>
		private double[] raws = new double[3];

		/// <summary>
		/// Construct, set the recv event, bind the serial handle
		/// </summary>
		/// <param name="razorPort"></param>
		public Razor_9DoF(string razorPort)
		{
			// Bind the serial port
			_Razor = new SerialPort(razorPort, 57600);

			// Options..
			_Razor.ReadTimeout = 0;

			// Open the port
			_Razor.Open();

			// Set the event handler
			_Razor.DataReceived += new SerialDataReceivedEventHandler(_Razor_DataReceived);
		}

		void _Razor_DataReceived(object sender, SerialDataReceivedEventArgs e)
		{
			// Create the buffer
			byte[] buffer = new byte[50];

			// Read the serial buffer
			_Razor.Read(buffer, 0, 50);

			// Get one sentence (Yes, I know, FUGLY!)
			string[] recv = bytesToString(buffer).Split('\n')[0].Split(',');

			// If it's complete, process it
			if (recv.Length > 3)
			{
				// Loop through the values in the sentence, parse 
				// them and add them to the raws array
				for (int i = 1; i <= 3; i++)
					raws[i - 1] = double.Parse(recv[i]);
			}
		}

		/// <summary>
		/// Turn a byte array into a string
		/// </summary>
		/// <param name="bytes"></param>
		/// <returns></returns>
		public string bytesToString(byte[] bytes)
		{
			string s = "";

			// Loop through the bytes in the arry and cast the values as chars,
			// then append them to the string
			for (int i = 0; i < bytes.Length; ++i)
				s += (char)bytes[i];

			return s;
		}


		/// <summary>
		/// Used internally
		/// </summary>
		/// <param name="x"></param>
		/// <param name="in_min"></param>
		/// <param name="in_max"></param>
		/// <param name="out_min"></param>
		/// <param name="out_max"></param>
		/// <returns></returns>
		private long map(long x, long in_min, long in_max, long out_min, long out_max)
		{
			return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
		}
	}
}

When the code gets to the _Razor.read part, I get this message…

[quote]GC: 2msec 63576 bytes used, 804 bytes available
Type 0F (STRING ): 72 bytes
Type 11 (CLASS ): 1788 bytes
Type 12 (VALUETYPE ): 36 bytes
Type 13 (SZARRAY ): 516 bytes
Type 15 (FREEBLOCK ): 804 bytes
Type 17 (ASSEMBLY ): 11364 bytes
Type 18 (WEAKCLASS ): 48 bytes
Type 1B (DELEGATE_HEAD ): 252 bytes
Type 1D (OBJECT_TO_EVENT ): 144 bytes
Type 1E (BINARY_BLOB_HEAD ): 46080 bytes
Type 1F (THREAD ): 768 bytes
Type 20 (SUBTHREAD ): 96 bytes
Type 21 (STACK_FRAME ): 1068 bytes
Type 27 (FINALIZER_HEAD ): 216 bytes
Type 31 (IO_PORT ): 216 bytes
Type 34 (APPDOMAIN_HEAD ): 72 bytes
Type 36 (APPDOMAIN_ASSEMBLY ): 840 bytes
Failed allocation for 13 blocks, 156 bytes
[/quote]

I know I don’t have that much coded, so it seems like there is some sort of memory hole in the read method. Any ideas?

you are down to 800 bytes of memory. You must have some large buffer that you do not need or some assemblies that you do not need.

Chris,

If you know you’re not using anything that large perhaps your creating and destroying objects too fast for the GarbageCollector to keep up. If you use Debug.GC(True) it will suspend your threads and force the collector. Looks like most of your memory is being located to blobs (bitmaps etc) as well, make sure you set them to null when you’re done.

Hi Gus, no, I figured it out.

I’m an idiot. I forgot that there was an \n after every sentence. The Double.Parse() method saw the \n and threw a hissy fit. I don’t have a ton of code written, so I doubt I’m using that much memory…