AssemblyDefinition.ReadAssembly fails: The type initializer for 'Mono.Cecil.Metadata.TableHeap' threw an exception.

400 views
Skip to first unread message

ShdNx

unread,
Mar 24, 2011, 2:33:09 PM3/24/11
to mono-cecil
Hey,

I'm trying to read a small test assembly using
AssemblyDefinition.ReadAssembly, and I get the following exception:

System.TypeInitializationException: The type initializer for
'Mono.Cecil.Metadata.TableHeap' threw an exception.
at Mono.Cecil.Metadata.TableHeap..ctor(Section section, UInt32
start, UInt32 size)
at Mono.Cecil.PE.ImageReader.ReadMetadataStream(Section section)
at Mono.Cecil.PE.ImageReader.ReadMetadata()
at Mono.Cecil.PE.ImageReader.ReadImage()
at Mono.Cecil.PE.ImageReader.ReadImageFrom(Stream stream)
at Mono.Cecil.ModuleDefinition.ReadModule(Stream stream,
ReaderParameters parameters)
at Mono.Cecil.ModuleDefinition.ReadModule(String fileName,
ReaderParameters parameters)
at Mono.Cecil.ModuleDefinition.ReadModule(String fileName)
at Mono.Cecil.AssemblyDefinition.ReadAssembly(String fileName)
at MyProgram.Main(String[] args) in C:\Dev\...\Program.cs:line 44
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly,
String[] args)
at System.AppDomain.nExecuteAssembly(RuntimeAssembly assembly,
String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence
assemblySecurity, String[] args)
at
Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state, Boolean
ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

Here is the InnerException:

System.ArgumentException: Value does not fall within the expected
range.
at
System.Runtime.CompilerServices.RuntimeHelpers.InitializeArray(Array
array, RuntimeFieldHandle fldHandle)
at Mono.Cecil.Metadata.TableHeap..cctor()

The test assembly I'm trying to load is a tiny .NET 4 console
application, here is the complete source code:

class Program
{
[Cached]
private static string _SomeExpensiveMethod()
{
Console.WriteLine("Expensive operation...");
return "stuff";
}

[Cached]
private static StringBuilder _GetBuilder()
{
Console.WriteLine("Creating builder...");
return new StringBuilder();
}

static void Main(string[] args)
{
string r1 = _SomeExpensiveMethod();
Console.WriteLine("Res: " + r1);
string r2 = _SomeExpensiveMethod();
Console.WriteLine("Res: " + r2);

_GetBuilder();
_GetBuilder();

Console.ReadKey();
}
}

The only thing worth mentioning are the CachedAttributes: they are
custom attributes from a project-referenced assembly.
What is interesting is that when I try to run my application in the
post-build event of the test project above, it works fine - I get the
error when I start the application directly. At first I thought that
it was some issue with relative paths, but the paths are relative in
the post-build event as well.

I have also seen this exception earlier when trying to open an
obfuscated assembly (my test assembly is NOT obfuscated), but I
thought it was because of the obfuscation. Now I'm not so sure.

Mono.Cecil version 0.9.4.0, binaries only. I could download the source
code, but that would mean installing git, which I'd never use
afterwards, and I hate having programs lying around.

Any hints?
Thank you very much in advance! And thanks for this great library!

Jb Evain

unread,
Mar 24, 2011, 8:20:33 PM3/24/11
to mono-...@googlegroups.com
Hi, could you attach or upload the binary that triggers the crash somewhere ?

Thanks !

> --
> --
> mono-cecil

Gábor Kozár

unread,
Mar 25, 2011, 4:13:41 AM3/25/11
to mono-...@googlegroups.com
Hey,


Hope it helps! The binaries are compiled in Debug mode - I don't know whether that's relevant - so I've also attached the pdb files.

2011/3/25 Jb Evain <jbe...@gmail.com>
--
--
mono-cecil

Gábor Kozár

unread,
Mar 26, 2011, 7:48:32 AM3/26/11
to mono-...@googlegroups.com
It appears that this bug only occurs if I have the VS 2010 debugger attached to the assembly that tries to read the test assembly.
If I start the application in Debug mode, but without the debugger attached, read the assembly, and attach the debugger afterwards, everything works fine (though I haven't tried actually saving the new assembly yet).Very weird.

Any idea what's causing it?

2011/3/25 Gábor Kozár <kozar...@gmail.com>

Jb Evain

unread,
Mar 26, 2011, 7:54:11 AM3/26/11
to mono-...@googlegroups.com
Hi,

On Sat, Mar 26, 2011 at 12:48 PM, Gábor Kozár <kozar...@gmail.com> wrote:
> Any idea what's causing it?

Yes, the assembly probably has edit and continue informations that may
not be supported by Cecil. I'll see about adding that.

Jb

Gábor Kozár

unread,
Mar 26, 2011, 7:58:01 AM3/26/11
to mono-...@googlegroups.com
Thank you very much!

2011/3/26 Jb Evain <jbe...@gmail.com>
--
--
mono-cecil

Jb Evain

unread,
Mar 27, 2011, 5:47:36 AM3/27/11
to mono-...@googlegroups.com
Hi,

On Sat, Mar 26, 2011 at 12:54 PM, Jb Evain <jbe...@gmail.com> wrote:
> Yes, the assembly probably has edit and continue informations that may
> not be supported by Cecil. I'll see about adding that.

Could you give a try to master? I can't reproduce it here.

Thanks,

Jb

Gábor Kozár

unread,
Mar 27, 2011, 10:44:10 AM3/27/11
to mono-...@googlegroups.com
Hey,

Works perfectly on master, thanks! Weird - I thought I was using the latest binaries from the master build, but apparently I was wrong.
Sorry about this false alarm!

Thank you very much for your help!

2011/3/27 Jb Evain <jbe...@gmail.com>
--
--
mono-cecil

Reply all
Reply to author
Forward
0 new messages