function CheckDC error on closing a Form lazarus / 39-Graphics ---------------------------------------------------------------------- Jeff_t · 2025-09-25 12:33 I have a simple form with a button and a TPaintBox In the PaintBox's Paint event, I draw on the canvas, having used from TColors on the way If I close the Form using the close gadget, I get an exception in function CheckDC - almost as if it's still trying to draw after the TPaintBox has been destroyed. Have I missed something important? ---------------------------------------------------------------------- Thaddy · 2025-09-25 13:05 Did you not use beginpaint/endpaint? ---------------------------------------------------------------------- Jeff_t · 2025-09-25 13:12 QuoteDid you not use beginpaint/endpaint? I did not. Haven't seen that mentioned in any online examples , such as this one from Lazarus Wiki Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TMyDrawingControl.Paint;var  x, y: Integer;  Bitmap: TBitmap;begin  Bitmap := TBitmap.Create;  try    // Initializes the Bitmap Size    Bitmap.Height := Height;    Bitmap.Width := Width;     // Draws the background    Bitmap.Canvas.Pen.Color := clWhite;    Bitmap.Canvas.Rectangle(0, 0, Width, Height);     // Draws squares    Bitmap.Canvas.Pen.Color := clBlack;    for x := 1 to 8 do      for y := 1 to 8 do        Bitmap.Canvas.Rectangle(Round((x - 1) * Width / 8), Round((y - 1) * Height / 8),          Round(x * Width / 8), Round(y * Height / 8));           Canvas.Draw(0, 0, Bitmap);  finally    Bitmap.Free;  end;   inherited Paint;end; I'm on a Mac. A search for BeginPaint gave me this: QuoteIn the Lazarus IDE and Free Pascal's LCL (Lazarus Component Library), BeginPaint is a low-level Windows API function, accessed via the LCLIntf unit, used in conjunction with EndPaint to manage the process of drawing or updating a control's visual appearance. It prepares a window for painting, fills a PAINTSTRUCT structure with painting information, and is often called from the OnPaint event handler or overridden Paint method to ensure that custom drawing persists correctly. So is it needed on all OS's? ---------------------------------------------------------------------- Thaddy · 2025-09-25 13:23 try Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.FormPaint(Sender: TObject);var  P: Array[0..4] of TPoint;  i: Integer;  phi: Double; begin  form1.canvas.LockCanvas;  for i := 0 to 4 do  begin    phi := 2.0 * pi / 5 * i + pi * 0.5;;    P[i].X := round(100 * cos(phi) + 110);    P[i].Y := round(100 * sin(phi) + 110);  end;  Canvas.Brush.Color := clRed;  Canvas.Polygon(P);  form1.canvas.UnlockCanvas;end;Or Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.FormPaint(Sender: TObject);var  P: Array[0..4] of TPoint;  i: Integer;  phi: Double; begin  form1.BeginFormUpdate;  for i := 0 to 4 do  begin    phi := 2.0 * pi / 5 * i + pi * 0.5;;    P[i].X := round(100 * cos(phi) + 110);    P[i].Y := round(100 * sin(phi) + 110);  end;  Canvas.Brush.Color := clRed;  Canvas.Polygon(P);  form1.EndFormUpdate;end; Both are blocking calls, I guess the second would also work on mac. ---------------------------------------------------------------------- Jeff_t · 2025-09-25 13:38 Well, Lock and Unlock didn't affect things. BeginPaint and EndPaint  report not available on my canvas Invalid Access exception. The problem line appears to be this: if TObject(dc) is TCocoaContext dc does have a longint value at this point, and that line is quite happy when the form opens. If I remove all code from the Paint, it works. The presence of these lines (which do produce the required rectangle) brings the error back Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i               // Set the fill color                Canvas.Brush.Color :=palette[cul1].palRGB;  //palRGB is a prefilled TColor                // Draw the rectangle                Canvas.Rectangle(R);   //R is a prefilled TRect ---------------------------------------------------------------------- Jeff_t · 2025-09-25 20:14 The exact same code works perfectly if pasted into the Form's Onpaint event. Confused. ---------------------------------------------------------------------- korba812 · 2025-09-25 21:07 It seems that you are drawing on form canvas. Try drawing on the PaintBox canvas. ---------------------------------------------------------------------- Jeff_t · 2025-09-26 06:12 QuoteIt seems that you are drawing on form canvas. If I was drawing on the form's canvas, the drawing would be relative to the top left corner of the form, yes? It is not - when I draw in the PaintBox methods, drawing begins top left corner of the Paintbox. But if I prefix the  calls to   Canvas.   with the name of the Paintbox, all seems well. eg if the Paintbox is named Brian, then using Brian.Canvas.Rectangle() is OK This is hugely unintuitive,  as I would have expected any reference to an unqualified 'Canvas' within a method of the TPaintBox , to refer to the TPaintbox, not the form. ---------------------------------------------------------------------- cdbc · 2025-09-26 06:35 Hi In the code-editor, if you hover the mouse over the method-name, 'Code-Tools'*) will show you to whom the method belongs... *) If you [Ctrl] + Click on the method-name, code-tools will take you to its     declaration. Regards Benny ---------------------------------------------------------------------- wp · 2025-09-26 09:50 Quote from: Jeff_t on September 25, 2025, 02:33:38 pmI have a simple form with a button and a TPaintBox Quote from: Jeff_t on September 25, 2025, 03:12:10 pmCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TMyDrawingControl.Paint;[...] Your introductory statement about issues in drawing on a Paintbox and the code shown later are contradictory because that code is for a custom control, TMyDrawingControl. Are you sure that your issues are not rooted somewhere in that custom control? I've never seen issues that you are reporting for a plain TPaintbox. Just tested again on a mac-VM, and the Paintbox is working fine.