2

I am working on a C# code running on .NETCF 3.5 on WindowsCE 6.0 that is throwing MissingMethodExceptions for Func`2<> during runtime. The code parts where the exception occurs is random.

The weird thing is, this happens when you already use the application for a while AND where definitely many calls to Func`2 already happened. (eg. via IEnumerable.Select() or .Where()) It seems that this behaviour starts if you just load enough Types during the lifetime of the application such that the sum of assembly file sizes exceeds ~18MB. But there is enough memory (RAM) available on the device to load the Type.

Also activated LoaderLogging but to no avail. It only shows me a TypeLoad error for Func`2.

As I ran out of ideas: What could be the cause of such errors?

Unfortunately I cannot share any code because it is 1) property of the company I work for and 2) many ten-thousand lines of code.

cpt. jazz
  • 1,336
  • 12
  • 21
  • 1
    Well good luck getting someone to help you ..I am sure that you can post an example of the code without using anything that will hurt the Company.. the company doesn't own the .NET Framework.. so change some hardcoded information and make a variable if you have to to show what you are trying to accomplish – MethodMan Oct 30 '12 at 21:43
  • DJ KRAZE, I already wrote that the problem only occurs if I load **many** types. We are talking about 20+ assemblies in combination that lead to this type of error. You cannot break this down to a two-liner. – cpt. jazz Oct 30 '12 at 21:51
  • can you possibly track down or do a Stack Trace on the line and or assembly where the error is happening..? – MethodMan Oct 30 '12 at 21:55
  • That's the bad thing. It happens at random positions. The only "constant" between all the test runs I did was that it only happens if you load just enough different Types. As I wrote, it occurs if the assemblies sum up to about 18MB. The last method in the stacktrace is always a method that wants to use a Func`2. – cpt. jazz Oct 30 '12 at 21:59
  • Func'2 means it's trying to execute some `Func<,>` delegate. Is it the same generic signature on each exception? Sounds like it might just be some reflection code that can't handle a specific method. – Tejs Oct 30 '12 at 22:06
  • Yes. It is always System.Core.Func`2. And the code where it breaks works for the next application run, if only it gets loaded early enough (meaning: if I enter the window that executes that code before 18MB are consumed) – cpt. jazz Oct 30 '12 at 22:10
  • Windows CE 6.0 or Windows Mobile 6? (just to be absolutely sure) – Alan Oct 30 '12 at 23:14
  • MissingMethodException is often a TypeLoad in disguise, and the behavior sure smells like fragmentation preventing a code JIT. How are you verifying that "there's enough memory...to load the Type"? – ctacke Oct 31 '12 at 03:40
  • ctacke, I just ran a test: I have ~28MB of 77MB RAM used when the Exception occurred. Total memory consumption peaked at about 10MB and settled at about 7MB at the end. So I guess the memory should not be the problem? – cpt. jazz Oct 31 '12 at 09:18

2 Answers2

4

It seems there is a limit on .NETCF:

One can only construct 1024 unique closed types per generic type declaration. (For more information see section "Limitations": http://blogs.msdn.com/b/romanbat/archive/2005/01/06/348114.aspx)

Meaning:

List<int> a;
List<int> b;
List<int> c;

takes one "slot",

List<int> d;
List<string> e;

takes two "slots" (two unique closings), and so on.

Tricky thing was: Usually this raises an ArgumentException, but sometimes .NETCF throws MissingMethodExceptions instead. (see "3. Differences in exceptions thrown.": http://blogs.msdn.com/b/nazimms/archive/2005/01/25/360324.aspx)

We reduced the usings of Func`2 by using our own Delegate Types where possible and this has solved the problem.

cpt. jazz
  • 1,336
  • 12
  • 21
0

you already answered your question yourself, if you know the backgrounds of Windows CE memory management: "...such that the sum of assembly file sizes exceeds ~18MB. But there is enough memory (RAM) available on the device to load the Type...."

You know, that every process only gets a 32MB program memory slot, regardless of having a GB RAM or more! That is by design. You know, that DLLs loaded by your or any other running process are loaded into the 32MB slot of EVERY process (although there are not loaded with there full size with Windows Embedded Handheld 6.5). So the more DLLs are loaded, a process gets less available program memory. See also http://community.intermec.com/t5/General-Development-Developer/Slaying-the-virtual-memory-monster/m-p/16764 for more details.

If the process memory, which growing from bottom of 32MB slot, grows against the loaded DLLs, which are loaded from top of 32MB slot, you will get strange program behaviours or even crashes.

The above is valid for all Windows CE 5 based OS versions like windows Mobile 6.1, Windows Mobile 5 or even the latest Windows Mobile 6.5.3 (Embedded Handheld). The above has to be revised for Windows CE 6 based OS versions.

regards

Josef

josef
  • 5,951
  • 1
  • 13
  • 24