Recent

Author Topic: Does ReadComponentRes function work correctly.  (Read 685 times)

dmitryb

  • New Member
  • *
  • Posts: 41
Does ReadComponentRes function work correctly.
« on: May 22, 2020, 09:21:51 pm »
 Hi

Can anyone check.
Does ReadComponentRes function work correctly?
I always get the error 'External: SIGSEGV' when call it.


jamie

  • Hero Member
  • *****
  • Posts: 2935
Re: Does ReadComponentRes function work correctly.
« Reply #1 on: May 23, 2020, 03:32:56 am »
I think you need to show us the code that is using that feature , a few lines before and after etc.
Number 1 at blue screen app creations!

dmitryb

  • New Member
  • *
  • Posts: 41
Re: Does ReadComponentRes function work correctly.
« Reply #2 on: May 23, 2020, 03:44:19 am »
Ok.
Code
Code: Pascal  [Select][+][-]
  1. procedure TLanguageResourceManagerEh.ReadComponentResource(
  2.   Component: TComponent; ResourceName: String);
  3. var
  4.   ConsumerItfs: ILanguageResourceLoadNotificationConsumer;
  5.   {$IFDEF FPC}
  6.   HRs: {%H-}HRSRC;
  7.   {$ELSE}
  8.   HRs: HRSRC;
  9.   {$ENDIF}
  10.   HIn: HINST;
  11. begin
  12.   {$IFDEF FPC}
  13.   HIn := HInstance;
  14.   {$ELSE}
  15.   HIn := FindResourceHInstance(FindClassHInstance(Component.ClassType));
  16.   {$ENDIF}
  17.   HRs := FindResource(HIn, PChar(ResourceName), PChar(RT_RCDATA));
  18.  
  19.   if HRs <> 0 then
  20.   begin
  21.     ReadComponentRes(ResourceName, Component);
  22.     if Supports(Component, ILanguageResourceLoadNotificationConsumer, ConsumerItfs) then
  23.       ConsumerItfs.ResourceLanguageChanged;
  24.   end;
  25. end;
  26.  

and two extra screens

dmitryb

  • New Member
  • *
  • Posts: 41
Re: Does ReadComponentRes function work correctly.
« Reply #3 on: May 23, 2020, 03:44:52 am »
Screen two

jamie

  • Hero Member
  • *****
  • Posts: 2935
Re: Does ReadComponentRes function work correctly.
« Reply #4 on: May 23, 2020, 04:09:49 pm »
You need to put a break point at the start of that code and then use F8 to step your way through it one line at a time..

 from looking at the code I suspect you are miss using the functions..

 It looks like you are mixing both System level resource functions with component level functions.
Number 1 at blue screen app creations!

dmitryb

  • New Member
  • *
  • Posts: 41
Re: Does ReadComponentRes function work correctly.
« Reply #5 on: May 23, 2020, 04:25:49 pm »
Try this test Project.
It have the same error.

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     procedure Button1Click(Sender: TObject);
  17.   private
  18.  
  19.   public
  20.  
  21.   end;
  22.  
  23. var
  24.   Form1: TForm1;
  25.  
  26. implementation
  27.  
  28. {$R *.lfm}
  29.  
  30. { TForm1 }
  31.  
  32. procedure TForm1.Button1Click(Sender: TObject);
  33. var
  34.   testFrm: TForm1;
  35. begin
  36.   testFrm := TForm1.Create(nil);
  37.   ReadComponentRes('TForm1', testFrm);
  38. end;
  39.  
  40. end.

jamie

  • Hero Member
  • *****
  • Posts: 2935
Re: Does ReadComponentRes function work correctly.
« Reply #6 on: May 23, 2020, 06:08:15 pm »
Try this test Project.
It have the same error.

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     procedure Button1Click(Sender: TObject);
  17.   private
  18.  
  19.   public
  20.  
  21.   end;
  22.  
  23. var
  24.   Form1: TForm1;
  25.  
  26. implementation
  27.  
  28. {$R *.lfm}
  29.  
  30. { TForm1 }
  31.  
  32. procedure TForm1.Button1Click(Sender: TObject);
  33. var
  34.   testFrm: TForm1;
  35. begin
  36.   testFrm := TForm1.Create(nil);
  37.   ReadComponentRes('TForm1', testFrm);
  38. end;
  39.  
  40. end.

 So I am assuming you are attempting to read the resource of a form that is named TForm1 that should be in the resource already ?

I've never done it that way but if you want to create forms that way you can create the form in the designer but indicate to have not auto create at runtime. So at runtime you can create these forms dynamically and they will read the resource from the same location for each instance of the form you create.

 Is that what you are after ?


Number 1 at blue screen app creations!

dmitryb

  • New Member
  • *
  • Posts: 41
Re: Does ReadComponentRes function work correctly.
« Reply #7 on: May 23, 2020, 06:30:06 pm »
Hello

I don't want to create a Form in real situation.

Here I used TForm1 to create I simple example of using ReadComponentRes.

---
In real situation I have dfm file with content, and it is not a Form.

EhLibLangConsts.ENU.dfm
Code: Text  [Select][+][-]
  1. object EhLibLanguageConsts: TEhLibLanguageConsts_ENU
  2.   Language = 'English'
  3.  
  4.   ClearSelectedCells = 'Clear selected cells?'
  5.   InvalidTextFormat = 'Invalid text format'
  6.   InvalidVCLDBIFFormat = 'Invalid VCLDBIF format'
  7.   ErrorDuringInsertValue = 'Error during insert value:'
  8. ...
  9. end
  10.  

EhLibLanguageConsts.PAS
Code: Pascal  [Select][+][-]
  1. { TEhLibLanguageConsts }
  2.  
  3.   TEhLibLanguageConsts = class(TComponent)
  4.   private
  5.     FVisibleColumns: String;
  6.     FPageOfPages: String;
  7.   published
  8.     property Language: String read FLanguage write FLanguage; //-'English'
  9.  
  10.     property ClearSelectedCells: String read FClearSelectedCells write FClearSelectedCells;
  11.     property InvalidTextFormat: String read FInvalidTextFormat write FInvalidTextFormat;
  12.     property InvalidVCLDBIFFormat: String read FInvalidVCLDBIFFormat write FInvalidVCLDBIFFormat;
  13.   end;
  14.  

APP.Pas
Code: Pascal  [Select][+][-]
  1. ...
  2. //Link dfm file (It is not a Form file but dfm file)
  3. {$R C:\RADStudio\lazarus\EhLib\Lib\Res\EhLibLangConsts.ENU.dfm}
  4.  
---
And here I want to read EhLibLangConsts.ENU.dfm resource into Component of TEhLibLanguageConsts type.

So I try to use:
Code: Pascal  [Select][+][-]
  1.   ReadComponentRes('TEhLibLanguageConsts_ENU', Component);
  2.  

It works in Delphi but don't work in Lazarus.

PascalDragon

  • Hero Member
  • *****
  • Posts: 1526
  • Compiler Developer
Re: Does ReadComponentRes function work correctly.
« Reply #8 on: May 23, 2020, 10:34:15 pm »
It works in Delphi but don't work in Lazarus.

Seems like no one really tested this before and thus no one noticed that ReadComponentRes is buggy. I'm already working on a bugfix.

As a workaround you can do the following (it's a copy of SysReadComponentRes from rtl\objpas\classes\classes.inc with a slight adjustment when calling FindResource; note: on Windows you'll need to use the Windows unit as well):

Code: Pascal  [Select][+][-]
  1. function MyReadComponentRes(HInstance : THandle; const ResName: String; Instance: TComponent): TComponent;
  2.  
  3. Var
  4.   H : TFPResourceHandle;
  5.  
  6. begin
  7.   { Windows unit also has a FindResource function, use the one from
  8.     system unit here.  }
  9.   H:=system.FindResource(HInstance,PChar(ResName) { ensure the PChar variant of FindResource is used! },RT_RCDATA);
  10.   if (PtrInt(H)=0) then
  11.     Result:=Nil
  12.   else
  13.     With TResourceStream.Create(HInstance,ResName,RT_RCDATA) do
  14.       try
  15.         Result:=ReadComponent(Instance);
  16.       Finally
  17.         Free;
  18.       end;
  19. end;

PascalDragon

  • Hero Member
  • *****
  • Posts: 1526
  • Compiler Developer
Re: Does ReadComponentRes function work correctly.
« Reply #9 on: May 23, 2020, 11:26:18 pm »
Seems like no one really tested this before and thus no one noticed that ReadComponentRes is buggy. I'm already working on a bugfix.

Fixed in r45479.

S_Gur

  • Newbie
  • Posts: 4
Re: Does ReadComponentRes function work correctly.
« Reply #10 on: May 24, 2020, 07:06:26 am »
I downloaded two of your fixed files - resh.inc and sysres.inc. I replaced them with those available in my assembly, in the fpc \ 3.0.4 \ source \ rtl \ inc folder. Then he climbed into the list of packages and added one, forcing Lazarus to rebuild the environment according to the "normal assembly" profile. After that I tried to start the project and got the same Access Violation on the same line - ReadComponentRes (ResourceName, Component); Hence the question - did I somehow improperly execute the Lazarus patch or did you still not completely correct the error? I apologize for my English - I don’t have much with it ...

Cyrax

  • Hero Member
  • *****
  • Posts: 806
Re: Does ReadComponentRes function work correctly.
« Reply #11 on: May 24, 2020, 07:33:33 am »
I downloaded two of your fixed files - resh.inc and sysres.inc. I replaced them with those available in my assembly, in the fpc \ 3.0.4 \ source \ rtl \ inc folder. Then he climbed into the list of packages and added one, forcing Lazarus to rebuild the environment according to the "normal assembly" profile. After that I tried to start the project and got the same Access Violation on the same line - ReadComponentRes (ResourceName, Component); Hence the question - did I somehow improperly execute the Lazarus patch or did you still not completely correct the error? I apologize for my English - I don’t have much with it ...

You need to rebuild your FPC installation, rebuilding Lazarus isn't enough.

PascalDragon

  • Hero Member
  • *****
  • Posts: 1526
  • Compiler Developer
Re: Does ReadComponentRes function work correctly.
« Reply #12 on: May 24, 2020, 10:23:09 am »
I downloaded two of your fixed files - resh.inc and sysres.inc. I replaced them with those available in my assembly, in the fpc \ 3.0.4 \ source \ rtl \ inc folder. Then he climbed into the list of packages and added one, forcing Lazarus to rebuild the environment according to the "normal assembly" profile. After that I tried to start the project and got the same Access Violation on the same line - ReadComponentRes (ResourceName, Component); Hence the question - did I somehow improperly execute the Lazarus patch or did you still not completely correct the error? I apologize for my English - I don’t have much with it ...

As Cyrax said, rebuilding Lazarus does not rebuild FPC and Lazarus only ships with the RTL and packages sources anyway to allow navigating. Instead you'll have to download the sources of FPC 3.0.4, apply the patch and completely build and install that instead.

S_Gur

  • Newbie
  • Posts: 4
Re: Does ReadComponentRes function work correctly.
« Reply #13 on: May 24, 2020, 10:40:44 am »
I'm understood, thank you

S_Gur

  • Newbie
  • Posts: 4
Re: Does ReadComponentRes function work correctly.
« Reply #14 on: May 27, 2020, 08:42:25 am »
Seems like no one really tested this before and thus no one noticed that ReadComponentRes is buggy. I'm already working on a bugfix.

Fixed in r45479.

Please tell me, your corrected modules can be used to rebuild the stable version FPC?

 

TinyPortal © 2005-2018