为什么访问非托管内存会导致System.AccessViolationException? [英] Why does accessing unmanaged memory cause a System.AccessViolationException?

查看:208
本文介绍了为什么访问非托管内存会导致System.AccessViolationException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 XGBoost的 dll(libxgboost.dll)创建一个DMatrix(其中就像一个二维数组),并得到它有多少列。它运行正常,直到在$ code> int cols = ... 行在下面的代码中抛出一个 System.AccessViolationException

I'm trying to use XGBoost's dll (libxgboost.dll) to create a DMatrix (which is like a 2D array) and get how many columns it has. It runs fine until it throws a System.AccessViolationException at the int cols = ... line in the code below:

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

Why does accessing unmanaged memory allocated with XGDMatrixNumCol(dmatrixPtr, dmatrixColumnsPtr) cause a System.AccessViolationException?

一种可能性是我使用pinvoke不正确的这些功能。以下是我使用的每个dll函数的定义:

One possibility might be that I'm using pinvoke incorrectly for these functions. Below are the definitions for each dll function I use:

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);

这里是我的项目的回购。我使用的是Visual Studio Enterprise 2015。它以Windows 10 Pro(64位)的Debug模式(针对x64)内置。可以找到x64二进制文件libxgboost.dll 这里。虽然链接的repo确实包含一个libxgboost.dll的副本。

Here is the repo for my project. I'm using Visual Studio Enterprise 2015 . It's built in "Debug" mode (targeting x64) on Windows 10 Pro (64-bit). x64 binaries for libxgboost.dll can be found here. Although the linked repo does contain a copy of 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);
    }
  }
}

这篇关于为什么访问非托管内存会导致System.AccessViolationException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆