mono / CppSharp

Tools and libraries to glue C/C++ APIs to high-level languages
MIT License
3.12k stars 514 forks source link

I get "Heap has been corrupted" when I run the generated bindings of native C++ code built in Debug mode in .Net 6 project #1689

Open ehabawni opened 2 years ago

ehabawni commented 2 years ago

# Brief Description

OS: Windows 10 / .Net6 Target: MSVC/ Visual Studio 2022 / C++17/C11 / Native is Built in Debug Mode

When I generate CppSharp Bindings from a C++ project built in Debug Mode and use the generating bindings and the native DLL in.Net 6, everything compiles successfully, but when I run the.Net project and try to call some functions, I get an unhandled exception: the heap has been corrupted, and occasionally memory access violations.

I'm using the following Setup and Parsing Options:

public void Setup(Driver driver)
        {
            var options = driver.Options;
            options.Compilation.Target = CompilationTarget.SharedLibrary;
            options.Quiet = _options.quietBuild;
            options.CheckSymbols = _options.checkSymbols;
            options.Compilation.DebugMode = _options.debugMode;
            options.Compilation.VsVersion = VisualStudioVersion.Latest;
            options.GenerateDebugOutput = _options.outputDebugInfo;
            options.GeneratorKind = GeneratorKind.CSharp;
            options.CompileCode = _options.compileOutputCode;
            options.GenerateFinalizers = true;
            options.GenerationOutputMode = _options.generationOutputMode;
            options.Verbose = _options.verbose;
            options.GenerateClassTemplates = _options.generateClassTemplates;
            options.Compilation.Platform = TargetPlatform.Windows;
            var module = options.AddModule(_managedLibName);
            module.IncludeDirs.Add(_includeDir);
            module.Headers.Add(header);
            module.LibraryDirs.Add(_libraryDirs);
            module.Libraries.Add(sharedLib);
            options.OutputDir = _localOutputDir;
}

 public void SetupPasses(Driver driver)
  {
      driver.Options.MarshalCharAsManagedChar = true;
      driver.Context.TranslationUnitPasses.RenameDeclsUpperCase(RenameTargets.Any);
      driver.Context.TranslationUnitPasses.AddPass(new FunctionToInstanceMethodPass());
}

  public void Preprocess(Driver driver, ASTContext ctx)
    {
    }
    public void Postprocess(Driver driver, ASTContext ctx)
    {
    }

How to solve this problem?

cppsharp error

ehabawni commented 1 year ago

Hey @tritao , Any recommendations on this subject, please? I will be grateful for your assistance because we have placed this use of CppSharp on hold until we find a solution to this issue.