为什么访问非托管内存会导致System.AccessViolationException
Why does accessing unmanaged memory cause a System.AccessViolationException?
我试图使用XGBoost的dll (libxgboost.dll)来创建一个DMatrix(这就像一个2D数组),并得到它有多少列。它可以正常运行,直到它在下面的代码中在int cols = ...
行抛出一个System.AccessViolationException
:
using System;
using System.Runtime.InteropServices;
namespace basicXgboost
{
class Program
{
[DllImport("../../libs/libxgboost.dll", CharSet = CharSet.Auto)]
public static extern int XGDMatrixCreateFromFile([MarshalAs(UnmanagedType.LPStr)] string file, int silent, IntPtr outputPtr);
[DllImport("../../libs/libxgboost.dll", CharSet = CharSet.Auto)]
public static extern int XGDMatrixNumCol(IntPtr dmatrixPtr, IntPtr dmatrixColumnsPtr);
static void Main(string[] args)
{
IntPtr dmatrixPtr = Marshal.AllocHGlobal(1000000);
IntPtr dmatrixColumnsPtr = Marshal.AllocHGlobal(10);
int result = XGDMatrixCreateFromFile("../../libs/test.txt", 0, dmatrixPtr);
int cols = XGDMatrixNumCol(dmatrixPtr, dmatrixColumnsPtr);
Marshal.FreeHGlobal(dmatrixPtr);
Marshal.FreeHGlobal(dmatrixColumnsPtr);
}
}
}
为什么访问XGDMatrixNumCol(dmatrixPtr, dmatrixColumnsPtr)
分配的非托管内存会导致System.AccessViolationException
?
一种可能是我对这些函数使用了错误的pinvoke。下面是我使用的每个dll函数的定义:
XGDMatrixCreateFromFile ()
/*!
* brief load a data matrix
* param fname the name of the file
* param silent whether print messages during loading
* param out a loaded data matrix
* return 0 when success, -1 when failure happens
*/
XGB_DLL int XGDMatrixCreateFromFile(const char *fname,
int silent,
DMatrixHandle *out);
XGDMatrixNumCol ()
/*!
* brief get number of columns
* param handle the handle to the DMatrix
* param out The output of number of columns
* return 0 when success, -1 when failure happens
*/
XGB_DLL int XGDMatrixNumCol(DMatrixHandle handle,
bst_ulong *out);
这是我的项目的repo。我使用的是Visual Studio Enterprise 2015。它在Windows 10 Pro(64位)上内置在"调试"模式(针对x64)。libxgboost.dll的X64二进制文件可以在这里找到。尽管链接的repo确实包含libxgboost.dll的副本。
感谢NineBerry的回答,这是我得到的解决方案。
using System;
using System.Runtime.InteropServices;
namespace basicXgboost
{
class Program
{
[DllImport("../../libs/libxgboost.dll", CharSet = CharSet.Auto)]
public static extern int XGDMatrixCreateFromFile([MarshalAs(UnmanagedType.LPStr)] string file, int silent, out IntPtr outputPtr);
[DllImport("../../libs/libxgboost.dll", CharSet = CharSet.Auto)]
public static extern int XGDMatrixNumCol(IntPtr dmatrixPtr, out ulong dmatrixColumnsPtr);
static void Main(string[] args)
{
IntPtr dmatrixPtr;
ulong dmatrixColumns;
int result = XGDMatrixCreateFromFile("../../libs/test.txt", 0, out dmatrixPtr);
int cols = XGDMatrixNumCol(dmatrixPtr, out dmatrixColumns);
}
}
}
相关文章:
- 如何在c++中使用system()来运行包含空格的python脚本
- System.InvalidCastException - SQL to C++ - safe_cast<float>
- C++/CLI System.AccessViolation在托管类中调用非托管函数时出现异常
- 来自 Android 应用程序内部的 boost 类型的 boost::wrapexcept<boost::system::system_error> 的未捕获异常
- 程序在使用 system() 启动另一个可执行文件时停止
- 为什么我的 DeviceInformation 对象没有 System.Devices.InterfaceClassGuid 属性?
- 为什么"using System;"不被视为不良做法?
- C++ 合并字符串以'system'函数错误
- System.AccessViolationException:shared_ptr C# .NET 和 C++ 应用程
- 将 Vcl::Controls::TCaption aka (System::UnicodeString) 转换为 co
- 对 boost::system::d etail::system_category_instance 的未定义引用,从
- Python os.system() 返回错误值
- 使用 system() 函数在C++程序中运行 cmd 命令
- 如何使用 C/C++ 和 system() 系统调用以外的其他方法在 Linux 中获取文件功能?
- 在C++中,如何在第一个"system()"结束后执行第二个"system()"?
- system() 无法运行我的程序,因为空间
- How to recover system gcc compiler on centos 6
- 导出 c++ 函数并使用 c# System.AccessViolationException 中的函数
- g++ 中的 system() 函数
- System Verilog DPI - 在 cpp 中运行并行线程,在 SV 中运行另一个并行线程