尝试在C#中读取共享内存时出现堆栈溢出错误

时间:2016-06-06 17:13:27

标签: c# stack-overflow shared-memory

以下代码产生堆栈溢出错误。它创建共享内存空间,然后尝试将共享内存内容复制到本地缓冲区。我已经编写了几个程序来在非托管C ++中执行此操作,但C#对我来说很陌生...我已经在堆上分配了一个缓冲区并且我正在尝试将共享内存缓冲区复制到我的本地缓冲区中。这是堆栈溢出错误触发的位置:accessor.Read<my_struct>(0, out ps.hi);。也许accessor.Read函数在将其复制到我提供的引用之前尝试创建共享内存缓冲区的本地副本?如果是这样,在C#中将大内存块传入和传出共享内存的推荐方法是什么?我在互联网搜索中没有发现这个问题,所以任何帮助都会受到赞赏......

确切的错误消息显示:“mscorlib.dll中发生了'System.StackOverflowException'类型的未处理异常”

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.IO.MemoryMappedFiles;
using System.Threading;
namespace ConsoleApplication2
{
unsafe struct my_struct
{
    public fixed UInt16 img[1280 * 960];
}
class Program
{
    my_struct hi;
    static void Main(string[] args)
    {
        Program ps = new Program();
        ps.hi = new my_struct();

        using (var mmf = MemoryMappedFile.CreateOrOpen("OL_SharedMemSpace", System.Runtime.InteropServices.Marshal.SizeOf(ps.hi)))
        {
            using (var accessor = mmf.CreateViewAccessor())
            {
                //Listen to event...
                EventWaitHandle request_event;
                EventWaitHandle ready_event;
                try
                {
                    request_event = EventWaitHandle.OpenExisting("OL_ReceiveEvent");
                }
                catch (WaitHandleCannotBeOpenedException)
                {
                    Console.WriteLine("Receive event does not exist...creating one.");
                    request_event = new EventWaitHandle(false, EventResetMode.AutoReset, "OL_ReceiveEvent");
                }
                try
                {
                    ready_event = EventWaitHandle.OpenExisting("OL_ReadyEvent");
                }
                catch (WaitHandleCannotBeOpenedException)
                {
                    Console.WriteLine("Ready event does not exist...creating one.");
                    ready_event = new EventWaitHandle(false, EventResetMode.AutoReset, "OL_ReceiveEvent");
                }
                System.Console.WriteLine("Ok...ready for commands...");
                while (true)
                {
                    accessor.Read<my_struct>(0, out ps.hi);
                    request_event.WaitOne();
                }
            }
        }
    }
}

}

2 个答案:

答案 0 :(得分:2)

也许它会有所帮助。这是C ++本机编写器:

#include <windows.h>
#include <memory>

const int BUFFER_SiZE = sizeof(uint8_t) * 1024 * 1024;
const wchar_t MMF_NAME[] = L"Global\\SharedMemoryExample";

int main()
{
    wprintf(L"Shared Memory example. Native writer\r\n"); 
    wprintf(L"BUFFER_SIZE: %d bytes\r\n", BUFFER_SiZE);
    wprintf(L"MMF name: %s\r\n", MMF_NAME);

    HANDLE hMapFile = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, BUFFER_SiZE, MMF_NAME);
    if (hMapFile == NULL)
    {
        wprintf(L"CreateFileMapping failed with error: %d", GetLastError());
        return -1;
    }
    std::shared_ptr<void> mapHandleGuard(hMapFile, &::CloseHandle);
    uint8_t* pBuffer = (uint8_t*)MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, BUFFER_SiZE);
    if (pBuffer == NULL)
    {
        wprintf(L"MapViewOfFile failed with error: %d", GetLastError());
        return -2;
    }
    std::shared_ptr<void> bufferGuard(pBuffer, &::UnmapViewOfFile);

    wprintf(L"Press 'Enter' to write some data to shared memory");
    getchar();

    // Write magic data :)
    memset(pBuffer, 0xFA, BUFFER_SiZE);

    wprintf(L"Press 'Enter' close application");
    getchar();

    return 0;
}

这是.NET阅读器:

class Program
    {
        private const string MMF_NAME = "Global\\SharedMemoryExample";
        const int BUFFER_SIZE = sizeof(byte) * 1024 * 1024;

        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("Shared Memory example. .NET reader");
                Console.WriteLine("BUFFER_SIZE: {0} bytes", BUFFER_SIZE);
                Console.WriteLine("MMF name: {0}", MMF_NAME);

                Console.WriteLine("Press 'Enter' to open Shared memory");
                Console.ReadLine();

                using (var mmf = System.IO.MemoryMappedFiles.MemoryMappedFile.OpenExisting(MMF_NAME))
                {
                    Console.WriteLine("{0} was opened.", MMF_NAME);

                    using (var accessor = mmf.CreateViewAccessor())
                    {
                        Console.WriteLine("ViewAccessor was created.");

                        byte[] buffer = new byte[BUFFER_SIZE];

                        Console.WriteLine("Press 'Enter' to read from Shared memory");
                        Console.ReadLine();

                        int cntRead = accessor.ReadArray(0, buffer, 0, BUFFER_SIZE);
                        Console.WriteLine("Read {0} bytes", cntRead);

                        if (IsBufferOk(buffer, cntRead))
                            Console.WriteLine("Buffer is ok");
                        else
                            Console.WriteLine("Buffer is bad!");
                    }
                }
            }
            catch(Exception  ex)
            {
                Console.WriteLine("Got exception: {0}", ex);
                Console.ReadLine();
            }
        }
        private static bool IsBufferOk(byte[] buffer, int length)
        {
            for(int i = 0; i < length; i++)
            {
                if (buffer[i] != 0XFA)
                    return false;
            }
            return true;
        }
    }

为了简化我使用的控制台输入进行同步。此外,如果您的操作系统&gt; = Windows Vista,则应在elevated command prompt下运行这些应用程序。

答案 1 :(得分:1)

您的ps.hi = new my_struct();没有将数据放在堆上。

C#struct总是一个值类型,在这种情况下是一个非常大的类型。太大了 在2 * 1280 * 960时,它将超出默认的1MB堆栈。

我不熟悉MMF API,但看起来您应该能够将其作为uint[]数组读取而不包含周围的结构。