Bugzilla – Bug 313797
NullReferenceException from embedded api, but not from the mono JIT
Last modified: 2007-09-15 21:24:46 UTC
---- Reported by zealy@freemail.hu 2004-02-04 16:49:06 MST ---- Description of Problem: The followings apply both to mono 0.30 built from source and the official debian package mono 0.29.999. When using libmono to call methods of .NET classes from within C, on the first attempt to use globalisation (using the type System.String in my case), an exception NullReferenceException is thrown in System.Globalization.CultureInfo. (See the section Actual output below for details). The "equivalent" C# code works as expected. Steps to reproduce the problem: 1. Compile the following C code #include <glib.h> #include <mono/jit/jit.h> #include <mono/metadata/object.h> #include <mono/metadata/debug-helpers.h> #include <mono/metadata/appdomain.h> extern void *mono_jit_trace_calls; int main(int argc, char **argv) { gchar* str = "What bug?"; gpointer params[1]; MonoDomain *domain = NULL; domain = mono_jit_init ("gotbug"); MonoAssembly *assembly = NULL; assembly = mono_domain_assembly_open (domain, "/usr/lib/mscorlib.dll"); // uncomment to enable tracing // mono_jit_trace_calls=mono_trace_parse_options(assembly, ""); // MonoClass *class; class = (MonoClass*) mono_class_from_name (assembly- >image, "System", "Console"); mono_class_init (class); static MonoMethod *method; MonoMethodDesc *desc = mono_method_desc_new (":WriteLine(string)", FALSE); method = mono_method_desc_search_in_class (desc, class); params[0] = (gpointer*) mono_string_new (domain, str); mono_runtime_invoke (method, NULL, params, NULL); return 0; } 2. Compile the following .cs code using System; public class GotBug { static int Main(string[] args) { Console.WriteLine("What bug?"); return 0; } } 3. Compare the outputs of the above programs Actual Results: Unhandled Exception: System.TypeInitializationException: An exception was thrown by the type initializer for System.Console ---> System.NullReferenceException: A null value was found where an object instance was required in <0x0000f> System.Globalization.CultureInfo:get_CurrentCulture () in <0x00010> System.String:ToUpper () in <0x0003f> System.Console:.cctor () --- End of inner exception stack trace --- Expected Results: "What bug?" echoed on the tty How often does this happen? Always Additional Information: ---- Additional Comments From vargaz@freemail.hu 2004-02-05 07:02:57 MST ---- You need to call mono_thread_attach (domain) before calling mono_runtime_invoke so the runtime can create its per-thread data structures. ---- Additional Comments From vargaz@freemail.hu 2004-02-05 07:04:11 MST ---- *** https://bugzilla.novell.com/show_bug.cgi?id=MONO52375 has been marked as a duplicate of this bug. *** ---- Additional Comments From zealy@freemail.hu 2004-02-05 10:06:18 MST ---- And the documentation mentions it so, but I quite missed that it applies to me as well, having no threads created by me but having threads created by the system quietly :) (The documentation my make good use of some revising anyway) Unknown bug field "cf_op_sys_details" encountered while moving bug <cf_op_sys_details>dist-upgraded to sid</cf_op_sys_details>