March 5, 201016 yr Commercial Member Anyone have an any insight into this error?Unhandled exception at 0x20b3e12e in fsx.exe: 0xC0000005: Access violation reading location 0x00000000.I have a block of code that triggers a bunch of Execute Calculator Code commands in sequence with a one second delay between each event. The section runs in its own separate thread (not sure if that's somehow a factor).Note that I'm writing, not reading stuff with these commands.execute_calculator_code("1 (>L:Blah1,bool)",NULL,NULL,NULL);*1 second delay*execute_calculator_code("1 (>L:Blah2 ,bool)",NULL,NULL,NULL);*1 second delay*execute_calculator_code("1 (>L:Blah3,bool)",NULL,NULL,NULL);*1 second delay*execute_calculator_code("1 (>L:Blah4 ,bool)",NULL,NULL,NULL);and so on all the way down to Blah10...The debugger in VS2008 always point to one of these Execute Calculator Code commands.The weird thing is that it doesn't point to the same one everytime.It's "random" as to which one will trigger the exception.Any ideas?Cheers, B. York FS2Crew Web Site / FS2Crew Facebook Page / FS2Crew Discord
March 5, 201016 yr Commercial Member The thread is most likely the factor. Ed Wilson Mindstar AviationMy Playland - I69
March 5, 201016 yr Author Commercial Member The thread is most likely the factor.But why? B. York FS2Crew Web Site / FS2Crew Facebook Page / FS2Crew Discord
March 5, 201016 yr Commercial Member That function is not even remotely thread-safe. Ed Wilson Mindstar AviationMy Playland - I69
March 6, 201016 yr Are you calling execute_calculator_code() anywhere else than in that thread? Gerry Howard
March 6, 201016 yr Author Commercial Member Are you calling execute_calculator_code() anywhere else than in that thread?Yes, but they're not called at the same time. Cheers, B. York FS2Crew Web Site / FS2Crew Facebook Page / FS2Crew Discord
March 6, 201016 yr Commercial Member Threads can execute simultaneously, so if it's being used in two separate threads... yes, they can indeed be called at the same time. Ed Wilson Mindstar AviationMy Playland - I69
March 6, 201016 yr Yes, but they're not called at the same time. Cheers,What are you using to prevent them being called at the same time - semaphores, signals...? Gerry Howard
March 6, 201016 yr Author Commercial Member It's definitely the thread that's causing the issue.I took the block of code that contains the execute calc code out of the separate thread and put it in the main (regular) section, and no more crashes.However, I'd really like to keep the Exec Calc Code stuff in a separate thread. Is there anyway of bullet proofing it somehow to prevent this odd crash? What are you using to prevent them being called at the same time - semaphores, signals...?Here's what the code looks like.... note I had to hide the L VAR names to protect the innocent :-)SafeSleep produces a pause of 1 second...if ( setup_cold_and_dark == 1) { if (Safe_Sleep(1000) != 0){return;} // MUTE BUTTON ON execute_calculator_code("1 (>L:XXXX,bool)",NULL,NULL,NULL); if (Safe_Sleep(1000) != 0){return;} execute_calculator_code("1 (>L:XXXXX,bool)",NULL,NULL,NULL); if (Safe_Sleep(1000) != 0){return;} execute_calculator_code("0 (>L:XXXXX,bool)",NULL,NULL,NULL); ... and so on B. York FS2Crew Web Site / FS2Crew Facebook Page / FS2Crew Discord
March 6, 201016 yr You could try using critical Section objects which can only be used by one thread at a time. This is perhaps(?) the simplest way but not necessarily the most effective depending on the application. Also, I think the function Sleep() which suspends the thread would be for efficient than your loop. I've only used threads in BorlandC++ Builder which provides wrappers for the Win API so there is the usual health warning.// Declare GLOBALLYCRITICAL_SECTION cs;// Called during initialisationInitializeCriticalSection(&cs);// In Thread 1...EnterCriticalSection(&cs);// Code to be protectedLeaveCriticalSection(&cs);// In Thread 2....EnterCriticalSection(&cs);// Code to be protectedLeaveCriticalSection(&cs);// Called during finalisationDeleteCriticalSection(&cs) Gerry Howard
March 6, 201016 yr Commercial Member Using critical sections has one drawback: It literally suspends access to the program's memory (code/data) for everything else except that thread while locked. If your thread has timing issues... it's going to show up in the primary thread as stutters and pauses. Ed Wilson Mindstar AviationMy Playland - I69
March 6, 201016 yr Using critical sections has one drawback: It literally suspends access to the program's memory (code/data) for everything else except that thread while locked. If your thread has timing issues... it's going to show up in the primary thread as stutters and pauses.Can you clarify? Do you mean, for example, that if a process has 3 threads and Thread 1 is in a critical section and Thread 2 is suspended waiting for the critical section that Thread 3 has no access to the program's memory? Gerry Howard
March 6, 201016 yr Commercial Member Can you clarify? Do you mean, for example, that if a process has 3 threads and Thread 1 is in a critical section and Thread 2 is suspended waiting for the critical section that Thread 3 has no access to the program's memory?In essence, correct. A critical lock will suspend other process threads until it's removed. Ed Wilson Mindstar AviationMy Playland - I69
March 6, 201016 yr In essence, correct. A critical lock will suspend other process threads until it's removed.I don't understand. According to Microsoft:"When a critical section object is owned, the only other threads affected are the threads that are waiting for ownership in a call to EnterCriticalSection. Threads that are not waiting are free to continue running."http://msdn.microsoft.com/en-us/library/ms682530(VS.85).aspx Gerry Howard
March 7, 201016 yr Commercial Member Send me one year's worth of tuition fees for computer programming and I'll be more than happy to educate. Seriously.I know what the EnterCriticalSection does and what can go seriously wrong if you don't release it. Try it sometime. See how the main thread likes that.But then, you're using the Borland threading system... which isn't actually the proper method of using threads. It actually cheats. Ed Wilson Mindstar AviationMy Playland - I69
Create an account or sign in to comment