-
April 22nd, 2010, 12:52 PM
#1
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.
-
April 22nd, 2010, 02:26 PM
#2
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.
-
April 22nd, 2010, 03:11 PM
#3
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.
-
April 22nd, 2010, 03:22 PM
#4
Re: memcpy error: Access violation reading
Originally Posted by gowrishwar
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
-
April 22nd, 2010, 03:37 PM
#5
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.
-
April 22nd, 2010, 03:48 PM
#6
Re: memcpy error: Access violation reading
Clearly, one or more of those pointers aren't valid......
-
April 22nd, 2010, 04:32 PM
#7
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.
-
April 22nd, 2010, 04:38 PM
#8
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 ?
-
April 22nd, 2010, 05:44 PM
#9
Re: memcpy error: Access violation reading
Originally Posted by Skizmo
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.
-
April 23rd, 2010, 11:47 AM
#10
Re: memcpy error: Access violation reading
Originally Posted by gowrishwar
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
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|