Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Double Buffering question

Double Buffering question

Scheduled Pinned Locked Moved .NET (Core and Framework)
graphicsquestiondockerhelp
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    ricecake
    wrote on last edited by
    #1

    I am trying to implement double buffering in a form, and I am getting a crash. I have a fix for the crash but I don't know if it's "correct". To enable double buffering, I call this function from the form's constructor:

    System::Void enable_double_buffering()
    {
    using System::Windows::Forms::ControlStyles;

    SetStyle(static\_cast<ControlStyles>(ControlStyles::DoubleBuffer |
                                        ControlStyles::UserPaint |
                                        ControlStyles::AllPaintingInWmPaint),
             true);
    UpdateStyles();
    

    }

    My Paint handler looks like this:

    System::Void PlotDisplayWindow_Paint(System::Object* /*sender*/, System::Windows::Forms::PaintEventArgs* e)
    {
    using System::Drawing::Graphics;

    Graphics\* g = e->Graphics;
    // draw an image
    g->Dispose();
    

    }

    If I do not enable double buffering, then it works fine, but with flickering. With double buffering, I get the following exception:

    ************** Exception Text **************
    System.ArgumentException: Invalid parameter used.
    at System.Drawing.Graphics.EndContainer(GraphicsContainer container)
    at System.Windows.Forms.DibGraphicsBufferManager.ReleaseBuffer(GraphicsBuffer buffer)
    at System.Windows.Forms.GraphicsBuffer.Dispose()
    at System.Windows.Forms.Control.WmPaint(Message& m)
    at System.Windows.Forms.Control.WndProc(Message& m)
    at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
    at System.Windows.Forms.ContainerControl.WndProc(Message& m)
    at System.Windows.Forms.Form.WndProc(Message& m)
    at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
    at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
    at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

    If I comment out the g->Dispose(); line in the Paint handler, then I do not get the crash. Is it correct/acceptable not to dispose of the Graphics object after painting? -- Marcus Kwok

    L 1 Reply Last reply
    0
    • R ricecake

      I am trying to implement double buffering in a form, and I am getting a crash. I have a fix for the crash but I don't know if it's "correct". To enable double buffering, I call this function from the form's constructor:

      System::Void enable_double_buffering()
      {
      using System::Windows::Forms::ControlStyles;

      SetStyle(static\_cast<ControlStyles>(ControlStyles::DoubleBuffer |
                                          ControlStyles::UserPaint |
                                          ControlStyles::AllPaintingInWmPaint),
               true);
      UpdateStyles();
      

      }

      My Paint handler looks like this:

      System::Void PlotDisplayWindow_Paint(System::Object* /*sender*/, System::Windows::Forms::PaintEventArgs* e)
      {
      using System::Drawing::Graphics;

      Graphics\* g = e->Graphics;
      // draw an image
      g->Dispose();
      

      }

      If I do not enable double buffering, then it works fine, but with flickering. With double buffering, I get the following exception:

      ************** Exception Text **************
      System.ArgumentException: Invalid parameter used.
      at System.Drawing.Graphics.EndContainer(GraphicsContainer container)
      at System.Windows.Forms.DibGraphicsBufferManager.ReleaseBuffer(GraphicsBuffer buffer)
      at System.Windows.Forms.GraphicsBuffer.Dispose()
      at System.Windows.Forms.Control.WmPaint(Message& m)
      at System.Windows.Forms.Control.WndProc(Message& m)
      at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
      at System.Windows.Forms.ContainerControl.WndProc(Message& m)
      at System.Windows.Forms.Form.WndProc(Message& m)
      at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
      at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
      at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

      If I comment out the g->Dispose(); line in the Paint handler, then I do not get the crash. Is it correct/acceptable not to dispose of the Graphics object after painting? -- Marcus Kwok

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #2

      ricecake wrote:

      If I comment out the g->Dispose(); line in the Paint handler, then I do not get the crash. Is it correct/acceptable not to dispose of the Graphics object after painting?

      General rule is you should not be calling Dispose on objects that you didnt create :)**

      How xacc.ide transforms text to colored words on the screen
      Intel PentuimM (aka Centrino) undervolting

      **

      R 1 Reply Last reply
      0
      • L leppie

        ricecake wrote:

        If I comment out the g->Dispose(); line in the Paint handler, then I do not get the crash. Is it correct/acceptable not to dispose of the Graphics object after painting?

        General rule is you should not be calling Dispose on objects that you didnt create :)**

        How xacc.ide transforms text to colored words on the screen
        Intel PentuimM (aka Centrino) undervolting

        **

        R Offline
        R Offline
        ricecake
        wrote on last edited by
        #3

        leppie wrote:

        General rule is you should not be calling Dispose on objects that you didnt create

        Yeah, looking back on it I think the call to Dispose() was an artifact of when I tried to use threads and CreateGraphics(). -- Marcus Kwok

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups