Jump to content
Yaroslav Brovin
Message added by Yaroslav Brovin,

Please be aware that these comments were copied here from another source and that the date and time shown for each comment may not be accurate.

  • Status: Fixed
  • Priority: Normal
  • Resolution: Fixed
  • Platform: iOS
  • Affects version: 1.18.1.3

Hi

Please see the attached demo.
If you create and free a frame with a TfgCollectionView  in code I get an  EAccessViolation with message 'Access violation....."

No issues on Android.

 

TestCollectionView.zip


Fix version: 1.18.1.3

User Feedback

Recommended Comments

  • Administrators

Hello @Alan,

The problem is in your demo sample code. You have 2 methods for manual creating and removing Items list.

procedure TFrameCollenionView.FixUpFrame;

  procedure CreateAndFillList;
  var
    loop: Integer;
  begin
    If not Assigned(FInternalList)  then
      FInternalList:= TStringList.Create;

    FInternalList.Clear;
    for Loop := 0 to 20 do
      FInternalList.Add('String # ' + Loop.ToString)
  end;

begin
  CreateAndFillList;
end;

procedure TFrameCollenionView.TearDownFrame;
begin
  FInternalList.Free;
end;

There are 2 problems with this code:

1. Since FInternalList is not available on Frame/Form creating process, you have to complicate your code by adding unnecessary checks on the nil like:

function TFrameCollenionView.fgCollectionView1GetItemCount(Sender: TObject): Integer;
begin
  If Assigned(FInternalList) then
    Result := FInternalList.Count -1
  else
    Result:= 0;
end;

So the best approach is:

  • Allocate FInternalList in Frame/Form constructor
  • Destroy FInternalList in Frame/Form destructor

2. Since you are destroying FInternalList in TearDownFrame manually. Free method doesn't reset reference of FInternalList on nil. So this the field contains reference on release memory. And you get AV, when your code is trying to check:

function TFrameCollenionView.fgCollectionView1GetItemCount(Sender: TObject): Integer;
begin
  If Assigned(FInternalList) then
    Result := FInternalList.Count -1
  else
    Result:= 0;
end;

The better solution is using FreeAndNil method instead (if you don't want to sue suggestion from (1)).

P.S. Also please don't remember to use the latest FGX Native version 1.18.1.3.

Alan

Active subscription

Hi @Yaroslav Brovin

Once again thanks for the detailed explanation.

My preference would be to  do exactly as you say.

6 hours ago, Yaroslav Brovin said:

So the best approach is:

  • Allocate FInternalList in Frame/Form constructor
  • Destroy FInternalList in Frame/Form destructor

But unless I have missed something Frames do not expose a constructor. They expose an OnDestroy event but no OnCreate?
That's why I have  FixUp and TearDown procedures. Could I request the addition of OnCreate for TfgFrames.

Thanks

Alan

  • Administrators
8 часов назад, Alan сказал:

But unless I have missed something Frames do not expose a constructor. They expose an OnDestroy event but no OnCreate?

Any class provides a constructor and a destructor. You just need to redefine it.

type
  TFrameCollenionView = class(TfgFrame)
  // .... 
  private
    FInternalList: TStringList;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  end;

constructor TFrameCollenionView.Create(AOwner: TComponent);
begin
  inherited;
  FInternalList:= TStringList.Create;
end;

destructor TFrameCollenionView.Destroy;
begin
  FInternalList.Free;
  inherited;
end;

Alan

Active subscription

Sorry, did not see update.
Problem is Fixed.

Thank you

  • Administrators

Изменено Статус на Fixed

Изменено Резолюция на Fixed

Изменено Версия с исправлением на 1.18.1.3

Create an account or sign in to comment

Recently Browsing 0

  • No registered users viewing this page.