locked
An unhandled exception of type 'System.AccessViolationException' occurred in System.Drawing.dll Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt. RRS feed

  • Question

  • Hello,

    Actually, iam grabbing image using third-party <g class="gr_ gr_43 gr-alert gr_spell gr_inline_cards gr_disable_anim_appear ContextualSpelling" data-gr-id="43" id="43">dlls</g> and when I am trying to render the image I get this error. Don't understand what is causing this error. <g class="gr_ gr_314 gr-alert gr_gramm gr_inline_cards gr_disable_anim_appear Grammar only-ins doubleReplace replaceWithoutSep" data-gr-id="314" id="314">Code</g> is given below.

                                                                                     

    Bitmap grbImg;

      void VideoPlayThread()
            {
                Bitmap TestAGC = null;
                int Mean = 0; StreamWriter sw = new StreamWriter(Environment.CurrentDirectory + "//Test.dat");
                try
                {
                    while (true)
                    {
                        lock (lockTerminatethread)
                        {
                            if (terminateThread)
                                break;
                        }

                        if (DeviceSet && !IsSelecting)
                        {
                            grbImg = null;
                            
                            if (VideoStream.GrabImageFromDevice(ref grbImg) && (grbImg != null) && GrabCont)
                            {
                                GrabCont = false;
                                IsPlaying = true; ErrorCount = 0;

                             
                                if (IsAutoAGCChecked)
                                {
                                    lock (lockVideoGrabImg)
                                    {
                                        checkIntensity(grbImg);

                                        if ((TestAGC = AutoAGC(grbImg)) != null)
                                            grbImg = TestAGC;
                                        Thread.Sleep(10);
                                    }
                                }



                                if (IsPlotCheckBoxChecked == true)
                                {
                                    if (PlotOXYMeanplot.Count == 255)
                                    { PlotOXYMeanplot.Dequeue(); }

                                    lock (lockVideoGrabImg)
                                    {
                                        sw.WriteLine("Plot Calculated " + DateTime.Now.ToString("mm:ss.FFF"));
                                        PlotOXYMeanplot.Enqueue(new DataPoint(OxyFrameCount++, Mean = grbImg.GetPixel(3, 2).R));

                                        Thread.Sleep(10);
                                    }

                                    this.Dispatcher.Invoke(() => {
                                        MainPlot.InvalidatePlot(true);
                                        labelMean.Content = $"Mean= {Mean}";//labelIntTime.Content = $"Int time= {intTime}";
                                        plotView.ItemsSource = PlotOXYMeanplot.AsEnumerable<DataPoint>();
                                        MainPlot.InvalidatePlot(false);
                                    });

                                lock (LockFPS)
                                {
                                    FrameCount++;
                                }

                                

                                ImageBoxVideo.Dispatcher.Invoke(() =>
                                {

                                    lock (lockVideoGrabImg)
                                    {
                                        var handle = grbImg.GetHbitmap();
                                        ImageBoxVideo.Source = Imaging.CreateBitmapSourceFromHBitmap(handle, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
                                        sw.WriteLine("Image Rendered " + DateTime.Now.ToString("mm:ss.FFF"));
                                        GrabCont = true;
                                    }

                                });
                                //GrabCont = true;
                            }
                            else if (++ErrorCount == 100)
                            {
                                ErrorCount = 0;
                                break;
                            }

                        }

                    }
                }
                catch (Exception ex)
                {
                    sw.Close();
    #if DEBUG
                    throw;
    #endif

                  
                }
                finally
                {
                    StartBLC = false;
                    BLCFrameCount = 0;
                    BLCTask = false; DidThreadTerminate = true;
                    ImageBoxVideo.Dispatcher.Invoke(new Action(delegate
                    {
                        ImageBoxVideo.Source = ImageSourceForBitmap(Properties.Resources.EagleVideoScreen);
                    }));
                }

            }

    Error is random and occurs at Imaging.CreateBitmapSourceFromHBitmap(handle, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());and at PlotOXYMeanplot.Enqueue(new DataPoint(OxyFrameCount++, Mean = grbImg.GetPixel(3, 2).R));

    • Moved by Bob Ding Friday, March 2, 2018 5:38 AM third part source related
    Wednesday, February 28, 2018 6:24 AM

All replies

  • Except for development and testing purposes avoid using Sleep to synchronize stuff. I don't know if that is relevant here you should use a more accurate and reliable way to do the synchronization.


    Sam Hobbs
    SimpleSamples.Info

    Wednesday, February 28, 2018 6:49 PM
  • Hi,

    The access violation may be caused by a third-party DLL, asking on the DLL support forum is probably the best place.

    This forum is not for debugging, you use some debug tool like Windbg to analyze your app, then if have any issues regarding Windows Debugging then asking on Windows Desktop Debugging forum.

    Best Regards,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, March 1, 2018 8:44 AM