Recent

Author Topic: 'External: SIGSEGV' error after drawing a BGRABitmap into TImage in FormCreate  (Read 1297 times)

_N_

  • New Member
  • *
  • Posts: 33
Hello. I am using BGRABitmap in my project to draw some graphical scene. As there is obvious need to show it, when everything is done, I draw the content of BGRABitmap image on TImage canvas. Also, there is a requirement to show initial scene right after application start, so I am just using OnCreate event of the main form (an instance of TImage is added at design time, so exists before entering FormCreate). Theoretically it should work just fine. But in fact, after second attempt to draw content of BGRABitmap to TImage canvas 'External: SIGSEGV' error occurs (this happens on event (say button clicked) after Application.Run was invoked). The error doesn't happen when I do not draw the initial scene in FormCreate, however such code looks ideologically valid to me.
I've created sample project to reproduce the issue (see attachment). Here is the essential code:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   bgra : TBGRABitmap;
  4.   aCanvas : TCanvas;
  5. begin
  6.   bgra := TBGRABitmap.Create(Image1.Width, Image1.Height);
  7.   try
  8.     aCanvas := bgra.Canvas;
  9.     aCanvas.TextOut(10, 10, 'Test');
  10.     aCanvas.TextOut(100, 100, '2222');
  11.     bgra.Draw(Image1.Canvas, 0, 0, True);
  12.   finally
  13.     bgra.Free();
  14.   end;
  15. //begin
  16. //  Image1.Canvas.TextOut(10, 10, 'Test');
  17. //  Image1.Canvas.TextOut(100, 100, '2222');
  18. end;
  19.  
  20. procedure TForm1.FormCreate(Sender: TObject);
  21. begin
  22.   Button1Click(Sender);
  23. end;
  24.  

So, when run the application, the text will appear in the image, but when click the button - 'External: SIGSEGV' error happens. However, the error doesn't happen if canvas of TImage is used directly (see commented code) OR we do not attempt to draw in FromCreate (comment Button1Click invocation in FormCreate and then, when click the button two or more times - everything is ok).

Is it a bug, or I just miss something?

Environment:
Linux Mint 20.3 (Una)
Lazarus 2.0.6+dfsg-3, FPC 3.0.4, linux gtk2 (from standard repository, a bit older, I know)

_N_

  • New Member
  • *
  • Posts: 33
There is a possibility to use BGRACanvas, e.g.
Code: Pascal  [Select][+][-]
  1. bgra.CanvasBGRA.TextOut(10, 10, 'Test');
  2.  
and this solved the issue!

However, my drawing procedures deal with standard TCanvas, so some code changes needed. Also, in that case, I'll lose possibility to draw on just a regular canvas.

P.S. The question why it crashes with regular canvas is open.

winni

  • Hero Member
  • *****
  • Posts: 3045
Hi!

Never put a code with visual components in the onCreate event of a Form. A lot of things might not be initialized and you do not know anything about possible side effects.

Put it in the onActivate event. And in the event disable itself:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormActivate(Sender: TObject);
  2. begin
  3.      Button1Click(Sender);
  4.      OnActivate:= Nil;
  5. end;
  6.  


Winni

winni

  • Hero Member
  • *****
  • Posts: 3045
Hi!

Second issue:

The correct way to write on a BGRAbitmap is this;

Code: Pascal  [Select][+][-]
  1. BGRA.TextOut(10,10,'Some Text', cssRed, taLeftJustify);

Don't use the defaullt canvas.

Read the BGRA tutorial:

https://wiki.freepascal.org/BGRABitmap_tutorial

Chapter 12 is about writing text and texteffects.

Winni


_N_

  • New Member
  • *
  • Posts: 33
Thank you, winni, I'll try to fix my mistakes.

 

TinyPortal © 2005-2018