Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3598606
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:15:47+00:00 2026-05-18T20:15:47+00:00

everyone — I have a Heisenbug, here (only shows up with debugger detached!). Seems

  • 0

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);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T20:15:48+00:00Added an answer on May 18, 2026 at 8:15 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

everyone, i have no idea why this doesn't work hopefully someone here can help
everyone I have one problem here. My site is powered by Mediawiki, which means,
Everyone in my office uses Macs and therefore most use Safari. We have a
everyone I got problem here I need to update a plist data in a
everyone, I have this piece of the code: void foo(int var, int var1) {
Hello everyone i have big query to calculation and counter by clinic ID SELECT
Everyone, I have really tried, but I can't find out how to upload photos
everyone! I'm trying to send mail in ASP.NET web-site. I have smtp configuration section
everyone. I have BMP specification from MS ( http://msdn.microsoft.com/en-us/library/dd183383.aspx ) and I want to
Everyone remembers google browser sync right? I thought it was great. Unfortunately Google decided

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.