从托管C#释放非托管内存的指针它

从托管C#释放非托管内存的指针它

问题描述:

在简短的词汇的问题是:
如何释放内存从本地DLL在托管代码返回ItrPtr

The question in short words is : How to free memory returned from Native DLL as ItrPtr in managed code?

详细信息:
假设我们有简单的函数有两个参数为OUTPUT,第一种是引用指针到字节数组,第二个是参考诠释。
的功能将分配根据一些规则的字节数量,并返回存储器的指针和字节的大小和(成功1和0 FAIL)的返回值。

Details : Assume we have simple function takes two parameters as OUTPUT, The first one is Reference Pointer to byte array and the second one is Reference Int . The function will allocate amount of bytes based on some rules and return the pointer of memory and the size of bytes and the return value (1 for success and 0 for fail) .

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

The code below works fine and I can get the byte array correctly and the count of bytes and the return value, but when I try to free the memory using the pointer (IntPtr) I get exception :

Windows已经引发了TestCppDllCall.exe一个断点。

Windows has triggered a breakpoint in TestCppDllCall.exe.

这可能是由于腐败堆,这表明在TestCppDllCall.exe或已加载的DLL的错误。

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.

这也可能是由于用户按F12,而TestCppDllCall.exe具有焦点。

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

输出窗口。可能有更多的诊断信息。

The output window may have more diagnostic information.

为了让事情变得清楚:


  1. 与其他DLL函数接下来的C#代码正常工作具有相同的签名和释放内存没有任何问题的作品。

  1. The next C# code work correctly with other DLL function have the same signature and freeing the memory works without any problem .

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

Any modification in (C) code accepted if you need to change allocation memory method or adding any other code .

我所需要的功能是本机DLL函数接受双参数通过引用(Byte数组和INT,在C#[字节数组和整型的IntPtr])与基于某些规则的一些值填充它们,并返回函数的结果(成功或失败)。

All the functionality I need is Native DLL function accept Two Parameter by reference (Byte array and int , In c# [IntPtr of byte array and int]) fill them with some values based on some rules and return the function result (Success or Fail) .

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)和缓冲区的设置值破发点是:

When I debug this code i set break point in the line (return 1) and the value of the buffer was :

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

和我在C#代码相同的值当函数调用返回和值:

And I got the same value in C# code when the function call return and the value was :

52121536

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

The result I Got the correct Memory pointer and i am able to get the byte array value , how to free these memory blocks with this pointer in C# ?

请让我知道如果有什么不明确或有任何错字,我不是英语发音。

Please let me know if there anything is not clear or if there any typo, I am not native English speaker .

简短的回答:你应该在释放你的记忆中DLL添加一个单独的方法

Short answer: you should add a separate method in the DLL that frees the memory for you.

龙答:有以不同的方式该内存可以在你的DLL实现内部进行分配。你释放内存的方式必须以你所分配的内存的方法相匹配。例如,内存分配新[] (方括号)需要用释放删除[] (如反对删除免费)。 C#不提供一种机制,让你做到这一点;您需要将指针返回给C ++。

Long answer: there are different ways in which the memory can be allocated inside your DLL implementation. The way you free the memory must match the way in which you have allocated the memory. For example, memory allocated with new[] (with square brackets) needs to be freed with delete[] (as opposed to delete or free). C# does not provide a mechanism for you to do it; you need to send the pointer back to C++.

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