I get "Heap has been corrupted" when I run the generated bindings of native C++ code built in Debug mode in .Net 6 project
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 25/100
Research direction
Start with the supplied Setup, SetupPasses, generated C# bindings, and the native DLL built in MSVC Debug mode; reproduce the failing function call in the .NET 6 project and compare it with the Release build. Done means isolating the cause of the heap corruption or access violation and documenting a verified fix or minimal reproduction.
Written by the indexing model from the issue text.
Description
# 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.
- When I compile the C++ native code in Release mode, the problem does not appear. Also, I'm using the default optimization and linker settings for the native project.
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?

- Dominant language
- C#
- Stars
- 3.4k
- Forks
- 541
- PR merge metrics
- No merged PRs in 30d
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from mono/CppSharp
-
Difficulty 4/5 3-5 days Newbie friendliness 35/100
-
Difficulty 4/5 3-5 days Newbie friendliness 35/100
-
Difficulty 4/5 3-5 days Newbie friendliness 45/100
-
How to get started Open
Difficulty 3/5 1-2 days Newbie friendliness 38/100
-
Difficulty 4/5 3-5 days Newbie friendliness 25/100
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
-
:watch: Not Triaged 11.0 fundamentals/subsvc
Difficulty 2/5 1-3 hours Newbie friendliness 92/100
dotnet/AspNetCore.Docs#37699 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
SubtitleEdit/subtitleedit#15108 · 1 comment ·
-
area/docs-content Bug pulumi/docs
Difficulty 1/5 1-3 hours Newbie friendliness 94/100
-
agentic-workflows untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 76/100