Main Site Documentation

Failed to allocate


#1

For some reason I am getting this error message when I try to compile my project for the FEZ Domino:

[quote]The thread 0x2 has exited with code 0 (0x0).
GC: 4msec 56064 bytes used, 8316 bytes available
Type 0F (STRING ): 84 bytes
Type 11 (CLASS ): 1296 bytes
Type 12 (VALUETYPE ): 48 bytes
Type 13 (SZARRAY ): 4068 bytes
Type 15 (FREEBLOCK ): 8316 bytes
Type 17 (ASSEMBLY ): 12840 bytes
Type 18 (WEAKCLASS ): 48 bytes
Type 1B (DELEGATE_HEAD ): 108 bytes
Type 1D (OBJECT_TO_EVENT ): 96 bytes
Type 1E (BINARY_BLOB_HEAD ): 5316 bytes
Type 1F (THREAD ): 384 bytes
Type 20 (SUBTHREAD ): 48 bytes
Type 21 (STACK_FRAME ): 30396 bytes
Type 27 (FINALIZER_HEAD ): 120 bytes
Type 31 (IO_PORT ): 144 bytes
Type 34 (APPDOMAIN_HEAD ): 72 bytes
Type 36 (APPDOMAIN_ASSEMBLY ): 996 bytes
GC: 4msec 62904 bytes used, 1476 bytes available
Type 0F (STRING ): 84 bytes
Type 11 (CLASS ): 1296 bytes
Type 12 (VALUETYPE ): 48 bytes
Type 13 (SZARRAY ): 4788 bytes
Type 15 (FREEBLOCK ): 1476 bytes
Type 17 (ASSEMBLY ): 12840 bytes
Type 18 (WEAKCLASS ): 48 bytes
Type 1B (DELEGATE_HEAD ): 108 bytes
Type 1D (OBJECT_TO_EVENT ): 96 bytes
Type 1E (BINARY_BLOB_HEAD ): 5316 bytes
Type 1F (THREAD ): 384 bytes
Type 20 (SUBTHREAD ): 48 bytes
Type 21 (STACK_FRAME ): 36516 bytes
Type 27 (FINALIZER_HEAD ): 120 bytes
Type 31 (IO_PORT ): 144 bytes
Type 34 (APPDOMAIN_HEAD ): 72 bytes
Type 36 (APPDOMAIN_ASSEMBLY ): 996 bytes
Failed allocation for 17 blocks, 204 bytes

GC: 4msec 62964 bytes used, 1416 bytes available
Type 0F (STRING ): 84 bytes
Type 11 (CLASS ): 1356 bytes
Type 12 (VALUETYPE ): 48 bytes
Type 13 (SZARRAY ): 4788 bytes
Type 15 (FREEBLOCK ): 1416 bytes
Type 17 (ASSEMBLY ): 12840 bytes
Type 18 (WEAKCLASS ): 48 bytes
Type 1B (DELEGATE_HEAD ): 108 bytes
Type 1D (OBJECT_TO_EVENT ): 96 bytes
Type 1E (BINARY_BLOB_HEAD ): 5316 bytes
Type 1F (THREAD ): 384 bytes
Type 20 (SUBTHREAD ): 48 bytes
Type 21 (STACK_FRAME ): 36516 bytes
Type 27 (FINALIZER_HEAD ): 120 bytes
Type 31 (IO_PORT ): 144 bytes
Type 34 (APPDOMAIN_HEAD ): 72 bytes
Type 36 (APPDOMAIN_ASSEMBLY ): 996 bytes
Failed allocation for 121 blocks, 1452 bytes

GC: 4msec 63144 bytes used, 1236 bytes available
Type 0F (STRING ): 84 bytes
Type 11 (CLASS ): 1356 bytes
Type 12 (VALUETYPE ): 48 bytes
Type 13 (SZARRAY ): 4788 bytes
Type 15 (FREEBLOCK ): 1236 bytes
Type 17 (ASSEMBLY ): 12840 bytes
Type 18 (WEAKCLASS ): 48 bytes
Type 1B (DELEGATE_HEAD ): 108 bytes
Type 1D (OBJECT_TO_EVENT ): 96 bytes
Type 1E (BINARY_BLOB_HEAD ): 5496 bytes
Type 1F (THREAD ): 384 bytes
Type 20 (SUBTHREAD ): 48 bytes
Type 21 (STACK_FRAME ): 36516 bytes
Type 27 (FINALIZER_HEAD ): 120 bytes
Type 31 (IO_PORT ): 144 bytes
Type 34 (APPDOMAIN_HEAD ): 72 bytes
Type 36 (APPDOMAIN_ASSEMBLY ): 996 bytes
Failed allocation for 39 blocks, 468 bytes

GC: 4msec 63144 bytes used, 1236 bytes available
Type 0F (STRING ): 84 bytes
Type 11 (CLASS ): 1356 bytes
Type 12 (VALUETYPE ): 48 bytes
Type 13 (SZARRAY ): 4788 bytes
Type 15 (FREEBLOCK ): 1236 bytes
Type 17 (ASSEMBLY ): 12840 bytes
Type 18 (WEAKCLASS ): 48 bytes
Type 1B (DELEGATE_HEAD ): 108 bytes
Type 1D (OBJECT_TO_EVENT ): 96 bytes
Type 1E (BINARY_BLOB_HEAD ): 5496 bytes
Type 1F (THREAD ): 384 bytes
Type 20 (SUBTHREAD ): 48 bytes
Type 21 (STACK_FRAME ): 36516 bytes
Type 27 (FINALIZER_HEAD ): 120 bytes
Type 31 (IO_PORT ): 144 bytes
Type 34 (APPDOMAIN_HEAD ): 72 bytes
Type 36 (APPDOMAIN_ASSEMBLY ): 996 bytes
Failed allocation for 39 blocks, 468 bytes

GC: 4msec 63144 bytes used, 1236 bytes available
Type 0F (STRING ): 84 bytes
Type 11 (CLASS ): 1356 bytes
Type 12 (VALUETYPE ): 48 bytes
Type 13 (SZARRAY ): 4788 bytes
Type 15 (FREEBLOCK ): 1236 bytes
Type 17 (ASSEMBLY ): 12840 bytes
Type 18 (WEAKCLASS ): 48 bytes
Type 1B (DELEGATE_HEAD ): 108 bytes
Type 1D (OBJECT_TO_EVENT ): 96 bytes
Type 1E (BINARY_BLOB_HEAD ): 5496 bytes
Type 1F (THREAD ): 384 bytes
Type 20 (SUBTHREAD ): 48 bytes
Type 21 (STACK_FRAME ): 36516 bytes
Type 27 (FINALIZER_HEAD ): 120 bytes
Type 31 (IO_PORT ): 144 bytes
Type 34 (APPDOMAIN_HEAD ): 72 bytes
Type 36 (APPDOMAIN_ASSEMBLY ): 996 bytes
Failed allocation for 241 blocks, 2892 bytes

GC: 4msec 63144 bytes used, 1236 bytes available
Type 0F (STRING ): 84 bytes
Type 11 (CLASS ): 1356 bytes
Type 12 (VALUETYPE ): 48 bytes
Type 13 (SZARRAY ): 4788 bytes
Type 15 (FREEBLOCK ): 1236 bytes
Type 17 (ASSEMBLY ): 12840 bytes
Type 18 (WEAKCLASS ): 48 bytes
Type 1B (DELEGATE_HEAD ): 108 bytes
Type 1D (OBJECT_TO_EVENT ): 96 bytes
Type 1E (BINARY_BLOB_HEAD ): 5496 bytes
Type 1F (THREAD ): 384 bytes
Type 20 (SUBTHREAD ): 48 bytes
Type 21 (STACK_FRAME ): 36516 bytes
Type 27 (FINALIZER_HEAD ): 120 bytes
Type 31 (IO_PORT ): 144 bytes
Type 34 (APPDOMAIN_HEAD ): 72 bytes
Type 36 (APPDOMAIN_ASSEMBLY ): 996 bytes
Failed allocation for 241 blocks, 2892 bytes[/quote]

It seems to say that I have filled up the flash, but I don’t know how that’s possible. The entire project is only ~100 lines.


#2

You filled up RAM not flash. You must have a memory leak or your code is doing something bad. Step in code and try to see what is going on.

Have you looked at “thinking small” section in the book? :slight_smile:


#3

I looked again at the output from GC and you have used about 30kb of ram in the first run! You said your code is 100 lines so why do you need to allocate so much memory?


#4

Here is my (very messy) code.

using System.Text;
using System.IO.Ports;

namespace SLCD_API
{
	public class SLCD
	{
		public string line_one
		{
			set
			{
				lcdHandle.Write(new byte[2] { 254, 128 }, 0, 2);

				if (fillLine)
					value = padString(value);

				sendStr(value);
			}
		}

		public int backlight
		{
			set
			{
				lcdHandle.Write(new byte[3] { 27, 42, (byte)value }, 0, 3);
				//backlight = value;
			}

			get
			{
				return backlight;
			}
		}

		private SerialPort lcdHandle;
		public bool fillLine = true;

		public SLCD(string comPort)
		{
			lcdHandle = new SerialPort(comPort, 9600);
			lcdHandle.Open();
		}

		private void sendStr(string str)
		{
			byte[] bytes = Encoding.UTF8.GetBytes(str);
			lcdHandle.Write(bytes, 0, bytes.Length);
		}

		public void clear()
		{
			lcdHandle.Write(new byte[2] { 27, 48 }, 0, 2);
		}

		private string padString(string input)
		{
			for (int i = 0; i <= input.Length; i++)
			{
				input += " ";
			}

			return input;
		}
	}
}
using System;
using Microsoft.SPOT;
using GHIElectronics.NETMF.Hardware;
using GHIElectronics.NETMF.IO;
using Microsoft.SPOT.Hardware;
using GHIElectronics.NETMF.FEZ;
using Microsoft.SPOT.IO;
using System.IO.Ports;
using System.Threading;
using SLCD_API;

namespace LCDUp
{
	public class Program
	{
		public static void Main()
		{
			SLCD slcd = new SLCD("COM1");

			//slcd.clear();

			for (int i = 0; i <= 255; i++)
			{
				slcd.backlight = i;
				Thread.Sleep(5);
			}



			Thread.Sleep(1000);
			
			slcd.line_one = "##############";
			slcd.fillLine = true;
			slcd.line_one = "test...";
			slcd.fillLine = false;
			slcd.line_one = "$$";
			

		}

	}


}

#5

I am seeing minor issues there but your code is not using 30K of RAM for sure! Are you including assembly you shouldn’t be adding like System.Xml.Legacy …graphics…networking…etc.

You can attach MFDeploy on power up, reset your board and then click pin. You will see back all the boot up messages. You are adding some large assembly that you shouldn’t be adding …which is eating your memory


#6

Check your padString method. The for loop never terminates?


#7

I second that. had 5 free minutes and just uploaded the loop to my fez. memory counted down until error, boom, magic smoke, power went out, and meteors fell from the sky :slight_smile:
otherwise, good code. I might borrow some for when i get my lcd soon :slight_smile:


#8

Hi Matt, Mike and Chimp,

The code is a very beta driver for the MeLabs SLCD (http://melabs.com/). I am good friedns with the (former) owner of the company, and have two of his LCDs. They only make the LCD serial backpack, which makes it a lot easier to talk to the actual LCD.

Although you are right that the loop in the API is hosed up, the loop that fades the backlight up is also hosed. I guess that should teach me to write code in the wee morning :wink:

I’ll be putting the full driver up when I am done. Hopefully the MeLabs product has a similar command system to other serial LCDs.

For now, I have plenty of work ahead of me. At least now I know what happens when you get an infinite loop. on the FEZ :wink:


#9

It turns out that the loops had nothing to do with the problem.

The problem was with this line of code:
backlight = value;

Anyone know why?


#10

Any reason you didn’t take our serial LCD driver and modify it to fill your needs?


#11

Not really. I just wanted to experiment with C# and this seemed liek an easy project to do it with.

In particular, I am trying to figure out how things this kind of thing is done:
slcd.Lines.one = “Something on L1”;

I am already familiar the part about using a getter method to catch the string and act on it, but how is the
slcd.Lines
done? It’s obviously a class within a class (I think, anyway), but I tried a few things and was never able to get that to work. Could someone post a quick demo of how that works, please? :stuck_out_tongue:


#12

backlight = value is a recursive call.

The best way to understand what your code is doing is use the debugger and single step. You can then see the flow of your code.


#13

Ah, of course. I am new to C# (tons of other OO programming experience though) and I assumed that when the assigned the variable in the setter method it would set the raw variable, not call the setter again.

My question on the class within a class is still standing ,though :slight_smile:


#14

A variable is only defined for you when you use auto-implemented properties. For example:

bool PadLine { get; set; }

In this case, an invisible variable is defined, but you can do no more than get or set that
variable.


#15

Chris:

Not really sure about your question about a class within a class. You do not have any that I see.

Are you asking about how to define a property?

Here are examples:

public class Foo
{
    private String line1 = String.Empty;

    public Foo()
    {

    }

     // "manually" implemented property
     public String Line1
     {
          get {return line1;}
          set {line1=value;}
     }

     // second auto implemented property
     public String Line2 { get; set; }

    
}

#16

Hi Mike,

I know how to define a property and work with classes (like I said, tons of OO experience), but I never really crossed paths with classes within classes, like this:

someclass.someproperty.someotherproperty.

Instinct tells me that someproperty has been assigned to an object, but is there some other way of doing something like this?

Eventually, I want to get my LCD class to the point where there is a lines object with overloaded methods and properties for each of the lines, like this:

slcd.lines.one.clear();

How would I code something like that? All the examples I have seen involve defining a static class within a non-static class, but I was never able to get those to work. If I define a static class within a non-static class, does that mean that the static class is only static within the context of the non-static class?


#17

Chris:

How about…

public class Line
{
     public String Data{get;set}


}

public class LCD
{
    public Line[] Lines {get;set;}
}

// example

LCD lcd = new LCD();

lcd.Lines = new Line[2] ;

lcd.Lines[0].Data = "Line 0";
lcd.Lines[1].Data = "Line 1";

Could also do class within a class

public class LCD
{
     public class Line
     {


     }

     public Line[] Lines {get;set;}


}

#18

That’s pretty close to what I want. How do I make-out the different keys in the setter and getter methods?


#19

I don’t understand what [quote]make-out the different keys in the setter and getter methods?[/quote] means?


#20

Nevermind, I see what’s going on.