Accessviolationexception - Read or write attempt in protected memory

Asked

Viewed 1,776 times

2

I’m working with the OPENCVSHARP (Opencv) to access a camera Ps3eye (Playstation 3) camera in C#.

But I am having a serious problem with protected memory! I use visual studio 2010 on Windows7.

Print do erro

Code:

    IntPtr _ptr;
    public IntPtr _ptrBmpPixels;
    static IntPtr _camera;
    static int w = 0, h = 0;

    public void button2_Click(object sender, EventArgs e)
    {
        _camera = CLEyeCreateCamera(CameraUUID(0), CLEyeCameraColorMode.CLEYE_COLOR_RAW, CLEyeCameraResolution.CLEYE_VGA, 75);
        CLEyeCameraGetFrameDimensions(_camera, ref w, ref h);
        CLEyeCameraStart(_camera);
        _ptrBmpPixels = Marshal.AllocHGlobal(w * h * 4);
        RtlZeroMemory(_ptrBmpPixels, w * h * 4);
        Bitmap bmpGraph = new Bitmap(w, h, w * 4, PixelFormat.Format32bppRgb, _ptrBmpPixels);
        pictureBox2.Image = bmpGraph;
        myTimer2.Enabled = true;
    }

   public void timer2(object sender, EventArgs e)
    {      
     CvInvoke.cvQueryFrame(_camera);// AQUI ESTÁ O ERRO ao ACESSAR O "_camera"

        CLEyeCameraGetFrame( _camera,_ptrBmpPixels,500);
        pictureBox2.Invalidate();  
        //IntPtr img = CvInvoke.cvQueryFrame(_ptrBmpPixels);        
        //IplImage iplImage = (IplImage)Marshal.PtrToStructure(img, typeof(IplImage));           
        Bitmap bmpGraph = new Bitmap (160, 480, 640, PixelFormat.Format32bppRgb, _ptrBmpPixels);    
        IplImage mp = Cv.CreateImage(Cv.Size(bmpGraph.Width, bmpGraph.Height), BitDepth.U8, 3);
        //learn(mp, bmpGraph);
    }

2 answers

1

There is more than one problem in your code and I will point out some of them to you. Although the error happens on that line, the problem is probably in the instructions that are executed before that.

It is important to realize that CLEyeCreateCamera() and other methods may fail. Therefore it is extremely essential defensively encode, and test the return of these methods:

_camera = CLEyeCreateCamera(CameraUUID(0), CLEyeCameraColorMode.CLEYE_COLOR_RAW,CLEyeCameraResolution.CLEYE_VGA, 75);
if (!_camera) {
    // Falhou, imprimir erro na tela
}

Another method that may fail is CLEyeCameraStart(), and so you should perform something like:

 bool success = CLEyeCameraStart(_camera);
 if (!success) {
     // Falhou, imprimir erro na tela
 }

And so on and so forth.

Something else, CvInvoke.cvQueryFrame() expects to receive an object of the type Capture, but you’re going through a IntPtr for him, and it doesn’t make any sense.

The main problem is that you are confusing the API of CL-Eye with the API of Opencv, caring for.

0

This communication between managed and unmanaged memory programs can be complicated. And since it works with pointer in place of objects, then, kind of, anything can happen.

I don’t know this OPENCVSHARP library, but since it uses Intptr then I know it is a pointer in "C" or "C++". I think the violation problem may be that the parameter passed is not quite the expected type. It’s like you’re passing a string-like parameter in a method that waits for an int, as it is a pointer, it can be a pointer to anything.

I looked for a similar call in C, and I found a code that uses these two methods:

  • Cleyecreatecamera
  • cvQueryFrame

And the cvQueryFrame call does not receive a camera, but a 'capture', which apparently is obtained from a method called "cvCaptureFromCAM" passing the camera ID. Follow the code, maybe it’ll help you solve this problem:

https://code.google.com/p/moveonpc/source/browse/src/tracker/camera_control.c?r=431e30226184846a5fe49823606b53a2a212c895

  • Cleyecreatecamera - Line 56
  • cvQueryFrame - Line 129
  • cvCaptureFromCAM - Line 67

Browser other questions tagged

You are not signed in. Login or sign up in order to post.