Recent

Author Topic: sigsegv error on loading image to form  (Read 4965 times)

titojff

  • Newbie
  • Posts: 3
sigsegv error on loading image to form
« on: January 12, 2013, 05:50:03 pm »
Hi,  I´m creating a program that has a button to open a picture, and to display it on the same form, (later I want to do pixel manipulation on the image), but when I compile it gives me a external sigsegv error.can you help me?

unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil,LResources, Forms, Controls, Graphics, Dialogs, ExtDlgs,
  StdCtrls, BGRABitmap, BGRABitmapTypes;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    OpenPictureDialog1: TOpenPictureDialog;
    procedure Button1Click(Sender: TObject);
    procedure FormPaint(Sender: TObject);

  private

    image: TBGRABitmap;

    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1; filename:AnsiString;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
begin
  if OpenPictureDialog1.Execute then
  filename := OpenPictureDialog1.FileName;
  image:= TBGRABitmap.Create(filename);
end;

procedure TForm1.FormPaint(Sender: TObject);
begin
  image.Draw(Canvas,220,240,True);
  image.Free;
end;

end.

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: sigsegv error on loading image to form
« Reply #1 on: January 12, 2013, 06:06:46 pm »
There's all kinds of mistakes.
- If..Then used for 2 lines, without begin..end.
- Created class objects (TBGRABitmap), but never freed them after use.
- Used class objects but never checked if they were even created first.

You need to make a onDestroy event to form also which will free the image at end, if it was created.
« Last Edit: January 12, 2013, 06:08:26 pm by User137 »

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: sigsegv error on loading image to form
« Reply #2 on: January 12, 2013, 06:17:53 pm »
the problem is that the paint event is executed before the image variable is assinged an image and of course it raises an exception change the formPaint procedure to the following

Code: [Select]
procedure TForm1.FormPaint(Sender: TObject);
begin
  if assigned(image) then begin
    image.Draw(Canvas,220,240,True);
    image.Free;
  end;
end;

also you need to make sure that when you open a second image you do not have a memory leak loosing the existing image object reference so change the button's click event to the following

Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
begin
  if OpenPictureDialog1.Execute then begin
    filename := OpenPictureDialog1.FileName;
    if assigned(image) then freeandnil(image);
    image:= TBGRABitmap.Create(filename);
  end;
end;

those changes will help with your current problem there is also the memory leak problem when you close the form after you have opened an image but if this is the programs main form the windows will clean that up for you if not the add a destroy event and free the image there.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: sigsegv error on loading image to form
« Reply #3 on: January 12, 2013, 11:00:40 pm »
Actually that would not work well; wipes graphics away on alt+tabbing, or moving or resizing of form etc. I was thinking code like:
Code: [Select]
procedure TForm1.FormPaint(Sender: TObject);
begin
  if assigned(image) then begin
    image.Draw(Canvas,220,240,True);
  end;
end;

Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
var filename: String; // << Edit; added local var instead
begin
  if OpenPictureDialog1.Execute then begin
    filename := OpenPictureDialog1.FileName;
    if assigned(image) then image.Free;
    image:=TBGRABitmap.Create(filename);
  end;
end;

form1's onDestroy event:
Code: [Select]
  if assigned(image) then image.Free;
« Last Edit: January 13, 2013, 03:44:44 am by User137 »

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: sigsegv error on loading image to form
« Reply #4 on: January 13, 2013, 01:03:04 am »
true I did not pay attention to the image.free call at all.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: sigsegv error on loading image to form
« Reply #5 on: January 13, 2013, 01:05:20 pm »
A minor point: the suggested test in the OnDestroy event is redundant
Code: [Select]
  if assigned(image) then image.Free;
The call
Code: [Select]
  image.Free;
alone is sufficient, since Free already contains the equivalent of an Assigned() test.
Or you could call Destroy without duplicating the test
Code: [Select]
  if assigned(image) then image.Destroy;

 

TinyPortal © 2005-2018