I have been looking around for a while trying to find an answer for this and really just can't wrap my head around read/write memory at all. I thought read/write memory is more for C++ because you manage it there where as c# usually takes care of it. None the less, below is a pastebim link to a function I created named DrawLevelCard. The purpose of this is to make a leveling card for my discord. The problem is sometimes it throws an error on the line that says:
var measure3 = gr.MeasureString(" / " + ExpNeeded + " Exp", Levelfont);
Error:
An unhandled exception of type 'System.AccessViolationException' occurred in System.Drawing.Common.dll
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
This does not occur on any other line and if I remove it all together, I haven't been able to replicate it without it. The problem is this is kinda necessary for the way I have setup the image. PasteBin: https://pastebin.com/ab00GLLs
Card Image for Reference: https://i.stack.imgur.com/uuUx3.png
UPDATE: Here is the full class if someone needs extra information :) https://pastebin.com/Bbi7zt2b
Thanks to @MitchWheat's Reply, I think I managed to find a solution. I will approve this solution if it turns out true. I think this is caused by the
GetFontName()
adding a font file to the collection multiple times. This was something I completely forgot to fix earlier.I am calling this multiple times and if its currently being read then it probably flags
UPDATE: Somehow I managed to resolve the issue by instead of setting the function to a variable, setting the funtion to run everytime the measurestring was ran.
var measure3 = gr.MeasureString(" / " + ExpNeeded + " Exp", GetFontName(16));
I do not know why this fixed the issue or even If it fully did or not. If the issue comes back I will make sure to update this post. Hopefully someone can give me and others some insight on why this is(was) occurring.Here is the updated class if someone wants to compare them : https://pastebin.com/r92zKjhW