❔ 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
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
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)
It's a bug
Will look at the ptr's again, ty.
How is it a bug?
You don't even know what they're doing
🙄
???
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
Or, you know, ask the C# discord server
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.
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
I have FNV-1 hashing with it, is that part of the issue?
I'm not sure what that is
Is that a custom algorithm? Does it come from some library?
Why would hashing require pointers?
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
access violations do not occur in correct code
so
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
It's a archive unpacker which uses offsets to extract data (bytes).
Can I just avoid the unsafe { } / fixed and pointers?
probably
there are so many battle tested packages on .net that you can do pretty much anything
without using pointers
well its a unknown file format kind of thing and no its not a online hack, as people may think. xD
oh?
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.