UIPanelObject using HTML and Javascript crashing with VarGet?

For topics related to the creation of simulation objects (SimObjects). This includes development of aircraft, ground, and maritime vehicles.
Post Reply
parabreton08
Posts: 1
Joined: Fri Oct 20, 2023 5:11 pm

UIPanelObject using HTML and Javascript crashing with VarGet?

Post by parabreton08 »

Hello, I have a problem with Prepar3D (version 5.3.17) that I just can't seem to troubleshoot my way out of.

I've narrowed down where in the code (javascript in the HTML) the error is happening. I have a simple UIPanelObject with an HTML page showing various types of information. I removed most functionality from the loop statement containing only the code that causes the crash.
At runtime, the program works great; however, when reloading the scenario, closing the scenario, or loading a different scenario the program will crash.

The Event Viewer gives Exception code: 0xc0000005, which I believe there is an attempt to write to RAM in an area where we no longer have permission. This has not been very helpful in my troubleshooting.

Here is the offending bit of javascript:

Code: Select all

	function loop(timestamp)
	{
		init();

		var leftInput = VarGet("L:KNL-Left", "number");
		var rightInput = VarGet("L:KNL-Right", "number");

		window.requestAnimationFrame(loop);
	}
	window.requestAnimationFrame(loop); // Starts first call of loop
Like I have stated above, this works great at runtime, but when reloading, closing, or loading a different scenario it crashes.
NOW, the weird bit I have found, is when I remove the act of saving the results of VarGet but still call it,
the crashing disappears.
Example below:

Code: Select all

	function loop(timestamp)
	{
		init();

		VarGet("L:KNL-Left", "number");
		VarGet("L:KNL-Right", "number");

		window.requestAnimationFrame(loop);
	}
	window.requestAnimationFrame(loop); // Starts first call of loop
I thought saving variables was giving me the memory error, but alas, that is also not the case.
Without saving the information of VarGet I tried to call that function everywhere in my code where I would have called the variable I saved it to (inefficient, i know). This reintroduces the crashing, which leads me to believe that it is the API in P3D that is causing the crash, but only when I use these functions.

Another bit of information that leads me to believe this is when I try using another function of the Javascript API IsSimPaused(), it crashes in the exact same way.

Code: Select all

	function loop(timestamp)
	{
		init();

		if (IsSimPaused())
		{
			// Empty!
		}

		window.requestAnimationFrame(loop);
	}
	window.requestAnimationFrame(loop); // Starts first call of loop

Other things I have tried:
  • Encasing the VarGet calls in a try catch statement.
  • Checking if the function VarGet exists before calling it everytime
Is there is a way in the API or in the SimDirector nodes to detect when the program is closing, I could stop the loop before the program closes to avoid the crashing.

Has anyone else experienced this type of issue, or know other troubleshooting steps I could take?
Thank you for reading about my problem and I'll take any help I can get.
User avatar
Rob McCarthy
Lockheed Martin
Posts: 3704
Joined: Wed Aug 24, 2011 1:37 pm

Re: UIPanelObject using HTML and Javascript crashing with VarGet?

Post by Rob McCarthy »

Hello,

Does replacing the window.requestAnimationFrame line with this resolve the issue:

function UpdateHTML()
{
loop();
}

UpdateHTML is called from Prepar3D when the UIPanel window updates.

Regards,
Rob McCarthy
Rob McCarthy
Prepar3D® Core Lead
Post Reply