TT_update() throws exception

Post Reply
baggepinnen
Posts: 9
Joined: Tue Oct 30, 2012 6:03 pm

TT_update() throws exception

Post by baggepinnen » Mon Jan 28, 2013 3:09 am

Hello!

Sometimes, calls to API function TT_update() throws an exception. Even though I wrap the call in a try/catch statement, the exception is not always caught. All info I get is
Unhandled exception at 0x12fde062 in MainProgram.exe: 0xC0000005: Access violation reading location 0x00000026.
My code for connecting to the tracker, (which now takes 7 seconds to run) follows

Code: Select all

		int Connect()
		{
			if(TT_Initialize() != NPRESULT_SUCCESS)
			{return -1;}
			while(TT_CameraCount()==0)
			{
				TT_Update();
				Sleep(5);
			}
			if (TT_LoadProject("D:\\GUIs\\project.ttp") != NPRESULT_SUCCESS)
			{return -2;}

			if(TT_LoadTrackables("D:\\GUIs\\Trackables.tra") != NPRESULT_SUCCESS)
			{return -3;}
			return 1;
		}
Any suggestions on what might be the cause? This call has worked before and I can not remember changing anything that could case an error to occur. We are using 6st FLEX13 cameras and I have tried with varying sampling intervals between 10ms and 40 ms without success.

Thankful for help
Fredrik

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

Re: TT_update() throws exception

Post by beckdo » Mon Jan 28, 2013 1:03 pm

Hm, that call should not result in an exception. You use the above code to connect and load your project and trackables and then your main loop calls update?

One thing to check is in your project settings C/C++ Code Generation->Run-Time. I believe you'll want to use the static non-DLL version of the runtime to prevent different memory managers begin used. However that's just a hunch.

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

Re: TT_update() throws exception

Post by beckdo » Mon Jan 28, 2013 1:04 pm

Let me know if you have a simple sample app that crashes reliably and I'll take a look.

baggepinnen
Posts: 9
Joined: Tue Oct 30, 2012 6:03 pm

Re: TT_update() throws exception

Post by baggepinnen » Mon Jan 28, 2013 5:53 pm

My call to update occurs every time a timer has elapsed, so not in a loop. Maybe I should mention that I am writing my main program in C# whereas the calls to the TT API is done through a C++ wrapper.

When I check 'project settings C/C++ Code Generation', there is no option called simply 'runtime'. Under the option 'Runtime Library' I can choose 'Multi-threaded DLL' or 'Multi-threaded', the DLL version being used currently. The wrapper I have written is supposed to be compiled into a DLL which is loaded by the main program upon request from the user. If I choose the option called 'Multi-threaded', the compiler generates the following error
error D8016: '/clr' and '/MTd' command-line options are incompatible

baggepinnen
Posts: 9
Joined: Tue Oct 30, 2012 6:03 pm

Re: TT_update() throws exception

Post by baggepinnen » Mon Jan 28, 2013 6:08 pm

After putting a breakpoint at the call to TT_update() and running over it a few times, the following output was generated
First-chance exception at 0x0e7bb9dc in MainProgram.exe: 0xC0000005: Access violation reading location 0x000000b0.
First-chance exception at 0x0e7bb9dc in MainProgram.exe: 0xC0000005: Access violation reading location 0x000000b0.
A first chance exception of type 'System.AccessViolationException' occurred in TTwrapper.dll
A first chance exception of type 'System.AccessViolationException' occurred in TTwrapper.dll
The thread 'Win32 Thread' (0xe40) has exited with code 0 (0x0).
The thread 'Win32 Thread' (0xf78) has exited with code 0 (0x0).

Unhandled Exception: System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
at TT_Update()
at TrackerCPP.TrackerCpp.Update() in d:\mainprogram\ttwrapper\ttwrapper.cpp:line 80
at OptiTrack.OptiTrack.Log(Object sender, ElapsedEventArgs e) in D:\MainProgram\OptiTrack\OptiTrack.cs:line 154
at System.Timers.Timer.MyTimerCallback(Object state)
at System.Threading._TimerCallback.TimerCallback_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading._TimerCallback.PerformTimerCallback(Object state)
The thread 'Win32 Thread' (0x958) has exited with code 0 (0x0).
The thread 'Win32 Thread' (0x1304) has exited with code 0 (0x0).
The thread 'Win32 Thread' (0x69c) has exited with code 0 (0x0).
The thread 'Win32 Thread' (0x17a0) has exited with code 0 (0x0).
The thread 'Win32 Thread' (0x14ec) has exited with code 0 (0x0).
The thread 'Win32 Thread' (0xf28) has exited with code 0 (0x0).
...a few more threads have exited with code 0...
The program '[2132] MainProgram.exe: Native' has exited with code 0 (0x0).
The program '[2132] MainProgram.exe: Managed (v4.0.30319)' has exited with code 0 (0x0).

The structure of my program is a Main program written in C# making calls to a C# object of type OptiTrack, a class written in C#, the C# class internally has an object of type TrackerCpp which is a wrapper around the API, written in C++. The OptiTrack C# class has a timer running an whenever it has elapsed, the call to update occurs.

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

Re: TT_update() throws exception

Post by beckdo » Tue Jan 29, 2013 10:30 am

Yeah, it looks like you're just linking to the wrong Runtime Library. That will likely cause this. I will look into this.

baggepinnen
Posts: 9
Joined: Tue Oct 30, 2012 6:03 pm

Re: TT_update() throws exception

Post by baggepinnen » Tue Jan 29, 2013 6:20 pm

I think I may have solved the problem.
If my theory is correct, there were a conflict between the thread executing the timer callback and the thread owning the Tracker object, causing the access violation exception to be thrown. Calling the TT_update() in a C# BackgroundWorker did not seem to work either. Spawning a new thread, which periodically polled the API using TT_update() and sleeping in between calls however seem to work well, and the problem no longer occurs. Even though the problem no longer occurs, it would be interesting to know why this new thread can pull it of, while the timer callback thread could not.

Thanks for your help and interest in this matter,
Fredrik

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

Re: TT_update() throws exception

Post by beckdo » Wed Jan 30, 2013 12:22 pm

Hey Fredrik,

Just a follow-up. Even though our current library uses the static runtime library you should be fine.

With regards to calls into the Tracking Tools API, you should make calls from a single thread into the API otherwise you will potentially have problems.

Post Reply