Beginner having trouble with invalid memory

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Iain Emerson
    New Member
    • Feb 2011
    • 1

    Beginner having trouble with invalid memory

    The following code is sending me into an Access violation reading location 0x025e2000 - I'm a novice programmer and I'm hoping somebody can point me in the right direction

    Code:
    CvCapture * Capture=NULL;
    Capture = cvCaptureFromCAM(-1);
    
    IplImage* FrameImage = 0;	
    IplImage* MirrorImage = 0;	
    
    for(;;)
    	{
    		FrameImage = cvQueryFrame(Capture);
    
    		MirrorImage = cvCloneImage(FrameImage);
    		cvFlip(MirrorImage, NULL, 1);
    
    int b, r, g;	//colour channels
    		int x, y;		//For indexing through image
    		
    		cvNamedWindow( "OutPut", CV_WINDOW_AUTOSIZE );
    
    		for ( x = 0; x < MirrorImage->width; x++ )
    		{
    			for ( y = 0; y < MirrorImage->height; y++ )
    			{
    				b = ((uchar *)(MirrorImage->imageData + x*MirrorImage->widthStep))[y*MirrorImage->nChannels+0];
    				g = ((uchar *)(MirrorImage->imageData + x*MirrorImage->widthStep))[y*MirrorImage->nChannels+1];
    				r = ((uchar *)(MirrorImage->imageData + x*MirrorImage->widthStep))[y*MirrorImage->nChannels+2];
    
    				if ( !( g > 170 && r < 150 && b < 150 ) )
    					{
    					(FrameImage->imageData + x*FrameImage->widthStep)[y*FrameImage->nChannels+0]=(MirrorImage->imageData + x*MirrorImage->widthStep)[y*MirrorImage->nChannels+0];
    					(FrameImage->imageData + x*FrameImage->widthStep)[y*FrameImage->nChannels+1]=(MirrorImage->imageData + x*MirrorImage->widthStep)[y*MirrorImage->nChannels+1];
    					(FrameImage->imageData + x*FrameImage->widthStep)[y*FrameImage->nChannels+2]=(MirrorImage->imageData + x*MirrorImage->widthStep)[y*MirrorImage->nChannels+2];
    					}
    			}
    		}
    		cvShowImage(WinName, FinImage);
    		cvShowImage("OutPut", FrameImage);
    }
    I'm trying to achieve a green screen application, but the code falls over while indexing through the image pixels, at x=482, y=64.

    I look forward to your wisdom.
    Last edited by Stewart Ross; Feb 15 '11, 01:15 PM. Reason: corrected end code tag
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    FrameImage and MirrorImpage are pointers. These pointers contain 0. The address 0 is not in your process address space, hence the Access Violation.

    Either create FrameImage and MirrorImage as stack arrays ot allocate the memory yourself and use them as heap arrays.

    In short, you will get errors whenever you try to access memory that your process does not own.

    Comment

    • Liam Kurmos
      New Member
      • Feb 2011
      • 8

      #3
      expanding the above answer in case it's not clear to you:

      "...allocat e the memory yourself and use them as heap arrays."

      this means use the new keyword to allocate the memory

      Comment

      • newb16
        Contributor
        • Jul 2008
        • 687

        #4
        They are not null, they are assigned something.
        FrameImage = cvQueryFrame(Ca pture);
        MirrorImage = cvCloneImage(Fr ameImage);
        The other thing is that you never free/release them after you process them.

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          I didn't see that. oops.

          However, you can't return an array from a function. You can return the address of the array but not the number of elements. So in this case, you have no idea how large the array is. In ths case, the function needs two output arguments. One for the array address and one for the number of elements. The return value should be a success/fail code.

          Comment

          Working...