尝试在 C# 中读取共享内存时发生堆栈溢出错误
Stack overflow error when attempting to read shared memory in C#
以下代码产生堆栈溢出错误。它创建一个共享内存 space 然后尝试将共享内存内容复制到本地缓冲区。我已经用非托管 C++ 编写了几个程序来执行此操作,但 C# 对我来说很陌生……我在堆上分配了一个缓冲区,并试图将共享内存缓冲区复制到我的本地缓冲区中。这是堆栈溢出错误触发的地方:accessor.Read<my_struct>(0, out ps.hi);
。也许 accessor.Read
函数在将共享内存缓冲区复制到我提供的引用之前尝试创建共享内存缓冲区的本地副本?如果是这样,在 C# 中将大内存块传入和传出共享内存的推荐方法是什么?我在 Internet 搜索中没有发现此问题,因此将不胜感激...
确切的错误消息是:"An unhandled exception of type 'System.WhosebugException' occurred in mscorlib.dll"
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();
}
}
}
}
}
}
您的 ps.hi = new my_struct();
没有将数据放在堆上。
C# 结构始终是值类型,在本例中是一个非常大的值类型。太大了。
在 2*1280*960 它将超出默认的 1MB 堆栈。
我对 MMF API 不熟悉,但看起来您应该能够将其作为没有周围结构的 uint[]
数组来读取。
也许它会有所帮助。这是 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 reader:
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;
}
}
为了简化,我使用控制台输入进行同步。此外,如果您的 OS >= Windows Vista,您应该 运行 这些应用程序位于 elevated command prompt。
以下代码产生堆栈溢出错误。它创建一个共享内存 space 然后尝试将共享内存内容复制到本地缓冲区。我已经用非托管 C++ 编写了几个程序来执行此操作,但 C# 对我来说很陌生……我在堆上分配了一个缓冲区,并试图将共享内存缓冲区复制到我的本地缓冲区中。这是堆栈溢出错误触发的地方:accessor.Read<my_struct>(0, out ps.hi);
。也许 accessor.Read
函数在将共享内存缓冲区复制到我提供的引用之前尝试创建共享内存缓冲区的本地副本?如果是这样,在 C# 中将大内存块传入和传出共享内存的推荐方法是什么?我在 Internet 搜索中没有发现此问题,因此将不胜感激...
确切的错误消息是:"An unhandled exception of type 'System.WhosebugException' occurred in mscorlib.dll"
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();
}
}
}
}
}
}
您的 ps.hi = new my_struct();
没有将数据放在堆上。
C# 结构始终是值类型,在本例中是一个非常大的值类型。太大了。
在 2*1280*960 它将超出默认的 1MB 堆栈。
我对 MMF API 不熟悉,但看起来您应该能够将其作为没有周围结构的 uint[]
数组来读取。
也许它会有所帮助。这是 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 reader:
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;
}
}
为了简化,我使用控制台输入进行同步。此外,如果您的 OS >= Windows Vista,您应该 运行 这些应用程序位于 elevated command prompt。