Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/136.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 调用c+;时访问冲突写入位置0x0000000C错误+;来自c的dll#_C#_C++_Swig - Fatal编程技术网

C# 调用c+;时访问冲突写入位置0x0000000C错误+;来自c的dll#

C# 调用c+;时访问冲突写入位置0x0000000C错误+;来自c的dll#,c#,c++,swig,C#,C++,Swig,我所做的: using System; namespace Test {     class Program     {         static void Main(string[] args)         {             Utility test1 = new Utility();             algobucket test2 = new algobucket();             Console.WriteLine("Algobucket Algo

我所做的

using System;
namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            Utility test1 = new Utility();
            algobucket test2 = new algobucket();

            Console.WriteLine("Algobucket Algorithm\n ");
            int k = 1;
            byte[] imageData1 = new byte[5000000];
            byte[] imageData2 = new byte[5000000];
            int[] target1 = new int[1000];

            while (k == 1)
            {
                int m;
               
                test1.CameraRead(imageData1);
                int a = test2.iristracker(imageData1, 640, 480, imageData2, target1, 1000);
                m = test1.ImageShow(imageData2);

                if (m == 1)
                {
                    Console.WriteLine("Stopped");
                    break;                    
                }
            }

            Console.WriteLine("Exited");
        }
    }
}
我从C++中调用了一个C++ DLL。它工作正常,但当我退出该进程时,它显示以下错误

停止工作

  • 当我在VisualStudio中调试代码时,它会显示

    Test.exe中0x773C77A2(ntdll.dll)处的未处理异常:0xC0000005:访问冲突写入位置0x0000000C

  • 我在C#中使用的代码:

    using System;
    namespace Test
    {
        class Program
        {
            static void Main(string[] args)
            {
                Utility test1 = new Utility();
                algobucket test2 = new algobucket();
    
                Console.WriteLine("Algobucket Algorithm\n ");
                int k = 1;
                byte[] imageData1 = new byte[5000000];
                byte[] imageData2 = new byte[5000000];
                int[] target1 = new int[1000];
    
                while (k == 1)
                {
                    int m;
                   
                    test1.CameraRead(imageData1);
                    int a = test2.iristracker(imageData1, 640, 480, imageData2, target1, 1000);
                    m = test1.ImageShow(imageData2);
    
                    if (m == 1)
                    {
                        Console.WriteLine("Stopped");
                        break;                    
                    }
                }
    
                Console.WriteLine("Exited");
            }
        }
    }
    
    在上面的代码中,dll在以下行中调用

                    test1.CameraRead(imageData1);
                    int a = test2.iristracker(imageData1, 640, 480, imageData2, target1, 1000);
                    m = test1.ImageShow(imageData2);
    
    CameraRead()和ImageShow()是从一个dll调用的,iristracker()是从另一个dll调用的

    实用程序.h

    #ifndef UTILITY_H
    #define UTILITY_H
    
    class Utility
    {
    
    public:
     int CameraRead(unsigned char* Output);
     int ImageShow(unsigned char* Input);
    int DrawRectOnImage(unsigned char* image,int* rect,int n);
    int DrawPointOnImage(unsigned char* image,int* rect,int n);
    };
    
    #endif  /* UTILITY_H */
    
    Iristracker.hpp

    #ifndef IRISTRACKER_HPP
    #define IRISTRACKER_HPP
    #include <iostream>
    class algobucket 
    {
    
    public:
    
    
     int iristracker(unsigned char* input,int width,int height,unsigned char* Output,int* output,int n,std::string xml_path="");  
    
    };
    
    #endif  /* IRISTRACKER_HPP */
    
    Swig接口Iris.i

    %module testing
    
    %{
    #include "Utility.h"
    %}
    %include "arrays_csharp.i"
    
      %apply unsigned char OUTPUT[] { unsigned char* Output}
       %apply unsigned char INPUT[] { unsigned char* Input}
       %apply unsigned char INPUT[] { unsigned char* image}
      %apply int INPUT[] { int* rect}
     %include "Utility.h"
    
    %module algo
    
    %{
    #include "IrisTracker.hpp"
    %}
    %include "arrays_csharp.i"
    %include "std_string.i"
      %apply unsigned char INPUT[] { unsigned char* input}
      %apply unsigned char OUTPUT[] { unsigned char* Output}
      %apply int OUTPUT[] { int* output}
     %include "IrisTracker.hpp"
    
    这里遗漏了上述swig接口文件内存处理的任何类型映射


    如何解决此问题?

    此问题可能存在于此处未提供的代码中。dll是如何导入的?C++代码看起来像什么?你确定这些数组足够吗?您需要将其减少到一个最小值。错误消息是典型的缓冲区溢出(可能在某些字符串中)。此外,请检查导入函数的调用约定。C++默认值>代码> CDDEL< /COD> vs.什么.NET作为默认值<代码> STDCALL> /COD>。堆栈中的不匹配会导致问题。@尼尔我使用使用swig生成的包装文件导入了dll。@CibinWilliam您没有提到哪一行调用了dll。第二,如果客户端必须猜测要分配多少字节,并祈祷分配足够,那么这是一个糟糕的设计。通常的做法是DLL创建内存,并返回内存和创建的大小。否则,客户端将提供最大大小作为参数。