使用 SWIG 作为参数传递给 C 库的错误值 [英] Wrong values passed as parameter to C library using SWIG
问题描述
遵循我的三个 以前 posts,我现在可以将一个托管的结构数组传递给我的包装方法.以下是文件的摘录:
Following my three previous posts, I can now pass a managed array of struct to my wrapped method. Here is an extract from the files:
// packer.i
typedef struct {
int width; // input
int height; // input
frame_t view; // output
frame_t dest; // output
} image_t;
CSHARP_ARRAYS(image_t, image_t)
%apply image_t INOUT[] { image_t *images }
int pack(image_t *images, int nb_images, parameters_t params);
生成具有此签名的函数:
Which generates a function with this signature:
// packer_cs.cs
public static int pack(image_t[] images, int nb_images, parameters_t arg2)
我这样称呼:
// Program.cs
var files = Directory.GetFiles("./images");
var images = new image_t[files.Length];
for (var f = 0; f < files.Length; f++)
{
using (var imgInfo = Image.FromFile(files[f]))
{
var imgStruct = new image_t()
{
width = imgInfo.Width,
height = imgInfo.Height,
dest = new frame_t(),
view = new frame_t()
};
images[f] = imgStruct;
}
}
var result = packer_cs.pack(images, images.Length, new parameters_t());
一切都很好,但是当我运行 pack()
方法时,我遇到了受保护的内存访问问题 (System.AccessViolationException
).幸运的是,我可以访问 C 库的源代码,并且一旦启用非托管代码调试,Visual Studio 就会自动打开它进行调试和单步调试.
All is well and done, but when I run the pack()
method, I have a protected memory access problem (System.AccessViolationException
). Thankfully I have access to the source code of the C library, and Visual Studio automagically opens it for debugging and stepping through as soon as I enable unmanaged code debugging.
因此,如果我在 pack()
函数的开头添加断点,并使用手表检查 images[x]
,我可以看到宽度和高度值与提供的内容无关(有时甚至是 0 或负数).这是怎么回事 ?如果我在 C# 端检查我的托管数组,则会正确存储和检索这些值.为什么 C 没有得到正确的值?其他参数(nb_images 和 params)没有任何问题.
So, if I add a breakpoint at the start of the pack()
function, and I use a watch to check images[x]
, I can see that the width and height values have nothing to do with what is provided (sometimes it's even 0 or negative). What's going on ? If I inspect my managed array on the C# side, the values are correctly stored and retrieved. Why doesn't C get the right values ? The other parameters (nb_images and params) don't have any problem.
谢谢!
推荐答案
执行以下操作:
- 检查
images[f].width
和 height 是否具有您期望的值 - 如果是,则检查 SWIG 生成的代码以验证这些字段是否已正确复制.
- 如果通过查看代码无法发现任何问题,则应中断
packer_cs.pack
并使用调试器查看将 C# 数组复制到 C++ 数组的包装器代码,看看有什么问题.
- Check that
images[f].width
and height have the values you expect - If yes, then check the SWIG-generated code to verify that those fields are properly copied.
- If you can't spot any problem by looking at the code, you should break on
packer_cs.pack
and use the debugger to look at the wrapper code that copies the C# array to the C++ array, see what is wrong.
可能是类型映射中的某些内容不正确.一旦您知道那是什么,您就可以将 SWIG 源(csharp_array.i 文件)中的类型映射代码复制到 .i 中的新类型映射,并根据需要进行修复.
It is probably something in the typemaps that is incorrect. Once you know what that is, you can copy the typemaps code from SWIG source (the csharp_array.i file) to a new typemap in your .i and fix as required.
这篇关于使用 SWIG 作为参数传递给 C 库的错误值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!