C block memory error (0xc0000005) causing simulation crash and strange behavior (fluctuations, restart required)

I am using the C block feature in PLECS, but I’ve found that after starting the simulation it crashes without any warning, and I cannot see any waveforms. It seems the error occurs at the very first step of simulation. I checked the Windows event log and saw a memory error:

Exception code: 0xc0000005
Fault offset: 0x000002630f68018e

I believe this is caused by the code inside the C block, where I have implemented a lookup‑table based control algorithm.

After further investigation, it seems the problem occurs when a floating‑point number is converted to an integer (used as an array index) and that integer becomes very large, exceeding the array bounds. However, I already added validation checks in my code to prevent the floating‑point value from being too large. So I am not sure what is going wrong. I also tested the same C code in VSCode on my PC and did not see any similar error.

What is even more strange are two issues I observed during debugging:

  1. My code is placed inside a function fun_1. Inside this function, another function fun_2 is called to perform the lookup, and it returns a value (call it A). After some calculations, fun_2 outputs another value B. However, when I simply change the return value of fun_1 to a constant or some other intermediate variable, the crash disappears (note that I kept the rest of fun_1 unchanged, which theoretically should still cause the error).

  2. When I change the return value of fun_1 to the output of fun_2, and modify fun_2 to return a constant (while keeping the lookup table code inside fun_2), the crash does not occur. But when I observe this constant value on an output waveform, it fluctuates around the constant I set. And when I set a larger constant, the waveform increases and also fluctuates around that larger value. In theory it should be a fixed output with no fluctuation. Even stranger, when I comment out the lookup table code, the fluctuation still exists. Then after closing and reopening PLECS with exactly the same code and simulation file, the fluctuation disappears.

It seems that PLECS does not properly isolate the code inside the C block, and memory errors might be occurring. From a software design perspective, it should not be possible for erroneous C block code to affect the normal operation of the simulation host. Moreover, the execution logic and failure behavior of the C block do not match expectations, and after deleting the code the problem persists, requiring a restart of the software to clear it.

Therefore, I would like to know how to debug this kind of error in my code, because at this point I cannot be sure whether the problem is caused by my C block code or by a potential memory issue within PLECS. Also, the C block provides no console output or debugging capabilities. Additionally, some of the issues seem to depend on whether the software has been restarted.

Thank you for any advice.