将非托管数组传递给托管线程。 - 内存损坏
我是 .Net 的新手,正在尝试使用托管线程。 我在代码中找不到任何问题,但当线程结束时它会触发异常。 像这样的东西: 0x5cbf80ea 处未处理的异常 (msvcr90d.dll) 0xC0000005:读取位置 0x000000d7 时发生访问冲突。
#include "stdafx.h"
using namespace System;
using namespace System::Threading;
#define sz 100
//int dt[sz]; //allcating a gloal buffer
int *dt;
void workerThread (void)
{
Console::WriteLine("Producer Thread Started!");
int data = 50;
for(int i=0; i<sz; i++)
{
Thread::Sleep(1);
dt[i] = i;
Console::WriteLine("Producer W={0}", i);
};
Console::WriteLine("Producer Thread Ending");
}
int main(array<System::String ^> ^args)
{
Console::WriteLine("This is a test on global variable and threading");
//allcating a buffer
dt = new int(sz);
Thread ^wthrd = gcnew Thread(gcnew ThreadStart(&workerThread));
//Starting Worker Thread..
wthrd->Start();
//Waiting for Worker Thread to end.
wthrd->Join();
Console::WriteLine("Worker Thread Ended.");
Console::ReadKey();
return 0;
}
但是,当我将缓冲区分配为全局数组时,它工作正常。当我使用“new”关键字时,就会出现此异常,因此是动态内存分配。 我犯了什么根本性错误吗? 这是要和垃圾收集器打交道的吗?或者由“new”关键字分配的非托管堆? 我真的更喜欢在非托管堆中拥有这个缓冲区。虽然我正在编写托管代码,但我使用的许多其他 DLL 都是非托管的。
I'm a newbie for .Net and trying to use managed threading.
I couldn't find any problem in my code, but it triggers an exception when the Thread Ends.
Something like:
Unhandled exception at 0x5cbf80ea (msvcr90d.dll)
0xC0000005: Access violation reading location 0x000000d7.
#include "stdafx.h"
using namespace System;
using namespace System::Threading;
#define sz 100
//int dt[sz]; //allcating a gloal buffer
int *dt;
void workerThread (void)
{
Console::WriteLine("Producer Thread Started!");
int data = 50;
for(int i=0; i<sz; i++)
{
Thread::Sleep(1);
dt[i] = i;
Console::WriteLine("Producer W={0}", i);
};
Console::WriteLine("Producer Thread Ending");
}
int main(array<System::String ^> ^args)
{
Console::WriteLine("This is a test on global variable and threading");
//allcating a buffer
dt = new int(sz);
Thread ^wthrd = gcnew Thread(gcnew ThreadStart(&workerThread));
//Starting Worker Thread..
wthrd->Start();
//Waiting for Worker Thread to end.
wthrd->Join();
Console::WriteLine("Worker Thread Ended.");
Console::ReadKey();
return 0;
}
However it works fine when I allocate the buffer as a global array. This exception kicks in when I use "new" keyword, hence a dynamic memory allocation.
Am I making any fundamental mistakes?
Is this something to deal with the garbage collector? or Unmanaged heap allocated by the "new" keyword?
I would really prefer to have this buffer in unmanaged heap. Although I am writing a managed code, many other DLLs I am using are unmanaged.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是分配一个单个整数(不是数组),并使用
sz
值(100)对其进行初始化。您想要的是这样的:这会分配一个大小为
dt
的数组。请注意,为了避免内存泄漏,您必须稍后像这样释放它:This is allocating a single integer, (not an array), and initializing it with the value of
sz
(100). What you want is this:This allocates an array of size
dt
. Note that in order to avoid leaking memory, you must later free it like this: