尝试在C#中读取共享内存时出现堆栈溢出错误[关闭](Stack overflow error when attempting to read shared memory in C# [closed])

编程入门 行业动态 更新时间:2024-10-23 09:40:27
尝试在C#中读取共享内存时出现堆栈溢出错误[关闭](Stack overflow error when attempting to read shared memory in C# [closed])

以下代码产生堆栈溢出错误。 它创建一个共享内存空间,然后尝试将共享内存内容复制到本地缓冲区。 我已经编写了几个程序来在非托管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(); } } } } }

}

The following code produces a stack overflow error. It creates a shared memory space and then attempts to copy the shared memory contents to a local buffer. I have written several programs to do this in unmanaged C++, but C# is foreign to me... I have allocated a buffer on the heap and am attempting to copy the shared memory buffer into my local buffer. This is where the stack overflow error triggers: accessor.Read<my_struct>(0, out ps.hi);. Perhaps the accessor.Read function attempts to create a local copy of the shared memory buffer before copying it into the reference I provide it? If so, what is the recommended way to transfer large memory chunks to and from shared memory in C#? I have not found this issue in my internet searches so any help would be appreciated...

The exact error message reads: "An unhandled exception of type 'System.StackOverflowException' 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(); 没有将数据放在堆上。

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

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

Your ps.hi = new my_struct(); is not placing the data on the heap.

A C# struct is always a value type, in this case a very big one. Too big. At 2*1280*960 it will overrun the default 1MB stack.

I'm not familiar with the MMF API but it looks like you should be able to read it as a uint[] array without the surrounding struct.

更多推荐

本文发布于:2023-07-31 20:35:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1347361.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:堆栈   内存   错误   overflow   Stack

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!