❔ System.AccessViolationException: 'Attempted to read or write protected memory.

Even with <AllowUnsafeCodeBlocks> enabled and using unsafe { }, this error does not go away. And before anyone asks, no this is not a cheat app or related to online gaming. Is this a limitation of C#? Thanks! System.AccessViolationException: 'Attempted to read or write protected memory. This is often an indication that other memory is corrupt.'
20 Replies
ero
ero3y ago
It's not a limitation and has nothing to do with allowing unsafe code or not You're just reading or writing protected memory So perhaps a null pointer, or some other address is incorrect $details
MODiX
MODiX3y ago
When you ask a question, make sure you include as much detail as possible. Such as code, the issue you are facing, and what you expect the result to be. Upload code here https://paste.mod.gg/ (see $code for more information on how to paste your code)
phaseshift
phaseshift3y ago
It's a bug
Temporal Nightmare
Will look at the ptr's again, ty.
ero
ero3y ago
How is it a bug? You don't even know what they're doing
phaseshift
phaseshift3y ago
🙄
ero
ero3y ago
???
Temporal Nightmare
First time I'm using ptr's in C# so I'm sure it's a PEBKAC error. 🙂 I will toy with it more, maybe ask google/chatgpt
ero
ero3y ago
Or, you know, ask the C# discord server
Temporal Nightmare
True, that would help. How do you make sure a ptr doesn't touch protected ram addresses? It's basically reading a file into memory and stuff.
ero
ero3y ago
I mean that shouldn't really ever happen if your code is sane To read file contents into memory, use File.ReadAllText or ReadAllBytes No reason for pointers
Temporal Nightmare
I have FNV-1 hashing with it, is that part of the issue?
ero
ero3y ago
I'm not sure what that is Is that a custom algorithm? Does it come from some library? Why would hashing require pointers?
Temporal Nightmare
1 sec Maybe I don't need them, it's a habit I picked up from C++ XD I wanted to sticky a variable to a part of memory, which is what caused this issue in the first place. :x
jcotton42
jcotton423y ago
access violations do not occur in correct code so
Henkypenky
Henkypenky3y ago
even if u use unsafe or allow unsafe blocks or whatever it won't go away, your code is trying to read or write unallocated memory perhaps (can't know because you didn't give much details) but it is safe to say that this is happening because your code is unmanaged and unsafe, it will never happen in managed safe code your best bet is to run the debugger step by step and see where you went wrong or correctly allocate memory, who Ok
Temporal Nightmare
It's a archive unpacker which uses offsets to extract data (bytes). Can I just avoid the unsafe { } / fixed and pointers?
Henkypenky
Henkypenky3y ago
probably there are so many battle tested packages on .net that you can do pretty much anything without using pointers
Temporal Nightmare
well its a unknown file format kind of thing and no its not a online hack, as people may think. xD oh?
Accord
Accord3y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.

Did you find this page helpful?