memcpy error: Access violation reading
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: memcpy error: Access violation reading

Hybrid View

  1. #1
    Join Date
    May 2009
    Posts
    11

    Smile memcpy error: Access violation reading

    Hi all,

    I am working on a camera to collect data. In the process one of the functions is throwing errors while copying the data from the buffer on board to local system buffer.

    The message which I am getting is


    Unhandled exception at 0x1026ef58 (msvcr90d.dll) in Camera.exe: 0xC0000005: Access violation reading location 0xd41dddcc.

    Code:
    __declspec(dllexport) int intf_daq_get_frame (void * intf_p, unsigned char * frame_data, unsigned long frame_size)
    {	
    	BFDev *bf_p;
    	bf_p = (BFDev *) intf_p;
    	if (bf_p == NULL)
    	{
    		return FAILURE;
    	}	
    	
    
    	// Wait for frame to be acquired.
    	bf_p->rv = BiCirWaitDoneFrame(bf_p->Board, &bf_p->BufArray, INFINITE,  &bf_p->CirHandle);
    	
    	// Process newly acquired frame 		
    	memcpy(frame_data, (unsigned char *) (bf_p->CirHandle.pBufData), frame_size);
    
    	// Mark the processed buffer available for acquisition
    	bf_p->rv = BiCirStatusSet(bf_p->Board, &bf_p->BufArray,  bf_p->CirHandle,  BIAVAILABLE);
    	if(bf_p->rv != BI_OK)
    	{		
    		BiErrorShow(bf_p->Board, bf_p->rv);
    
    		return FAILURE;
    	}
    
    
    	return SUCCESS;
    }
    Can someone tell me what would have went wrong and any suggestions.

    Thanks in advance.

  2. #2
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: memcpy error: Access violation reading

    I know nothing about cameras and stuff, but if you are sure that the memcpy is the failure, then I have to assume that 'bf_p->CirHandle.pBufData' is not valid. So, or 'bf_p' is not valid or 'pbBufData' is not valid. You can use the debugger to inspect bot pointers and see what they contain.

  3. #3
    Join Date
    May 2009
    Posts
    11

    Smile Re: memcpy error: Access violation reading

    Thanks for the reply Skizmo

    Both pointers are valid, the data in bf_p->CirHandle.pBufData is 3677536032, which is unsigned int and I am Type Casting it to unsigned char.

    When I debug, the output shows

    Code:
    First-chance exception at 0x1026ef58 (msvcr90d.dll) in Camera.exe: 0xC0000005: Access violation reading location 0xd41dddcc.
    Unhandled exception at 0x1026ef58 (msvcr90d.dll) in Camera.exe: 0xC0000005: Access violation reading location 0xd41dddcc.
    Is this a problem with Overlapping Buffers? or something else.

  4. #4
    Join Date
    Oct 2002
    Location
    Austria
    Posts
    1,284

    Re: memcpy error: Access violation reading

    Quote Originally Posted by gowrishwar View Post
    Is this a problem with Overlapping Buffers? or something else.
    If you think that the problem might be overlapping buffers use memmove() instead of memcpy().
    Kurt

  5. #5
    Join Date
    May 2009
    Posts
    11

    Smile Re: memcpy error: Access violation reading

    I have tried memmove(). Even then it is throwing the same exception.
    Code:
    Output:
    First-chance exception at 0x1026f448 (msvcr90d.dll) in Camera.exe: 0xC0000005: Access violation reading location 0xd41dddcc.
    Unhandled exception at 0x1026f448 (msvcr90d.dll) in Camera.exe: 0xC0000005: Access violation reading location 0xd41dddcc.
    Then I try to debug more by adding the following piece of code in the place of memcpy().

    Code:
    	unsigned char *FData;
    	if ((FData = (unsigned char *) calloc(1 , sizeof(unsigned char))) == NULL)
    	{
    		return NULL;
    	}
    	FData = (unsigned char *) frame_data;
    
    
     if (FData <= (unsigned char *) (bf_p->CirHandle.pBufData) || FData >= ((unsigned char *) (bf_p->CirHandle.pBufData) + frame_size))
    	 {
    		 /*
              * Non-Overlapping Buffers
              * copy from lower addresses to higher addresses
              */
             while (frame_size--)
                  *FData++ = *(unsigned char *) (bf_p->CirHandle.pBufData)++;
    	 }
    	else
    	 {
    		 /*
             * Overlapping Buffers
             * copy from higher addresses to lower addresses
             */
             FData += frame_size - 1;
             *(unsigned char *) (bf_p->CirHandle.pBufData) = *(unsigned char *) (bf_p->CirHandle.pBufData) + (frame_size - 1);
    		 while (frame_size--)
                 *FData-- = *(unsigned char *) (bf_p->CirHandle.pBufData)--;
    	   }
    Then the exception I am getting is
    Unhandled exception at 0x0077193b in Camera.exe: 0xC0000005: Access violation writing location 0x9b9b9b99.

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: memcpy error: Access violation reading

    Clearly, one or more of those pointers aren't valid......

  7. #7
    Join Date
    Oct 2002
    Location
    Austria
    Posts
    1,284

    Re: memcpy error: Access violation reading

    Quote Originally Posted by gowrishwar View Post
    Code:
    	unsigned char *FData;
    	if ((FData = (unsigned char *) calloc(1 , sizeof(unsigned char))) == NULL)
    	{
    		return NULL;
    	}
    	FData = (unsigned char *) frame_data;
    This really looks strange.
    You calloc a singe byte just to immediately overwrie the returned pointer creating a memory leak.
    Kurt

  8. #8
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,634

    Re: memcpy error: Access violation reading

    Casts are pure evil when used (without a deep understanding) just to solve a compiler error and I think this shows that you are in deep water at the moment
    unsigned char *FData;
    if ((FData = (unsigned char *) calloc(1 , sizeof(unsigned char))) == NULL)
    {
    return NULL;
    }
    FData = (unsigned char *) frame_data;
    I would advise you to get rid of all casts and solve the compilation issues by using the correct types instead.
    Last edited by S_M_A; April 22nd, 2010 at 04:35 PM.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    http://www.microsoft.com/visualstudio/eng/downloads

  9. #9
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: memcpy error: Access violation reading

    I would advise you to get rid of all casts and solve the compilation issues by using the correct types instead.
    And how would you like to do that if 'bf_p->CirHandle.pBufData' is a void* that actually points to a byte buffer ?

  10. #10
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,634

    Re: memcpy error: Access violation reading

    Quote Originally Posted by Skizmo View Post
    And how would you like to do that if 'bf_p->CirHandle.pBufData' is a void* that actually points to a byte buffer ?
    memcpy is declared like
    Code:
    void* memcpy(void* dst, void* src, size_t size);
    which is to say it accepts any pointer for the params so why should it be any problem to use a pointer to the correct type? The cast in the call to memcpy just tells the compiler: "ignore that this looks strange, I know what I'm doing"

    So, if pBufData actually points at a byte buffer I prefer to declare it as an unsigned char* to help the compiler help me in finding my mistakes.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    http://www.microsoft.com/visualstudio/eng/downloads

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center