GetFrameImage Access Violation

Post Reply
RLovelett
Posts: 3
Joined: Fri May 23, 2008 10:33 am

GetFrameImage Access Violation

Post by RLovelett »

I am having an issue that when I call GetFrameImage() on a camera that I am sure is working and has already grabbed a frame from and the frame is not null.

Our model and revision numbers are NP_HW_MODEL_TRACKIR and NP_HW_REVISION_TRACKIR_PRO respectively.

My code is based upon the example "Optitrack_Win32Sample_v2". We have a UI already built for our robot that is written using FLTK as the UI control stuff. The problem is that I cannot use the _INPCameraEvent model because that requires MFC and ATL which will not play well with FLTK, well if I could just have ATL and extend the abstract base class correctly it wouldn't be a problem but the MFC part is.

So, my fix for it was to start a thread that did a similar thing as the code in the example I referenced earlier. When it reaches the GetFrameImage command inside of LoadPixelBuffer() it gives me an "Access violation writing location 0x...". So I'm not really sure what the problems are, my only difference between the sample code and mine is that the threading, so I'm sure it's the problem. Any suggestions? Code and work flow follow.

Basically the work flow of the class and thread is this...
1) Declare all the objects and get the environment ready
2) Connect to our camera by model and type
3) Start a thread to watch for non-null frames
4) Grab images from the TrackIR when not null.

Constructor Code

Code: Select all

//== Initialize Microsoft COM Interop ================
CoInitialize(NULL);

cameraCollection.CoCreateInstance(CLSID_NPCameraCollection);

//== Enumerate (Identify) Available Cameras ==========----
cameraCollection->Enum();

cameraCount = 0;
frameCounter = 0;

cameraCollection->get_Count(&cameraCount);	//	Get number of cameras

load_our_camera();		//	Find and load our camera

//== Set Greyscale Mode =========================----
camera->SetOption(NP_OPTION_VIDEO_TYPE        , (CComVariant) 1 );

//== Set to discard every other frame ===========----
//== 60 Frames/Second on the C120 ===============----
camera->SetOption(NP_OPTION_FRAME_DECIMATION  , (CComVariant) 1 );

frameBuffer = new unsigned char[353*288];

GrabFrames.SetThreadName("GrabFramesTracker");
GrabFrames.CreateThread(get_frames, this);
Thread Code

Code: Select all

HeadTracker *trackerObj = (HeadTracker*)trkObj;

	if(trackerObj->camera->Open() == S_OK) {
		if(trackerObj->camera->Start() == S_OK) {
			while(trackerObj->GrabFrames.IsThreadActive()) {		//	While thread is active
				trackerObj->LoadPixelBuffer();
				Sleep(5);	//	Wait 5 milliseconds
			}
		}
	}

	cout << "Thread is dying!" << endl;
LoadPixelBuffer

Code: Select all

camera->GetFrame(0, &frame);
	if(frame != 0) {
		long is_corrupt;
		frame->get_IsCorrupt((VARIANT_BOOL*)&is_corrupt);

		if(VB2B(is_corrupt)) {
			cout << "My frame is corrupt!" << endl;
		} else {
			camera->GetFrameImage(frame, 353, 288, 353, 8, (byte *) &frameBuffer);
			frame->Free();
		}
	}
	frame.Release();
beckdo
Posts: 520
Joined: Tue Jan 02, 2007 2:02 pm

Re: GetFrameImage Access Violation

Post by beckdo »

Your code does looks relatively straight-forward. I don't see anything wrong with the code that you've posted. Please make sure that you're not letting your frameBuffer allocation is not being deallocated somehow. You might want to declare it in your thread to see if that makes a difference since the only writing the GetFrameImage() does is to the frameBuffer and that's what the access violation is making reference of.

Another good test would be to thread earlier. Then put everything from the creation of the cameracollection, camera, and framebuffer all the way through the grabbing of the frame images in the same thread to help highlight any threading issues that make be coming into play.
RLovelett
Posts: 3
Joined: Fri May 23, 2008 10:33 am

Re: GetFrameImage Access Violation

Post by RLovelett »

Doug,

Thanks for the help. Unfortunately, that doesn't really fix the error it just transposes it to another area. Basically, I have changed the LoadPixelBuffer command from what it was to:

Code: Select all

unsigned char* frameBuffer = new unsigned char[353*288];

camera->GetFrame(0, &frame);
if(frame != 0) {
	long is_corrupt;
	frame->get_IsCorrupt((VARIANT_BOOL*)&is_corrupt);

	if(VB2B(is_corrupt)) {
		cout << "My frame is corrupt!" << endl;
	} else {
		camera->GetFrameImage(frame, 353, 288, 353, 8, (byte *) &frameBuffer);
		frame->Free();
	}
}
frame.Release();

delete [] frameBuffer;
frameBuffer = 0;
Now when camera->GetFrameImage() finishes (running in debug mode) I look at camera, frame and frameBuffer and they all have become invalid pointers. frameBuffer has become a "" and camera and frame are now "{???}" and frame->Free(); now throws an exception.

This is not happening in the other example code when I run it.

Ryan
beckdo
Posts: 520
Joined: Tue Jan 02, 2007 2:02 pm

Re: GetFrameImage Access Violation

Post by beckdo »

I think you just have something funky with your threading code. I put together a simple threaded example for you. Please email support and they will send it to you. If you need more feel free to ask them.
Post Reply