从 C# 线程调用非托管代码

发布于 2024-10-19 04:12:23 字数 1973 浏览 1 评论 0原文

我有一个非托管 C++ 库,我为其创建了一个托管 C++ 包装器。我现在尝试从 C# 调用它。到目前为止,一切都很好。但是,当我尝试从 C# 线程中调用相同的代码时,我从非托管代码中收到异常:

表达式:向量下标超出范围

这可能吗?我假设每个线程都会获得它自己的非托管类的实例?

我花了很长时间努力寻找有关从线程内调用非托管代码的更多信息,但至少可以说,信息似乎很少。

预先感谢您的任何帮助

C++ 包装器

// Managed wrapper
public ref class EllipseFit
{
   private:
       // Pointer to unmanaged class
   UnmanagedEllipseFit* _unmanagedEllipseFit;

   public:

       // Constructor & Destructor
   EllipseFit() 
   {
       _unmanagedEllipseFit = new UnmanagedEllipseFit();
       }

   ~EllipseFit() 
   { 
       delete _unmanagedEllipseFit; 
   }

       List<Ellipse^>^ ProcessImage(array<Byte>^ image, int width, int height)
       { 
           pin_ptr<unsigned char> pimg = &image[0];
       _unmanagedEllipseFit->processsImage(pimg, width, height); 

           // Marshal the results... <edited>
       return ellipses;
       }
};

C# 线程

    private void DcThread()
    {
        EllipseFit ellipseFit = new EllipseFit();

        string fullPath = _fileList.GetNext();
        while (fullPath != null)
        {
            // Load the image
            Bitmap bitmap = new Bitmap(fullPath);
            byte[] imageData = TsImage.ConvertBitmap(bitmap);

            // Process
            List<DcEllipse> ellipses = ellipseFit.ProcessImage(imageData, bitmap.Width, bitmap.Height);

            // Save the associated text file.. (Debug)
            TextWriter textFile = new StreamWriter(fullPath.Replace(".jpg", ".txt"));
            foreach (DcEllipse ellipse in ellipses)
                textFile.WriteLine(String.Format("{0} {1} {2} {3} {4}", ellipse.X, ellipse.Y, ellipse.MajorAxisLength, ellipse.MinorAxisLength, ellipse.Angle));
            textFile.Close();

            fullPath = _fileList.GetNext();
        }
    }

C# 线程启动

Thread t1 = new Thread(DcThread);
t1.Start();

I have an unmanaged C++ library for which I've created a managed C++ wrapper. I'm now trying to call this from C#. So far so good. However when I try to call the same code from within in a C# thread I get exceptions from within the unmanaged code:

Expression: vector subscript out of range

Is this even possible? I'm assuming each thread would get it's own instance of the unmanaged class?

I've searched long and hard for more information on calling unmanaged code from within threads but info seems sparce to say the least.

Thanks in advance for any help

C++ Wrapper

// Managed wrapper
public ref class EllipseFit
{
   private:
       // Pointer to unmanaged class
   UnmanagedEllipseFit* _unmanagedEllipseFit;

   public:

       // Constructor & Destructor
   EllipseFit() 
   {
       _unmanagedEllipseFit = new UnmanagedEllipseFit();
       }

   ~EllipseFit() 
   { 
       delete _unmanagedEllipseFit; 
   }

       List<Ellipse^>^ ProcessImage(array<Byte>^ image, int width, int height)
       { 
           pin_ptr<unsigned char> pimg = &image[0];
       _unmanagedEllipseFit->processsImage(pimg, width, height); 

           // Marshal the results... <edited>
       return ellipses;
       }
};

C# Thread

    private void DcThread()
    {
        EllipseFit ellipseFit = new EllipseFit();

        string fullPath = _fileList.GetNext();
        while (fullPath != null)
        {
            // Load the image
            Bitmap bitmap = new Bitmap(fullPath);
            byte[] imageData = TsImage.ConvertBitmap(bitmap);

            // Process
            List<DcEllipse> ellipses = ellipseFit.ProcessImage(imageData, bitmap.Width, bitmap.Height);

            // Save the associated text file.. (Debug)
            TextWriter textFile = new StreamWriter(fullPath.Replace(".jpg", ".txt"));
            foreach (DcEllipse ellipse in ellipses)
                textFile.WriteLine(String.Format("{0} {1} {2} {3} {4}", ellipse.X, ellipse.Y, ellipse.MajorAxisLength, ellipse.MinorAxisLength, ellipse.Angle));
            textFile.Close();

            fullPath = _fileList.GetNext();
        }
    }

C# Thread Start

Thread t1 = new Thread(DcThread);
t1.Start();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

只想待在家 2024-10-26 04:12:23

.NET 中的托管类型遵循相同的规则,无论它们是用 C# 还是 C++/CLI 编写的。

虽然可以为每个线程创建 C++/CLI 类的新实例,但如果您不告诉编译器您想要什么,它就不会自动发生。

编辑:查看代码,除了内存泄漏之外,我没有看到任何问题。 C++/CLI 类应该同时具有析构函数和终结器,如下所示:

!EllipseFit() 
{ 
    delete _unmanagedEllipseFit; 
    _unmanagedElipseFit = nullptr;
}


~EllipseFit() { this->!EllipseFit(); }

至于崩溃 - 也许非托管代码使用静态或全局变量,因此不能从多个线程同时使用。

Managed types in .NET follow the same rules, no matter whether they're written in C# or C++/CLI.

While it's possible to create a new instance of the C++/CLI class for each thread, it's not going to happen automatically without you telling the compiler that's what you want.

EDIT: Looking at the code, I don't see any problems apart from a memory leak. The C++/CLI class should have both a destructor and finalizer, like this:

!EllipseFit() 
{ 
    delete _unmanagedEllipseFit; 
    _unmanagedElipseFit = nullptr;
}


~EllipseFit() { this->!EllipseFit(); }

As for the crash -- perhaps the unmanaged code uses static or global variables, and thus can't be used concurrently from multiple threads.

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