1

While grabbing images (Image type) to convert them into "Base64 String" in a kind of loop, im always getting this error at a random time of the execution.

"Unhandled Exception: System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt."

        private void doCapture()
        {
            CaptureInfo.CaptureFrame();
        }

        void CaptureInfo_FrameCaptureComplete(PictureBox Frame)
        {
            string str = toB64img(Frame.Image);
            //do something with the string
            this.doCapture();
        }

        private string toB64img(Image image)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                // Convert Image to byte[]
                image.Save(ms, ImageFormat.Png);   <==== error HERE
                byte[] imageBytes = ms.ToArray();
                // Convert byte[] to Base64 String
                string base64String = Convert.ToBase64String(imageBytes);
                return base64String;
            }
        }

Image come from directx.capture, webcam capture. (working fine) I assume that's because something is still in access and wasn't closed yet, so error cause already in use. But how can i fix this issue please?

8
  • The pattern of Disposing of the passed in image is not great as you don't know what else may be trying to use it. Commented Feb 18, 2015 at 12:28
  • i added that .dipose() while trying to get ride of the error. Didn't work . im editing to remove that now, so we keep concentrate on the real error so ^^ thanks Commented Feb 18, 2015 at 12:30
  • I think you are going to need to shed a bit more light on where the image is coming from for us to help. Commented Feb 18, 2015 at 12:33
  • 2
    It look like a threading problem. Where image comes from? Please provide a code. Commented Feb 18, 2015 at 12:35
  • 4
    The cause of the error is most likely not in the code you posted. I'd guess that the image has been corrupted before this code gets called. Do you have any unsafe code in your application? Commented Feb 18, 2015 at 12:36

1 Answer 1

3

i was right in my though. Something wasn't freed fast enought. Adding a force GC check right before the RETURN of the function, worked like a charm.. strange.

private string toB64img(Image image)
    {
        using (MemoryStream ms = new MemoryStream())
        {
            // Convert Image to byte[]
            image.Save(ms, ImageFormat.Png);   
            byte[] imageBytes = ms.ToArray();
            // Convert byte[] to Base64 String
            string base64String = Convert.ToBase64String(imageBytes);
            GC.Collect();                       <======
            GC.WaitForPendingFinalizers();      <======
            GC.Collect();                       <======
            return base64String;
        }
    }

``

Sign up to request clarification or add additional context in comments.

1 Comment

may i understand why someone just neg reped me... i found a solution i could't find anywhere on the web yet, shared it incase someone else experience the same another day. i dont think it is a bad move much "lol"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.