System.AccessViolationException Attempted to read or write protected memory

Open
#1,841 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Needs clarification
Activity status
Stale
Tech stack
cpp, csharp
Domain
devtools

Research direction

Reproduce the crash with Program.cs and the PclIoLib configuration, then trace the reported path through CppSharp.ConsoleDriver.Run, Driver.ParseCode, and ClangParser.ParseHeader. Check the supplied pcl/io/pcd_io.h, pcl/memory.h, and pcl/type_traits.h inputs and their include and library directories. Done means parsing completes without AccessViolationException and generated bindings are produced.

Written by the indexing model from the issue text.

Description

Brief Description

I want to generate csharp bindings for pcl-1.14.0 .

follow the doc:

dotnet add package CppSharp --version 1.1.5.3168
    internal class PclIoLib : ILibrary
    {
        private string _includeDir;
        private string _libDir;
        private string _outputDir;

        public PclIoLib(string includeDir, string libDir, string outputDir)
        {
            _includeDir = includeDir;
            _libDir = libDir;
            _outputDir = outputDir;
        }

        public void Setup(Driver driver)
        {
            var pclRoot = Environment.GetEnvironmentVariable("PCL_ROOT");
            var boostIncludeRoot = Path.Combine(pclRoot!, "3rdParty", "Boost", "include", "boost-1_84");

            var options = driver.Options;
            options.GeneratorKind = GeneratorKind.CSharp;
            var module = options.AddModule("pcl_io");
            module.IncludeDirs.Add(_includeDir);
            module.IncludeDirs.Add(boostIncludeRoot);
            module.Headers.Add(@"pcl\io\pcd_io.h");
            module.Headers.Add(@"pcl\memory.h");
            module.Headers.Add(@"pcl\type_traits.h");
            module.LibraryDirs.Add(_libDir);
            module.Libraries.Add("pcl_io.lib");

            driver.Options.OutputDir = _outputDir;
        }

        public void SetupPasses(Driver driver)
        {
            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)
        {
        }
    }

And Program.cs as:

var pclRoot = Environment.GetEnvironmentVariable("PCL_ROOT");
var includeRoot = Path.Combine(pclRoot!, "include", "pcl-1.14");
var libRoot = Path.Combine(pclRoot!, "lib");
var outputDir = "C:\\Users\\perso\\source\\desktop-apps\\Pcl.Net\\src";

Console.WriteLine($"{nameof(pclRoot)}:{pclRoot}");
Console.WriteLine($"{nameof(includeRoot)}:{includeRoot}");
Console.WriteLine($"{nameof(libRoot)}:{libRoot}");

ConsoleDriver.Run(new PclIoLib(includeRoot, libRoot, outputDir));

Console.WriteLine("Ok, It's done!");

run and output:

pclRoot:C:\Program Files\PCL 1.14.0
includeRoot:C:\Program Files\PCL 1.14.0\include\pcl-1.14
libRoot:C:\Program Files\PCL 1.14.0\lib
Parsing libraries...
Parsed 'pcl_io.lib'
Parsing code...
Fatal error. System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Repeat 2 times:
--------------------------------
   at CppSharp.Parser.ClangParser+__Internal.ParseHeader(IntPtr)
--------------------------------
   at CppSharp.Parser.ClangParser.ParseHeader(CppSharp.Parser.CppParserOptions)
   at CppSharp.ClangParser.ParseSourceFiles(System.Collections.Generic.IEnumerable`1<System.String>, CppSharp.Parser.ParserOptions)
   at CppSharp.Driver.ParseCode()
   at CppSharp.ConsoleDriver.Run(CppSharp.ILibrary)
   at Program.<Main>$(System.String[])

How can I fix this?

Fatal error. System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

OS: Windows
IDE: VS2022

Used headers
Used settings

Target: MSVC

Stack trace or incompilable generated code
Dominant language
C#
Stars
3.4k
Forks
541
PR merge metrics
No merged PRs in 30d

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from mono/CppSharp

All issues in mono/CppSharp

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.