Roald87 / roald87.github.io

Mostly cooking and coding in TwinCAT.
https://cookncode.com
MIT License
4 stars 2 forks source link

twincat/2022/09/18/page-fault #47

Open utterances-bot opened 1 year ago

utterances-bot commented 1 year ago

Why am I getting a page fault in TwinCAT?

Earlier I talked about how you can prevent page faults from references. In this post, I try to show a complete overview of page fault origins and how to prevent them.

https://cookncode.com/twincat/2022/09/18/page-fault.html

trofimich commented 1 year ago

Page faults also occurs in case if:

1) If pointer is not zero, but points to invalid object: pa : POINTER TO ObjectA; b : ObjectB; pa := ADR(b); // address of incompatible object pa.MethodB(); // page fault here

or

a : ObjectA; pa1 : POINTER TO ObjectA; pa2 : POINTER TO ObjectB; pa1 := ADR(a); pa2 := ADR(pa1); // address of pointer instead of object pa2.Method(); // Page fault here

2) Repeated memory release or invalid address for memory release pa := POINTER TO ObjectA; pa := NEW(ObjectA); DELETE(pa); __DELETE(pa); // Page fault or even BSOD here

or

pa : PVOID; pa := 123456789; __DELETE(pa); // Page fault or BSOD here

Hope we will have TRY/CATCH in 4026 build and will have possibility to catch such problems.

trofimich commented 1 year ago

3) Access to array element out of lower or upper bound:

oa : ARRAY[1..5] OF ObjectA;

oa[0].Method(); // Invalid array index used

Roald87 commented 1 year ago

Thanks for the additions @trofimich!

rruiter87 commented 1 year ago

Another case from a reader

There is one more case, that I encountered two weeks ago. I had a function block with a VAR_IN_OUT variable and a method, that changed this variable. I called the method before function blocks instance was called for the first time and it caused the page fault exception.

For bonus points, I was working with CX7080, where you cannot log in after exception and my core dump file was damaged and couldn't be opened. There is a warning for changing VAR_IN_OUT from inside of a method, but well, my warnings were disabled. Took me a solid while to find it :)