[英]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.