C# 调用C++的问题

C++ 的接口


```c++
typedef enum {
    YT_IMG_BGR_8UC3,
    YT_IMG_RGB_8UC3,
    YT_IMG_GRAY_8UC1,
    YT_IMG_DEPTH_16UC1,
} yt_img_type;

typedef struct yt_image_t {
    unsigned char *data;
    int width;
    int height;
    yt_img_type type;
} yt_image;

typedef struct yt_rect_t {
    int x;
    int y;
    int width;
    int height;
} yt_rect;

typedef struct yt_pointf_t {
    float x;
    float y;
} yt_pointf;
/**
 * @brief 追踪接口,适合于视频流检测,内部封装了检测、5点配准、稳定模块,具有内部状态
 *
 * @param[in] handle 实例句柄
 * @param[in] image 图片,图片类型:YT_IMG_BGR_8UC3
 * @param[out] tracked_faces 人脸追踪结果
 * @param[out] tracked_faces_count 检测到的人脸数量
 * @return YT_SUCCESS:成功,其他:失败
 */

YT_PUBLIC int yt_face_tracker_track(yt_handle handle, const yt_image image, yt_tracked_face **tracked_faces, int *tracked_faces_count);



C#代码

public enum yt_img_type
        {
            YT_IMG_BGR_8UC3,
            YT_IMG_RGB_8UC3,
            YT_IMG_GRAY_8UC1,
            YT_IMG_DEPTH_16UC1,
        }        
      
        public struct yt_image
        {
            public IntPtr data;
            public int width;
            public int height;
            public yt_img_type type;
        };
        public struct yt_pointf
        {
            float x;
            float y;
        }

        public struct yt_rect_t
        {
            int x;
            int y;
            int width;
            int height;
        }

        public struct yt_tracked_face_t
        {
            yt_pointf face_5_points;   
            yt_pointf face_rect;
            int frame_id;      ///< 帧ID
            int trace_id;      ///< 轨迹ID
            bool consecutive;  ///< 轨迹是否存在

            float pitch;  ///< 人脸俯仰角
            float yaw;    ///< 人脸偏航角
            float roll;   ///< 人脸翻滚角
        }



```c#
[DllImport(@"libYTFaceTracker.dll", CallingConvention = CallingConvention.Cdecl)]
       
private extern static int yt_face_tracker_detect(IntPtr handle, yt_image image, yt_face_tracker_param_t param, ref IntPtr yt_tracked_face, ref int tracked_faces_count);

Mat mat = new Mat(@"\_MG_0721.JPG");
 yt_image yt_img = new yt_image
 {
  data = mat.Data,
 width = 1616,
 height = 1077,
 type = yt_img_type.YT_IMG_BGR_8UC3
 };

 YTFaceTrackerHelper.yt_face_tracker_param_t param= new yt_face_tracker_param_t
 {
 min_face_size = 50,
 max_face_size = 100,
threshold =70.0f,
bigger_face_mode = 0,
  detect_interval = 6
  };
            yt_tracked_face_t s = new yt_tracked_face_t();
            IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(s));
            Marshal.StructureToPtr(s, ptr, false);
            int factcount = 0;
 result = face_tracker_detect(Handle, yt_img, param, ref ptr, ref factcount);

执行后提示错误:尝试读取或写入受保护的内存。这通常指示其他内存已损坏

能看看YT_PUBLIC吗,这个情况如果排除c++方法内指针动内存,那怀疑是dll弹栈时清理过了栈空间,c#这边的cdel又清理了一遍,栈被破坏后导致的

应该就是指针参数错误 ,但是不知道错误在那

yt_image ytImage;
ytImage.data = frame->data.get();
ytImage.width = frame->width;
ytImage.height = frame->height;
ytImage.type = yt_img_type::YT_IMG_BGR_8UC3;

yt_tracked_face* faces=nullptr;
int faceCount = 0;

C# 怎么写
int code = -1;
if(isContinuous){
code = yt_face_tracker_track(track, ytImage, &faces, &faceCount);
}