System.AccessViolationException

Christ Kennedy 41 Reputation points
2022-04-06T22:13:32.877+00:00

I have a background worker that takes in a parameter which it uses to create a RichTextFile it needs to draw a bitmap representation of a word in the described RichTextFile. This background worker works 99 times out of a 100 and then it intermittently has this error :

System.AccessViolationException
HResult=0x80004003
Message=Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Source=<Cannot evaluate the exception source>
StackTrace:
<Cannot evaluate the exception stack trace>

here is my source code

 class classRTX_Data
        {
            public Size Size = new Size();
            public string Rtf = "";
            public int SelectionStart = 0;
            public int SelectionLength = 0;
            public int ScrollCaretIndex = 0;
            public int RightMargin = 0;
            public classRTX_Data(ref RichTextBox ckRTX)
            {
                Size = ckRTX.Size;
                Rtf = ckRTX.Rtf;
                SelectionStart = ckRTX.SelectionStart;
                SelectionLength = ckRTX.SelectionLength;
                RightMargin = ckRTX.RightMargin;
                ScrollCaretIndex = ckRTX.GetCharFromPosition(new Point(0, 0));
            }

            public RichTextBox fromArgs(ref RichTextBox rtxRetVal)
            {
                if (rtxRetVal == null) return null;
                rtxRetVal.Size = Size;
                rtxRetVal.Rtf= Rtf;
                rtxRetVal.SelectionStart = SelectionStart;
                rtxRetVal.SelectionLength = SelectionLength;
                rtxRetVal.RightMargin = RightMargin;
                rtxRetVal.Select(ScrollCaretIndex, 0);
                rtxRetVal.ScrollToCaret();

                return rtxRetVal;
            }
        }

        private void BckDrawWordMap_DoWork(object sender, DoWorkEventArgs e)
        {
            RichTextBox rtxCopy = new RichTextBox();
            RichTextBox rtxCopy_Text = new RichTextBox();

            Point ptTopChar = new Point();
            Point ptLastChar = new Point();

            {
                try
                {
                    classRTX_Data cRtxData = (classRTX_Data)e.Argument;

                    cRtxData.fromArgs(ref rtxCopy_Text);
                    cRtxData.fromArgs(ref rtxCopy);

                    Bitmap bmpDraw = null;
                    if (rtxCopy.Text.Length != 0 && strWord.Length > 0)
                    {
                        rtxCopy_Text.Text = rtxCopy_Text.Text.ToUpper();
                        try
                        {
                            ptTopChar = rtxCopy.GetPositionFromCharIndex(0);                                         // error occurs here or next line
                            ptLastChar = rtxCopy.GetPositionFromCharIndex(rtxCopy.Text.Length - 1);
                        }
                        catch (Exception)
                        {
                            bolBackgroundWorker_RunAgain = true;
                            return;
                        }

                        string strLastChar = rtxCopy.Text[rtxCopy_Text.Text.Length - 1].ToString();

                        Size szLastChar = TextRenderer.MeasureText(strLastChar, rtxCopy.Font);

                        Rectangle recSource = new Rectangle(0, 0, rtxCopy.Width, ptLastChar.Y - ptTopChar.Y + szLastChar.Height);

                        double dblFactorDraw = (double)szPic.Height / (double)recSource.Height;
                        Size szTextSource = TextRenderer.MeasureText(strWord, rtxCopy.Font);
                        Size szDrawRec = new Size((int)(szTextSource.Width * dblFactorDraw), (int)(szTextSource.Height * dblFactorDraw));

                        if (szDrawRec.Width < 3)
                            szDrawRec.Width = 3;
                        if (szDrawRec.Height < 2)
                            szDrawRec.Height = 2;
                        Size szBitmp = new Size((int)(recSource.Width * dblFactorDraw), (int)(recSource.Height * dblFactorDraw));

                        if (szBitmp.Width < 3) szBitmp.Width = 3;
                        if (szBitmp.Height < 3) szBitmp.Height = 3;

                        bmpDraw = new Bitmap(szBitmp.Width, szBitmp.Height );
                        int intCounter = 0;
                        using (Graphics g = Graphics.FromImage(bmpDraw))
                        {
                            g.FillRectangle(Brushes.White, new RectangleF(0, 0, bmpDraw.Width, bmpDraw.Height));
                            int intIndex = rtxCopy_Text.Text.IndexOf(strWord);
                            while (intIndex >= 0)
                            {
                                intCounter++;
                                char chrBefore = intIndex > 0
                                                          ? rtxCopy_Text.Text[intIndex - 1]
                                                          : ' ';
                                char chrAfter = intIndex + strWord.Length < rtxCopy_Text.Text.Length - 2
                                                                          ? rtxCopy_Text.Text[intIndex + strWord.Length]
                                                                          : ' ';

                                if (!Char.IsLetter(chrBefore) && !Char.IsLetter(chrAfter))
                                {
                                    Point ptStart = rtxCopy.GetPositionFromCharIndex(intIndex);
                                    Point ptDraw = new Point((int)(dblFactorDraw * ptStart.X), (int)(dblFactorDraw * (ptStart.Y - ptTopChar.Y)));
                                    g.FillRectangle(Brushes.Red, new Rectangle(ptDraw, szDrawRec));
                                }

                                try
                                {
                                    intIndex = rtxCopy_Text.Text.IndexOf(strWord, intIndex + 1);
                                }
                                catch (Exception)
                                {

                                    intIndex = -1;
                                }
                            }
                        }
                        bmpDraw.MakeTransparent(Color.White);
                    }
                    _bmpMapBase = bmpDraw;
                }
                catch (Exception error)
                {
                }
            }

            rtxCopy.Dispose();
            rtxCopy_Text.Dispose();
        }
  • I've tried adding an extra try-catch specifically where it occurs but even inside 2 Try-Catches it still stalls during runtime (debug mode) at the same two lines of code where rtxCopy.getPositionFromCharIndex() is called.
  • I've tried changing the Target Platform from "Any CPU" to "x86" as it was suggested in another post and that has not solved the problem.

the error message says "This is often an indication that other memory is corrupt." would this be an issue with my laptop alone ? or is my code the source of the memory corruption?

Developer technologies C#
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.