Hi. In my tracking application which I can track both single and multiple markers in I seem to randomly get this sort of error:
First-chance exception at 0x00000030 in New Single Track1.exe: 0xC0000005: Access violation reading location 0x00000030.
Unhandled exception at 0x00000030 in New Single Track1.exe: 0xC0000005: Access violation reading location 0x00000030.
and the program hangs pointing at:
hr = pCamera->GetFrame(waittime, &pFrame);
So it looks like something went wrong during retrieval of a new frame from the camera. This doesn't always seem to happen. It happens every so often. I'm not sure why. Does anybody know of a reason that could cause this error? I'll post my code if you think it could help. Thanks in advance.
~Paul Smith
Unhandled exceptions randomly occuring
-
- Posts: 28
- Joined: Wed Apr 25, 2007 10:39 am
-
- Posts: 28
- Joined: Wed Apr 25, 2007 10:39 am
Re: Unhandled exceptions randomly occuring
Could someone please shed some light on this for me. Thanks alot
~Paul
~Paul
Re: Unhandled exceptions randomly occuring
Could you post a sample of your code, please?
-
- Posts: 28
- Joined: Wed Apr 25, 2007 10:39 am
Re: Unhandled exceptions randomly occuring
Yes sorry. here it is. Thanks
-----------------------------------
#include "stdafx.h"
#include <atlbase.h>
#include "C:\Program Files\NaturalPoint\Optitrack\inc\optitrack.h"
#import "C:\Program Files\NaturalPoint\Optitrack\inc\optitrack.tlb"
INPCameraCollection *cameraCollection;
INPCamera *pCamera;
INPCameraFrame *pFrame;
#define HRCHECK(message) if (FAILED(hr)) { fprintf(stderr, "%s\n", message); return 0; }
//== SAMPLE APPLICATION ENTRY POINT ======================================-----
int _tmain(int argc, _TCHAR* argv[])
{
printf("OptiTrack WIN32 Tracking Application ============================================\n");
HRESULT hr;
LONG count;
LONG misc;
LONG waittime;
int choice = 0;
VARIANT vx[10], vy[10], va[10];
VARIANT voption;
int running = 1;
int toerrors = 0;
printf("Tracking Application Running!\n");
// probably not needed, but doesn't hurt:
for(int i = 0; i < 4; i++)
{
VariantInit(&vx);
VariantInit(&vy);
VariantInit(&va);
}
VariantInit(&voption);
CoInitialize(NULL); // Startup Microsoft crap
// create CameraCollection and Vector objects:
hr = CoCreateInstance(__uuidof(NPCameraCollection), NULL, CLSCTX_ALL,
__uuidof(INPCameraCollection), (void **) &cameraCollection);
// find all the cameras:
hr = cameraCollection->Enum();
HRCHECK("CameraCollection->Enum");
hr = cameraCollection->get_Count(&count);
HRCHECK("CameraCollection->get_Count");
if (count < 1) {
fprintf(stderr, "camera count < 1\n");
return 0;
}
// get one:
hr = cameraCollection->Item(0, &pCamera);
HRCHECK("CameraCollection->Item");
hr = pCamera->get_SerialNumber(&misc);
HRCHECK("Camera->get_SerialNumber");
printf("serial number = %ld\n", misc);
hr = pCamera->Open();
HRCHECK("Camera->Open");
// turn on illumination LEDs
hr = pCamera->SetLED(NP_LED_ONE, VARIANT_TRUE);
HRCHECK("Camera->SetLED");
#if 0
voption.vt = VT_BOOL;
voption.boolVal = VARIANT_TRUE;
hr = pCamera->SetOption(NP_OPTION_SEND_EMPTY_FRAMES, voption);
HRCHECK("Camera->SetOption");
#endif
hr = pCamera->Start();
HRCHECK("Camera->Start");
waittime = 10; // value in milliseconds; 0 = don't wait; INFINITE = wait till data
running = 12000; // run ~1 minute for waittime=10 ms
int ck = 0; //used to display Tracking Now message
LONG lCount = 0;
LONG lRank;
INPObject *spObject;
printf("Make choice \n(1)Track with area\n(2)Track just x,y coords\nEnter Choice >");
scanf("%d",&choice);
if(choice==1)
printf("Tracking position and Area. Place markers in camera view now!\n");
else if(choice==2)
printf("Tracking x,y position. Place markers in camera view now!\n");
else
while(choice!=1 && choice!=2)
{
printf("Please Enter either 1 or 2 to make a valid choice!\nEnter choice >");
scanf("%d",&choice);
if(choice == 1 || choice == 2){
if(choice==1)
printf("Tracking position and Area. Place markers in camera view now!\n");
else if(choice==2)
printf("Tracking x,y position. Place markers in camera view now!\n");
}
}
while(running--) {
hr = pCamera->GetFrame(waittime, &pFrame);
if (FAILED(hr)) {
// fprintf(stderr,"Camera->GetFrame %x ", hr);
switch(hr) {
case S_OK:
fprintf(stderr, "OK\n"); break;
case E_POINTER:
fprintf(stderr, "E_POINTER\n"); break;
case NP_E_DEVICE_DISCONNECTED:
fprintf(stderr, "NP_E_DEVICE_DISCONNECTED\n"); break;
case NP_E_DEVICE_NOT_SUPPORTED:
fprintf(stderr, "NP_E_DEVICE_NOT_SUPPORTED\n"); break;
case HRESULT_FROM_WIN32(ERROR_TIMEOUT):
/*fprintf(stderr, "HRESULT_FROM_WIN32(ERROR_TIMEOUT)\n");*/
toerrors++;
break;
default:
fprintf(stderr, "default\n"); break;
}
continue;
}
pFrame->get_Count(&lCount);
// display number of markers found on camera's numeric LED
CComVariant var((long)lCount);
pCamera->SetOption(NP_OPTION_NUMERIC_DISPLAY_ON, var);
//start object track
for(int i=0; i<lCount; i++)
{
hr = pFrame->Item(i, &spObject);
HRCHECK("Frame->Item");
hr = spObject->get_Rank(&lRank);
HRCHECK("Object->get_Rank");
if(ck == 0)
printf("Tracking Object(s) Now.\n");
// this is the currently tracked object...
// values are double's so we need to use a variant to query for them
hr = spObject->get_X(&vx);
HRCHECK("Object->get_X");
hr = spObject->get_Y(&vy);
HRCHECK("Object->get_Y");
if(choice == 1)
{
spObject->get_Area(&va);
HRCHECK("Object->get_Area");
}
ck = 1;
if(choice == 1)
{
for(int i = 0; i<lCount ; i++)
{
if(i == lCount-1)
printf("M%d=(%.2f,%.2f). Area%d =%.2f.\t\t\t\t\t\r", i+1, vx.dblVal, vy.dblVal, i+1, va.dblVal);
else if(i < lCount-1)
printf("M%d=(%.2f,%.2f) Area%d =%.2f. ", i+1, vx.dblVal, vy[i].dblVal, i+1, va[i].dblVal);
}
}
else if(choice == 2)
{
for(int i = 0; i<lCount ; i++)
{
if(i == lCount-1)
printf("M%d =(%.2f,%.2f).\t\t\t\t\r", i+1, vx[i].dblVal, vy[i].dblVal);
else if(i < lCount-1)
printf("M%d =(%.2f,%.2f). ", i+1, vx[i].dblVal, vy[i].dblVal);
}
}
hr = pFrame->Free();
hr = pFrame->Release();
}
pCamera->SetLED(NP_LED_ONE, VARIANT_FALSE);
pCamera->Stop();
pCamera->Close();
pCamera->Release();
}
-------------------------------------------------
Cheers
~Paul
-----------------------------------
#include "stdafx.h"
#include <atlbase.h>
#include "C:\Program Files\NaturalPoint\Optitrack\inc\optitrack.h"
#import "C:\Program Files\NaturalPoint\Optitrack\inc\optitrack.tlb"
INPCameraCollection *cameraCollection;
INPCamera *pCamera;
INPCameraFrame *pFrame;
#define HRCHECK(message) if (FAILED(hr)) { fprintf(stderr, "%s\n", message); return 0; }
//== SAMPLE APPLICATION ENTRY POINT ======================================-----
int _tmain(int argc, _TCHAR* argv[])
{
printf("OptiTrack WIN32 Tracking Application ============================================\n");
HRESULT hr;
LONG count;
LONG misc;
LONG waittime;
int choice = 0;
VARIANT vx[10], vy[10], va[10];
VARIANT voption;
int running = 1;
int toerrors = 0;
printf("Tracking Application Running!\n");
// probably not needed, but doesn't hurt:
for(int i = 0; i < 4; i++)
{
VariantInit(&vx);
VariantInit(&vy);
VariantInit(&va);
}
VariantInit(&voption);
CoInitialize(NULL); // Startup Microsoft crap
// create CameraCollection and Vector objects:
hr = CoCreateInstance(__uuidof(NPCameraCollection), NULL, CLSCTX_ALL,
__uuidof(INPCameraCollection), (void **) &cameraCollection);
// find all the cameras:
hr = cameraCollection->Enum();
HRCHECK("CameraCollection->Enum");
hr = cameraCollection->get_Count(&count);
HRCHECK("CameraCollection->get_Count");
if (count < 1) {
fprintf(stderr, "camera count < 1\n");
return 0;
}
// get one:
hr = cameraCollection->Item(0, &pCamera);
HRCHECK("CameraCollection->Item");
hr = pCamera->get_SerialNumber(&misc);
HRCHECK("Camera->get_SerialNumber");
printf("serial number = %ld\n", misc);
hr = pCamera->Open();
HRCHECK("Camera->Open");
// turn on illumination LEDs
hr = pCamera->SetLED(NP_LED_ONE, VARIANT_TRUE);
HRCHECK("Camera->SetLED");
#if 0
voption.vt = VT_BOOL;
voption.boolVal = VARIANT_TRUE;
hr = pCamera->SetOption(NP_OPTION_SEND_EMPTY_FRAMES, voption);
HRCHECK("Camera->SetOption");
#endif
hr = pCamera->Start();
HRCHECK("Camera->Start");
waittime = 10; // value in milliseconds; 0 = don't wait; INFINITE = wait till data
running = 12000; // run ~1 minute for waittime=10 ms
int ck = 0; //used to display Tracking Now message
LONG lCount = 0;
LONG lRank;
INPObject *spObject;
printf("Make choice \n(1)Track with area\n(2)Track just x,y coords\nEnter Choice >");
scanf("%d",&choice);
if(choice==1)
printf("Tracking position and Area. Place markers in camera view now!\n");
else if(choice==2)
printf("Tracking x,y position. Place markers in camera view now!\n");
else
while(choice!=1 && choice!=2)
{
printf("Please Enter either 1 or 2 to make a valid choice!\nEnter choice >");
scanf("%d",&choice);
if(choice == 1 || choice == 2){
if(choice==1)
printf("Tracking position and Area. Place markers in camera view now!\n");
else if(choice==2)
printf("Tracking x,y position. Place markers in camera view now!\n");
}
}
while(running--) {
hr = pCamera->GetFrame(waittime, &pFrame);
if (FAILED(hr)) {
// fprintf(stderr,"Camera->GetFrame %x ", hr);
switch(hr) {
case S_OK:
fprintf(stderr, "OK\n"); break;
case E_POINTER:
fprintf(stderr, "E_POINTER\n"); break;
case NP_E_DEVICE_DISCONNECTED:
fprintf(stderr, "NP_E_DEVICE_DISCONNECTED\n"); break;
case NP_E_DEVICE_NOT_SUPPORTED:
fprintf(stderr, "NP_E_DEVICE_NOT_SUPPORTED\n"); break;
case HRESULT_FROM_WIN32(ERROR_TIMEOUT):
/*fprintf(stderr, "HRESULT_FROM_WIN32(ERROR_TIMEOUT)\n");*/
toerrors++;
break;
default:
fprintf(stderr, "default\n"); break;
}
continue;
}
pFrame->get_Count(&lCount);
// display number of markers found on camera's numeric LED
CComVariant var((long)lCount);
pCamera->SetOption(NP_OPTION_NUMERIC_DISPLAY_ON, var);
//start object track
for(int i=0; i<lCount; i++)
{
hr = pFrame->Item(i, &spObject);
HRCHECK("Frame->Item");
hr = spObject->get_Rank(&lRank);
HRCHECK("Object->get_Rank");
if(ck == 0)
printf("Tracking Object(s) Now.\n");
// this is the currently tracked object...
// values are double's so we need to use a variant to query for them
hr = spObject->get_X(&vx);
HRCHECK("Object->get_X");
hr = spObject->get_Y(&vy);
HRCHECK("Object->get_Y");
if(choice == 1)
{
spObject->get_Area(&va);
HRCHECK("Object->get_Area");
}
ck = 1;
if(choice == 1)
{
for(int i = 0; i<lCount ; i++)
{
if(i == lCount-1)
printf("M%d=(%.2f,%.2f). Area%d =%.2f.\t\t\t\t\t\r", i+1, vx.dblVal, vy.dblVal, i+1, va.dblVal);
else if(i < lCount-1)
printf("M%d=(%.2f,%.2f) Area%d =%.2f. ", i+1, vx.dblVal, vy[i].dblVal, i+1, va[i].dblVal);
}
}
else if(choice == 2)
{
for(int i = 0; i<lCount ; i++)
{
if(i == lCount-1)
printf("M%d =(%.2f,%.2f).\t\t\t\t\r", i+1, vx[i].dblVal, vy[i].dblVal);
else if(i < lCount-1)
printf("M%d =(%.2f,%.2f). ", i+1, vx[i].dblVal, vy[i].dblVal);
}
}
hr = pFrame->Free();
hr = pFrame->Release();
}
pCamera->SetLED(NP_LED_ONE, VARIANT_FALSE);
pCamera->Stop();
pCamera->Close();
pCamera->Release();
}
-------------------------------------------------
Cheers
~Paul
Re: Unhandled exceptions randomly occuring
To avoid the exception you will want to make sure pFrame is not null before accessing the frame methods, as well as using the array index when modifying the vx,vy,va variables.
To keep the post short I will send you a PM with the updated code.
To keep the post short I will send you a PM with the updated code.
Re: Unhandled exceptions randomly occuring
We would also recommend adding a message pump into your main loop so that notifications within the OptiTrack SDK work properly. This can also be found in our C++ Win32 command line sample application.
Code: Select all
MSG msg;
while( PeekMessage( &msg, NULL, 0, 0, PM_NOREMOVE ) )
{
if(GetMessage( &msg, NULL, 0, 0 ) )
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
-
- Posts: 28
- Joined: Wed Apr 25, 2007 10:39 am
Re: Unhandled exceptions randomly occuring
Thats great. Thanks alot. Very much appreciated.
Paul Smith
Paul Smith