Click to See Complete Forum and Search --> : Unhandled Exception


dacky
July 17th, 2006, 11:21 PM
why do i get an unhandled exception on the memcpy function?
here is the case...


LPBYTE m_ImageBuffer;
Long nTotalBytesRead = 32768
Long nBytesRead = 32768;
LPBYTE pImageData;


pImageData = SomeFunction(); //put some data

memcpy( (LPBYTE) (m_ImageBuffer + nTotalBytesRead - nBytesRead ),
pImageData, nBytesRead );

////////////////////////////////////////////////////
thanks gurus

Bornish
July 18th, 2006, 01:37 AM
I don't see where you allocate memory that m_ImageBuffer points to,... so probably memcpy tries to copy at an uninitialized pointer. Not to mention that you must trust "SomeFunction" that always returns a valid address. And you should also specify in your post what is the exception you're getting (error code and / or error description) 'cause I've just assumed 0xC0000005 (error message might also say if error was generated when reading or writting, since memcpy does both ;) )
Regards,

dacky
July 18th, 2006, 10:39 AM
thanks GURU Bornish..i think i got your point...

Krishnaa
July 18th, 2006, 11:01 AM
why do i get an unhandled exception on the memcpy function?
here is the case...


LPBYTE m_ImageBuffer;
Long nTotalBytesRead = 32768
Long nBytesRead = 32768;
LPBYTE pImageData;


pImageData = SomeFunction(); //put some data

memcpy( (LPBYTE) (m_ImageBuffer + nTotalBytesRead - nBytesRead ),
pImageData, nBytesRead );

////////////////////////////////////////////////////
thanks gurus

You should use initialized pointers in order to detect the problems of allocation failure & use try catch blocks where you suspect exception would occur.


LPBYTE m_ImageBuffer = NULL;
Long nTotalBytesRead = 32768
Long nBytesRead = 32768;
LPBYTE pImageData = NULL;

try{
pImageData = SomeFunction(); //put some data
}catch(...){printf("Exception..."); exit();}
if(pImageData)
{
// Are you sure that pointer points to data if size nBytesRead = 32768 ??
memcpy( (LPBYTE) (m_ImageBuffer + nTotalBytesRead - nBytesRead ),
pImageData, nBytesRead );
}