Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access violation #77

Open
ga5tan opened this issue Oct 31, 2023 · 7 comments
Open

Access violation #77

ga5tan opened this issue Oct 31, 2023 · 7 comments

Comments

@ga5tan
Copy link

ga5tan commented Oct 31, 2023

Hi

I tried to run your Sample Master as a thread from WinForm app and I am getting Access Violation Exception

image

I run the Master as an async Task before form pops up

image

The idea is to run HMI and controller as separate tasks/processes, so the HMI does not freeze...

Any idea what could be the problem?
I know that reading of variables is in unsafe{} block, but it didnt seem that unsafe (meaning it worked), when it was running in your original Sample

thx
--G

@Apollo3zehn
Copy link
Owner

So it is working if you just use the sample master and it fails if you plug it into a Windows Forms app? I don't know what could be the reason for the access violation exception. I would first ensure that the pointer (varErrorCode.DataPtr) is non-zero. If that is the case, everything should be fine. EtherCAT.NET allocates the memory for the I/O map itself and access to that map should be safe. Only if the pointer is 0, things should go bad. Or if the pointer calculation is bugged somehow ...

@ga5tan
Copy link
Author

ga5tan commented Nov 2, 2023

So it is working if you just use the sample master and it fails if you plug it into a Windows Forms app?

that is correct.
DataPtr is nonzero:
0.) 'Error code', Idx: '603Fh', DataPtr: '16138129'
0.) 'Statusword', Idx: '6041h', DataPtr: '16138131'
0.) 'Modes of operation display', Idx: '6061h', DataPtr: '16138133'
0.) 'Position actual value', Idx: '6064h', DataPtr: '16138134'
0.) 'Touch probe status', Idx: '60B9h', DataPtr: '16138138'
0.) 'Touch probe pos1 pos value', Idx: '60BAh', DataPtr: '16138140'
0.) 'Following error actual value', Idx: '60F4h', DataPtr: '16138144'
0.) 'Digital inputs', Idx: '60FDh', DataPtr: '16138148'
4.) 'Controlword', Idx: '6040h', DataPtr: '16138120'
4.) 'Modes of operation', Idx: '6060h', DataPtr: '16138122'
4.) 'Target position', Idx: '607Ah', DataPtr: '16138123'
4.) 'Touch probe function', Idx: '60B8h', DataPtr: '16138127'

First comes this exception
Exception thrown: 'System.InvalidOperationException' in mscorlib.dll

and then (often on _actualWorkingCounter = EcHL.UpdateIo(this.Context, out _dcTime);)
Exception thrown: 'System.AccessViolationException' in EtherCAT.NET.dll
An unhandled exception of type 'System.AccessViolationException' occurred in EtherCAT.NET.dll
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

@ga5tan
Copy link
Author

ga5tan commented Nov 12, 2023

And it's not only in SampleMaster where access violation happens. it's also in EcMaster.cs:

image

@ga5tan
Copy link
Author

ga5tan commented Nov 16, 2023

Found a null pointer:

image

@Apollo3zehn any idea why context is null?

@ga5tan
Copy link
Author

ga5tan commented Nov 20, 2023

I inserted the code into WPF app, thinking maybe WinForms somehow collides with SOEM dlls, but the problem is the same. Context is null, and UpdateIO fails with access violation.
Anyone reading this, do you have any suggestions as what to try ?

@Apollo3zehn
Copy link
Owner

Sorry, it is hard for me to find the root cause of this. Unfortunately I am also quite busy right now :-( To debug the problem I would first try to reproduce it and then find out why context is null, e.g. by setting a breakpoint on that particular function or a little above in the call stack. This gets easier when the native DLLs are compiled in debug mode with optimizations disabled.

@Tmoney2014
Copy link
Contributor

I had same ex
and I did change Thread.sleep -> await Task.Delay()

and It working

I hope, you did find your way to work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants