AccessViolationException - Attempt to read or write to protected memory

2

I'm working with OPENCVSHARP (OpenCV) to access a Ps3Eye camera in C #.

But I'm having a serious protected memory problem! I use visual studio 2010 in Windows7.

Code:

IntPtr_ptr;publicIntPtr_ptrBmpPixels;staticIntPtr_camera;staticintw=0,h=0;publicvoidbutton2_Click(objectsender,EventArgse){_camera=CLEyeCreateCamera(CameraUUID(0),CLEyeCameraColorMode.CLEYE_COLOR_RAW,CLEyeCameraResolution.CLEYE_VGA,75);CLEyeCameraGetFrameDimensions(_camera,refw,refh);CLEyeCameraStart(_camera);_ptrBmpPixels=Marshal.AllocHGlobal(w*h*4);RtlZeroMemory(_ptrBmpPixels,w*h*4);BitmapbmpGraph=newBitmap(w,h,w*4,PixelFormat.Format32bppRgb,_ptrBmpPixels);pictureBox2.Image=bmpGraph;myTimer2.Enabled=true;}publicvoidtimer2(objectsender,EventArgse){CvInvoke.cvQueryFrame(_camera);//AQUIESTÁOERROaoACESSARO"_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);
    }
    
asked by anonymous 16.06.2014 / 16:55

2 answers

1

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

It's important to realize that CLEyeCreateCamera() and other methods can fail. Therefore it is extremely essential to encode defensive , and test the return of those methods:

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

Another method that can fail is CLEyeCameraStart() , so you should run something like:

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

And so on.

Another thing, CvInvoke.cvQueryFrame() expects to receive an object of type Capture , but you're passing IntPtr to it, and that does not make any sense.

The main problem is that you are confusing the CL-Eye API with the OpenCV API, be careful.

    
03.07.2014 / 17:00
0

This communication between managed and unmanaged memory programs can be complicated. And as you work with pointer instead of objects, then, kind of might happen.

I do not know this OPENCVSHARP library, but since it uses IntPtr then I know it's a "C" or "C ++" pointer. I think the violation problem may be that the passed parameter is not quite the expected type. It's like you're passing a string parameter to a method that expects an int, such as a pointer, it can be a pointer to anything.

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

  • CLEyeCreateCamera
  • cvQueryFrame

And the cvQueryFrame call does not get a camera, but a 'capture', which is apparently obtained from a method called "cvCaptureFromCAM" by passing the camera ID. Here's the code, maybe it will help you solve this problem:

link

  • CLEyeCreateCamera - Line 56
  • cvQueryFrame - Line 129
  • cvCaptureFromCAM - Line 67
30.06.2014 / 19:50