I am writing a video program, everything works fine except I am often (ie most times, but not every time) getting the following error:

Unhandled exception at 0x6076fde0 (msvcr90d.dll) in javaw.exe: 0xC0000005: Access violation reading location 0x0a450020.

I have tracked it down to this function:

//fill the buffer from data
void TFrame::WriteBuffer(unsigned char *ucBuffer){
	if (PixelBuffer != NULL)
	{
		if ( ucBuffer != NULL)
		{
			if (PixelBufferSize != NULL)
			{
                                //the error doesnt occour when this line is commented out
				memcpy (PixelBuffer,ucBuffer,PixelBufferSize);
			}
		}
	}
}

I start a thread to capture the video. The exception only happens when I am ending it. This is the "run" method:

void TImageProcessor::run()
{
	//only perform action when CapturingVideo is true;
	TerminatedThread=false;
	while(true){
		if (capturingVideo)
		{
			unsigned char * ret = StillCapture->GrabImage();
			Frame->WriteBuffer( ret );

                        //the following line is where the exception occours vvv
			Sleep(20);
		} else {
			// checks it isnt needed ~ 50 times a second

                        Sleep(20);
		}
		if (TerminatedThread)
		{
			break;
		}
	}
	cout << "ending thread" << endl;
}

I really have no idea why a memcpy would fail like this! At first I thought that some of the variables were null or something, I am used to programming in Java, so I dont have a whole lot of experience with pointers, so I am not sure my null checks are right.

Thanks for any help

You are attempting to fiddle around with memory that does not belong to your programme. Make sure that all the memory from PixelBuffer to PixelBuffer+PixelBufferSize and ucBuffer to ucBuffer+PixelBufferSize belongs to you.

You are attempting to fiddle around with memory that does not belong to your programme. Make sure that all the memory from PixelBuffer to PixelBuffer+PixelBufferSize and ucBuffer to ucBuffer+PixelBufferSize belongs to you.

I'm not sure how to aproach doing this, how can I tell if memory doesnt belong to my program? My first guess would be to check whether it is null?

for example:

//check if the end of the array exists
unsigned char endOfArray = ucBuffer[PixelBufferSize-1];
if (endOfArray != NULL)
{
	//success
	memcpy (PixelBuffer,ucBuffer,PixelBufferSize);
}

(it doesnt work!)

Memcpy works by reading and writing consecutive memory locations, starting at PixelBuffer/ucBuffer and going all the way to to PixelBuffer+PixelBufferSize / ucBuffer+PixelBufferSize.

You must examine where in your code you are actually allocating all the memory for ucBuffer and PixelBuffer ,and ensure that you are allocating enough.

Checking for null will only tell you if you have a null pointer; it will tell you nothing about how much memory is yours to play with.

unsigned char endOfArray = ucBuffer[PixelBufferSize-1];
if (endOfArray != NULL)

This is fundamentally flawed, and I think you've not understood how memory is allocated. You have created a single character called endOfArray, and given it the value that is in ucBuffer[PixelBufferSize-1].

Null checking is usually done on a pointer, not a char, and all it tells you is that someone has set that pointer to be a null pointer. If a piece of memory contains whatever value is being used as NULL (usually zero), that doesn't actually tell you anything at all about whether or not you can play with that memory. That piece of memory could contain the value zero completely by chance because it's random garbage, or that piece of memory could be a completely different object that happens to have the value zero. The contents of the memory has nothing to do with whether or not you allocated it yourself for some purpose.

If a piece of memory contains whatever value is being used as NULL (usually zero), that doesn't actually tell you anything at all about whether or not you can play with that memory

I knew that checking if it was null was nonsence really.. just didnt know what else to do.

I looked for the source of the problem, it is (i think) is this method. After the camera is closed, m_hG == 0, so the image isn't grabbed.

unsigned char* Tuc480StillCapture::GrabImage(){
	
	if ( m_hG != 0 )
	{
		is_FreezeVideo( m_hG, IS_WAIT );
		is_RenderBitmap( m_hG, m_lMemoryId, m_hWnd, IS_RENDER_NORMAL );
		//is_GetImageMem (m_hG, (void**)&m_pcImageMemory);
	}
	
	return (unsigned char*)m_pcImageMemory;
}

The problem is that when I close the camera, the thread doesn't allways end imediatley, whereas closing the camera is imediate.

In the end, I solved it by adding a "ThreadFinnished" flag, and when the user disconnects wait for the capture to finish before disconnecting:

//wait for the thread to finish before disconnecting

if (CapturingVideo){
	StopContinousCapture();
}
while(FinishedEndingThread==false)
{
	Sleep(20);
}
StillCapture->DisconnectFromDevice()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.