2

Background / Description of problem I'm trying to run c# scripts as part of my c++ application which I'm compiling in Debian-9 Linux. However, my application crashes with an error from coming from the mono runtime.

I have installed mono from Debian repositories and have even tried the same experiment with a version of mono compiled by from source. The c# code, I have compiled into a dll using mcs -t:library *.cs . When trying to run my application it crashes with the following error:

* Assertion at object.c:116, condition `is_ok (error)' not met, 

function:mono_runtime_object_init, (null) assembly:/usr/lib/mono/4.5/mscorlib.dll type:TypeInitializationException member:(null)


=================================================================
    Native Crash Reporting
=================================================================
Got a SIGABRT while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries 
used by your application.
=================================================================

=================================================================
    Native stacktrace:
=================================================================
    0x7f80f3fb9ccb - /usr/lib/libmonosgen-2.0.so.1 : 
    0x7f80f3fba069 - /usr/lib/libmonosgen-2.0.so.1 : 
    0x7f80f3f4479f - /usr/lib/libmonosgen-2.0.so.1 : 
    0x7f80f3fb9273 - /usr/lib/libmonosgen-2.0.so.1 : 
    0x7f80f3e5b510 - /lib/x86_64-linux-gnu/libpthread.so.0 : 
    0x7f80f3994081 - /lib/x86_64-linux-gnu/libc.so.6 : gsignal
    0x7f80f397f535 - /lib/x86_64-linux-gnu/libc.so.6 : abort
    0x7f80f3ea6f94 - /usr/lib/libmonosgen-2.0.so.1 : 
    0x7f80f41935f6 - /usr/lib/libmonosgen-2.0.so.1 : 
    0x7f80f41ae2db - /usr/lib/libmonosgen-2.0.so.1 : 
    0x7f80f41ae89d - /usr/lib/libmonosgen-2.0.so.1 : monoeg_assertion_message
    0x7f80f40bf357 - /usr/lib/libmonosgen-2.0.so.1 : mono_runtime_object_init
    0x561bf41542e0 - ./mono_test : 
    0x7f80f3980bbb - /lib/x86_64-linux-gnu/libc.so.6 : __libc_start_main
    0x561bf415412a - ./mono_test : 
    .... more details follow ....

Minimal example C++ code (main.cpp):

#include <mono/jit/jit.h>
#include <mono/metadata/assembly.h>

#include <stdexcept>

int main(){
    auto domain = mono_jit_init("MyApp");  
    
    auto assembly = mono_domain_assembly_open(domain, "hello.dll");
    if (assembly == nullptr) {
        throw std::runtime_error("Error loading mono assembly hello.dll");
    }

    auto image = mono_assembly_get_image(assembly);
    auto mono_class = mono_class_from_name(image, "Test", "Hello");
    if (mono_class == nullptr) {
        throw std::runtime_error("Error finding class Test::Hello");
    }

    auto instance = mono_object_new(domain, mono_class);
    mono_runtime_object_init(instance);

    mono_jit_cleanup(domain);
}

C# code (hello.cs):

using System;

namespace Test {
    class Hello {

        Hello() {
            Console.WriteLine("Hello constructor.");
        }

        void Update() {
            Console.WriteLine("Yay update is called.");
        }

    }
}

CMakeLists:

cmake_minimum_required(VERSION 3.9)

project(mono_test)
set (CMAKE_CXX_FLAGS "-std=c++17")


file(GLOB SOURCES *.cpp)
add_executable(mono_test ${SOURCES})

INCLUDE(FindPkgConfig)

PKG_SEARCH_MODULE(MONO REQUIRED mono-2)

target_include_directories(mono_test SYSTEM PRIVATE ${MONO_INCLUDE_DIRS})

target_link_libraries(mono_test ${MONO_LIBRARIES})

I've been reading different tutorials and trying different things all day (including recompiling mono from source) without any success. I have no experience with embedding mono and don't really know how to interpret the crash report or debug it. Any help would be greatly appreciated.

Antiro42
  • 177
  • 8

1 Answers1

0

In case anyone else lands here in the future, you have to call mono_set_assemblies_path() before mono_jit_init():

mono_set_assemblies_path("mono/lib");
MonoDomain* domain = mono_jit_init("MyApp");

And make SURE there's this .dll in "[working_dir]/mono/lib/mono/4.5/mscorlib.dll", otherwise mono_jit_init() will immediately crash the application without any error or call stack.

Here's a livestream of mono_jit_init() crashing: https://youtu.be/EKnJOwGhPj4?t=7099

Andrei Despinoiu
  • 370
  • 3
  • 17