简体   繁体   English

试图删除 c++ dll 中的 c# 数组。 获取 System.AccessViolationException

[英]Trying to delete c# array in c++ dll. Getting System.AccessViolationException

I've been working with a custom C++ DLL and am having issues with deleting an array in C++ that was passed from C# using [DLLImport].我一直在使用自定义 C++ DLL,但在删除使用 [DLLImport] 从 C# 传递的 C++ 数组时遇到问题。 When it's running its giving me the exception System.AccessViolationException.当它运行时,它给我异常 System.AccessViolationException。

Any help is greatly appreciated.任何帮助是极大的赞赏。

__declspec(dllexport) void empty(Game* x[], int y)
    {
        try
        {
            delete x[y];
        }
        catch (const std::exception& exc)
        {
            // catch anything thrown within try block that derives from std::exception
            ExceptionOutput << exc.what();
        }
    }

In this case C++ and C# code would have different heaps and it is not possible to allocate data in one part and deallocate in another.在这种情况下,C++ 和 C# 代码将具有不同的堆,并且不可能在一个部分分配数据并在另一部分取消分配。 If the array was created in C# code it should be freed in C# code - basically C# code owns that data and it in charge of reallocating that.如果数组是在 C# 代码中创建的,它应该在 C# 代码中被释放——基本上 C# 代码拥有这些数据,它负责重新分配它。

If in your case C++ code knows better when to deallocate the memory it worth creating a function in C# code and export it to C++ code which would call it when it is needed.如果在您的情况下,C++ 代码更清楚何时释放内存,那么值得在 C# 代码中创建一个函数并将其导出到 C++ 代码,以便在需要时调用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将数组移交给 C# 中动态加载的 C++ DLL 时出现 System.AccessViolationException - System.AccessViolationException when handing an array over to a dynamically loaded c++ DLL in C# Visual C#在包装的C ++ \\ CLI dll中调用Lapack时抛出System.AccessViolationException - System.AccessViolationException thrown by Visual C# on call to Lapack inside wrapped C++\CLI dll 异常信息:调用 C++ Dll 时出现 System.AccessViolationException - Exception Info: System.AccessViolationException when calling C++ Dll 调用 C++ dll 时出现 System.AccessViolationException - System.AccessViolationException when calling C++ dll C#Offreg.dll&#39;System.AccessViolationException&#39;ORGetValue - C# Offreg.dll 'System.AccessViolationException' ORGetValue Delphi dll导入到C#-System.AccessViolationException DllImport - Delphi dll importing to C# - System.AccessViolationException DllImport System.AccessViolationException dll C#HANTEK SDK - System.AccessViolationException dll C# HANTEK SDK System.AccessViolationException从C#调用C ++函数 - System.AccessViolationException calling c++ function from C# 从C#调用C ++时出现System.AccessViolationException - System.AccessViolationException when calling C++ from C# 使用RightFax在C#中获取&#39;System.AccessViolationException&#39;异常 - Getting a 'System.AccessViolationException' exception in C# using RightFax
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM