将非托管数组传递给托管线程。 - 内存损坏

发布于 2024-10-05 11:21:01 字数 1217 浏览 5 评论 0原文

我是 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

残疾 2024-10-12 11:21:01
dt = new int(sz);

这是分配一个单个整数(不是数组),并使用sz值(100)对其进行初始化。您想要的是这样的:

dt = new int[sz];

这会分配一个大小为 dt数组。请注意,为了避免内存泄漏,您必须稍后像这样释放它:

delete [] dt;
dt = new int(sz);

This is allocating a single integer, (not an array), and initializing it with the value of sz (100). What you want is this:

dt = new int[sz];

This allocates an array of size dt. Note that in order to avoid leaking memory, you must later free it like this:

delete [] dt;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文