Recent

Author Topic: [SOLVED] Passing TPortableNetworkGraphic as result  (Read 285 times)

pcurtis

  • Sr. Member
  • ****
  • Posts: 351
[SOLVED] Passing TPortableNetworkGraphic as result
« on: September 26, 2020, 08:35:41 am »
Hi All,

I need to pass a TPortableNetworkGraphic as the result of a function but I keep getting SIGSEGV errors. What am I doing wrong?

Code below. The resource file and data are ok.

Thanks in advance.

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, Buttons, ExtCtrls, Windows;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     BitBtn1: TBitBtn;
  16.     Image1: TImage;
  17.     procedure FormCreate(Sender: TObject);
  18.     function GetRes(aRESOURCE : string) : TPortableNetworkGraphic;
  19.   private
  20.  
  21.   public
  22.  
  23.   end;
  24.  
  25. var
  26.   Form1: TForm1;
  27.  
  28. implementation
  29.  
  30. {$R *.lfm}
  31.  
  32. {$R buttonpkg.res}
  33.  
  34. { TForm1 }
  35.  
  36. function TForm1.GetRes(aRESOURCE : string) : TPortableNetworkGraphic;
  37. var
  38.   aPNG : TPortableNetworkGraphic;
  39.   ResStream : TResourceStream;
  40. begin
  41.  Result := TPortableNetworkGraphic.Create;
  42.  aPNG := TPortableNetworkGraphic.Create;
  43.   try
  44.     ResStream := TResourceStream.Create(HINSTANCE, aRESOURCE, RT_RCDATA);
  45.     try
  46.       aPNG.LoadFromStream(ResStream);
  47.       Result := aPNG;
  48.     finally
  49.       ResStream.Free;
  50.     end;
  51.   finally
  52.     aPNG.Free;
  53.   end;
  54. end;
  55.  
  56. procedure TForm1.FormCreate(Sender: TObject);
  57. var
  58.   aPNG : TPortableNetworkGraphic;
  59. begin
  60.  aPNG := TPortableNetworkGraphic.Create;
  61.  aPNG := GetRes('ADD');
  62.  BitBtn1.Glyph.Assign(aPNG);
  63.  Image1.Picture.Assign(aPNG);
  64.  aPNG.Free;
  65. end;
  66.  
  67. end.
  68.  
« Last Edit: September 26, 2020, 12:36:33 pm by pcurtis »
Windows 10 / Linux Mint 20
Laz 2.10.0
FPC 3.2.0

BeanzMaster

  • Sr. Member
  • ****
  • Posts: 270
Re: Passing TPortableNetworkGraphic as result
« Reply #1 on: September 26, 2020, 09:01:48 am »
Hi it's because you free the aPNG Variable. You assign aPNG as reference to Result . It's not a copy. So when your free aPNG result also become nil
Use Result.Assign(aPNG) instead.
But why using intermediate variable ? just use result.

Sorry for my english.

Code: Pascal  [Select][+][-]
  1. function TForm1.GetRes(aRESOURCE : string) : TPortableNetworkGraphic;
  2. var
  3.   ResStream : TResourceStream;
  4. begin
  5.    Result := TPortableNetworkGraphic.Create;
  6.     ResStream := TResourceStream.Create(HINSTANCE, aRESOURCE, RT_RCDATA);
  7.     try
  8.       Result.LoadFromStream(ResStream);
  9.     finally
  10.       ResStream.Free;
  11.     end;
  12. end;

Best regards

Handoko

  • Hero Member
  • *****
  • Posts: 3909
  • My goal: build my own game engine using Lazarus
Re: Passing TPortableNetworkGraphic as result
« Reply #2 on: September 26, 2020, 09:11:09 am »
Try mine, it works:
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, ExtCtrls, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     Image1: TImage;
  17.     procedure Button1Click(Sender: TObject);
  18.   private
  19.     function LoadImageFromResource(const S: string): TPortableNetworkGraphic;
  20.   end;
  21.  
  22. var
  23.   Form1: TForm1;
  24.  
  25. implementation
  26.  
  27. {$R *.lfm}
  28.  
  29. { TForm1 }
  30.  
  31. procedure TForm1.Button1Click(Sender: TObject);
  32. begin
  33.   Image1.Picture.Assign(LoadImageFromResource('IMG'));
  34. end;
  35.  
  36. function TForm1.LoadImageFromResource(const S: string): TPortableNetworkGraphic;
  37. var
  38.   Stream: TResourceStream;
  39.   Image:  TPortableNetworkGraphic;
  40. begin
  41.   Stream := TResourceStream.Create(HInstance, 'IMG', RT_RCDATA);
  42.   Image := TPortableNetworkGraphic.Create;
  43.   Image.LoadFromStream(Stream);
  44.   Stream.Free;
  45.   Result := Image;
  46. end;
  47.  
  48. end.

BeanzMaster posted faster than me.
« Last Edit: September 26, 2020, 09:13:10 am by Handoko »

pcurtis

  • Sr. Member
  • ****
  • Posts: 351
Re: Passing TPortableNetworkGraphic as result
« Reply #3 on: September 26, 2020, 09:24:23 am »
Thanks All.

@BeanzMaster - Don't apologize for your English. I must apologize for my lack of skills :-[
Windows 10 / Linux Mint 20
Laz 2.10.0
FPC 3.2.0

 

TinyPortal © 2005-2018