PsychoPlayce -_-
PsychoPlayce -_-

Reputation: 9

Memory leaks when embedding C# Mono into a C++ project

Currently I am trying to embed C# Mono into my MSVC C++ project, but I experienced a large amount (maybe hundreds) of memory leaks. (I ran _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF); to check for leaks.) When I try to track where they are allocated, I find out they're from Mono. All that my code does is simply init and unload.

My code:

mono_set_assemblies_path("../lib/mono");
MonoDomain* rootDomain = mono_jit_init("TestRuntime");
if (rootDomain == nullptr) {
    throw("Failed to initialize Mono");
    return 0;
}

// Create a new app domain
// @param domainName: Name of the domain
// @param configFile: Config file to use
char domainName[] = "TestRuntime";
MonoDomain* appDomain = mono_domain_create_appdomain(domainName, nullptr);

// Set the app domain as the active domain
// @param domain: The domain to set as active
// @param force: If true, the domain is set as the active domain even if it is not the root domain
mono_domain_set(appDomain, true);

//////MonoAssembly* CoreAssembly = loadCSharpAssembly("../Extern/Scripts/ScriptCore.dll");
//////MonoImage* assemblyImage = mono_assembly_get_image(CoreAssembly);

mono_domain_set(mono_get_root_domain(), false);

mono_domain_unload(appDomain);
appDomain = nullptr;

mono_jit_cleanup(rootDomain);
rootDomain = nullptr;

It first happened on my main game project. I tried to build a test project from scratch to see if it would help, but there are still memory leaks. In both cases, I tried to reference Cherno to link Mono into my project. I even copied all the necessary folders, like lib and include, but it didn't help. Is it an issue with my setup, or something else?

Upvotes: 0

Views: 46

Answers (0)

Related Questions