开发者

OpenCv from C# interop hangs on cvSaveImage, if no debugger!

everyone -- I have a Heisenbug, here (only shows up with debugger detached!). Seems to be in the C# to unmanaged interop -- If I do OpenCv cvSaveImage to the %TEMP% directory, it's ok, but to other directories I've tried, it throws an AccVio or hangs. Running as admin, so not directly a security issue. Running under the C# Visual Studio 2010 debugger, it works ok to any directory. Debugger detached (ctrl F5) ... crash or hang! Accch.

I would be most grateful for any advice. I know some of you out there live and breathe at this interop layer, but I don't have the tools & sufficiently recent experience. I've made a really small repro for you to play with

Must have OpenCv2.2 [free image processing and computer vision (cv)] (Dec 2010 version) installed using defaults, from sourceforge.net/projects/opencvlibrary/

Then pick up my Visual Studio 2010 project from http://dl.dropbox.com/u/1997638/OpenCv2.2Test.zip

This has the wrapper (thanks to Heiko Kießling, iib-chemnitz.de for the original!) and a TEST program with comments that guide you to the repeatable bug.

once again, I'd be most grateful for any advice or clues!

EDIT: @Aliostad: Here is the whole program (minus the wrapper, which is in the VS project I linked)

        static void Main(string[] args)
    {
        CvLib.CvNamedWindow("TestWindow2", CvLib.CV_WINDOW_AUTOSIZE);

        var img = CvLib.CvCreateImage(
            CvLib.CvSize(256, 256),
            depth: 8,
            channels: 3);

        var pts = new[] {
            CvLib.CvPoint(0, 0),
            CvLib.CvPoint(0, 255),
            CvLib.CvPoint(255,255),
            CvLib.CvPoint(255, 0)
    开发者_运维问答    };

        CvLib.CvFillConvexPoly(
            img: ref img,
            pts: ref pts[0],
            npts: 4,
            color: CvLib.CV_RGB(0, 0, 255),
            line_type: 8,
            shift: 0);

        CvLib.CvCircle(
            img: ref img,
            center: CvLib.CvPoint(128, 128),
            radius: 20,
            color: CvLib.CV_RGB(255, 255, 0),
            thickness: 5,
            line_type: 8,
            shift: 0);

        CvLib.CvShowImage(
            name: "TestWindow2",
            image: ref img);

        var tempDirectory = Environment.GetEnvironmentVariable("TEMP");
        var path = tempDirectory + @"\test.png";

        CvLib.CvSaveImage(
            path: path,
            img: ref img);

        MessageBox.Show("Click OK to exit");
        CvLib.CvDestroyAllWindows();
    }

Changing "path" to anything else causes crash or hang when debugger is NOT attached.

EDIT: @Aliostad -- here is the interop code for cvSaveImage. I am sure I don't understand the MarshalAs attribute, and I'm guessing it's the source of the problem

        public static void CvSaveImage(string path, ref IplImage img)
    {
        cvSaveImage(path, img.ptr);
    }
    [DllImport(HIGHGUI_LIBRARY, CallingConvention = CallingConvention.Cdecl)]
    private static extern void cvSaveImage([MarshalAs(UnmanagedType.LPStr)] String filename, IntPtr img);


This is the declaration of the native cvSaveImage() function, retrieved from highgui.h:

CVAPI(int) cvSaveImage( const char* filename, const CvArr* image,
                        const int* params CV_DEFAULT(0) );

This is the pinvoke declaration for it, retrieved from the wrapper, HighGui.cs:

    [DllImport(HIGHGUI_LIBRARY, CallingConvention = CallingConvention.Cdecl)]
    private static extern void cvSaveImage([MarshalAs(UnmanagedType.LPStr)] String filename, IntPtr img);

Note how the native declaration has three arguments, the pinvoke declaration has only two. It is possible to call the native function from a C++ program and pass only two arguments, the compiler automatically applies the default value for the 3rd argument. But that doesn't work that way with the pinvoke marshaller, it has no clue that the 3rd argument even exists.

The result is that the native function gets an arbitrary value for the params parameter. It works when it is 0 by accident. But there are 4 billion other ways for the function to get a non-null argument and nose-dive on an access violation when it dereferences the pointer. Failure will be random.

Fix the problem by editing the pinvoke declaration and adding an IntPtr as the 3rd parameter. Pass IntPtr.Zero in your C# code.

This is rather a basic mistake, expect more trouble with this wrapper.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜