gpt4 book ai didi

c# - 使用它的指针从托管 C# 中释放非托管内存

转载 作者:可可西里 更新时间:2023-11-01 15:38:36 25 4
gpt4 key购买 nike

简而言之,问题是:如何释放从 native DLL 返回的内存作为托管代码中的 ItrPtr?

详细信息:假设我们有一个简单的函数,将两个参数作为输出,第一个是指向字节数组的引用指针,第二个是 Reference Int 。该函数将根据一些规则分配字节数,并返回内存指针和字节大小以及返回值(1 表示成功,0 表示失败)。

下面的代码工作正常,我可以正确获取字节数组以及字节数和返回值,但是当我尝试使用指针 (IntPtr) 释放内存时,出现异常:

Windows has triggered a breakpoint in TestCppDllCall.exe.

This may be due to a corruption of the heap, which indicates a bug in TestCppDllCall.exe or any of the DLLs it has loaded.

This may also be due to the user pressing F12 while TestCppDllCall.exe has focus.

The output window may have more diagnostic information.

把事情说清楚:

  1. 下一个 C# 代码可以与具有相同签名的其他 DLL 函数一起正常工作,并且可以毫无问题地释放内存。

  2. 如果您需要更改分配内存方法或添加任何其他代码,则接受 (C) 代码中的任何修改。

  3. 我需要的所有功能是 native DLL 函数通过引用接受两个参数(字节数组和 int,在 c# 中 [字节数组和 int 的 IntPtr])根据某些规则用一些值填充它们并返回函数结果(成功或失败)。


CppDll.h

#ifdef CPPDLL_EXPORTS
#define CPPDLL_API __declspec(dllexport)
#else
#define CPPDLL_API __declspec(dllimport)
#endif

extern "C" CPPDLL_API int writeToBuffer(unsigned char *&myBuffer, int& mySize);

CppDll.cpp

#include "stdafx.h"
#include "CppDll.h"

extern "C" CPPDLL_API int writeToBuffer(unsigned char*& myBuffer, int& mySize)
{
mySize = 26;

unsigned char* pTemp = new unsigned char[26];
for(int i = 0; i < 26; i++)
{
pTemp[i] = 65 + i;
}
myBuffer = pTemp;
return 1;
}

C# 代码:

using System;
using System.Text;
using System.Runtime.InteropServices;

namespace TestCppDllCall
{
class Program
{
const string KERNEL32 = @"kernel32.dll";
const string _dllLocation = @"D:\CppDll\Bin\CppDll.dll";
const string funEntryPoint = @"writeToBuffer";

[DllImport(KERNEL32, SetLastError = true)]
public static extern IntPtr GetProcessHeap();
[DllImport(KERNEL32, SetLastError = true)]
public static extern bool HeapFree(IntPtr hHeap, uint dwFlags, IntPtr lpMem);
[DllImport(_dllLocation, EntryPoint = funEntryPoint, CallingConvention = CallingConvention.Cdecl)]
public static extern int writeToBuffer(out IntPtr myBuffer, out int mySize);

static void Main(string[] args)
{
IntPtr byteArrayPointer = IntPtr.Zero;
int arraySize;
try
{
int retValue = writeToBuffer(out byteArrayPointer, out arraySize);
if (retValue == 1 && byteArrayPointer != IntPtr.Zero)
{
byte[] byteArrayBuffer = new byte[arraySize];
Marshal.Copy(byteArrayPointer, byteArrayBuffer, 0, byteArrayBuffer.Length);
string strMyBuffer = Encoding.Default.GetString(byteArrayBuffer);
Console.WriteLine("Return Value : {0}\r\nArray Size : {1}\r\nReturn String : {2}",
retValue, arraySize, strMyBuffer);
}
}
catch (Exception ex)
{
Console.WriteLine("Error calling DLL \r\n {0}", ex.Message);
}
finally
{
if (byteArrayPointer != IntPtr.Zero)
HeapFree(GetProcessHeap(), 0, byteArrayPointer);
}
Console.ReadKey();
}
}
}

当我调试这段代码时,我在行中设置了断点(返回 1)并且缓冲区的值为:

myBuffer = 0x031b4fc0 "ABCDEFGHIJKLMNOPQRSTUVWXYZ‎‎‎‎««««««««î‏"

当函数调用返回时,我在 C# 代码中得到了相同的值:

52121536

结果我得到了正确的内存指针并且我能够获得字节数组值,如何在 C# 中使用这个指针释放这些内存块?

如果有什么不清楚或有错别字,请告诉我,我的母语不是英语。

最佳答案

简短的回答:您应该在 DLL 中添加一个单独的方法来为您释放内存。

长答案:在您的 DLL 实现中可以通过不同的方式分配内存。释放内存的方式必须与分配内存的方式相匹配。例如,使用 new[](带方括号)分配的内存需要使用 delete[](与 delete免费)。 C# 没有为您提供执行此操作的机制;您需要将指针发送回 C++。

extern "C" CPPDLL_API void freeBuffer(unsigned char* myBuffer) {
delete[] myBuffer;
}

关于c# - 使用它的指针从托管 C# 中释放非托管内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12273961/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com