* * *

Author Topic: What is wrong with this object assignment?  (Read 931 times)

vfclists

  • Sr. Member
  • ****
  • Posts: 385
    • HowTos Considered Harmful?
What is wrong with this object assignment?
« on: February 06, 2013, 11:32:34 pm »
In the code below I am using the variable f as an abbreviation for a field named FDataModule. The idea is to check that the data module exists an display a related form. If the data module does not exist, it is created before.

I thought that by f:= FDataModule following by the Assigned procedure would work as I expected, f would not be nil iif FDataModule was already created. However in this case f is always nil and new forms are always opened.

What is wrong with the code?

Code: [Select]
procedure TdmCentral.actionExecute(Sender: TObject);
var
  f: TdmDateModule;
begin
  {
  f := FDataModule;
  if not Assigned(f) then
  begin
    f := TdmDataModule.Create(self);
  end;
  f.DisplayForm;
  }
  if not Assigned(FDataModule) then
  begin
    FDataModule := TdmDataModule.Create(self);
  end;
  FDataModule.DisplayForm;

end
;
Windows - Lazarus 0.9.30.1/FPC 2.4.4
Windows - Lazarus 1.0.8/FPC 2.6.0
Linux - Lazarus 1.3 /FPC 2.6.2

Blaazen

  • Hero Member
  • *****
  • Posts: 1624
Re: What is wrong with this object assignment?
« Reply #1 on: February 06, 2013, 11:58:24 pm »
Strange.
Isn't there missing:
Code: [Select]
FDataModule := f;below the construction?
I mean that the TdmDateModule (in variable f) is created again and again but always lost (but not freed) when method ends.
Lazarus 1.3 r44722M FPC 2.7.1 r27408 x86_64-linux-qt Chakra 2013.5, Qt 4.8.5, KDE4.12.2
Lazarus 1.2.0 r44303 FPC 2.6.4 i386-win32-win32/win64 Wine 1.7.16

vfclists

  • Sr. Member
  • ****
  • Posts: 385
    • HowTos Considered Harmful?
Re: What is wrong with this object assignment?
« Reply #2 on: February 07, 2013, 12:42:19 pm »
I see what you mean.

I thought that setting
Code: [Select]
f:= FDataModule and creating f would automatically assign FDataModule as well. In effect  Assigned(f) would be true if FDataModule was not nil to begin with.
Strange.
Isn't there missing:
Code: [Select]
FDataModule := f;below the construction?
I mean that the TdmDateModule (in variable f) is created again and again but always lost (but not freed) when method ends.
Windows - Lazarus 0.9.30.1/FPC 2.4.4
Windows - Lazarus 1.0.8/FPC 2.6.0
Linux - Lazarus 1.3 /FPC 2.6.2

User137

  • Hero Member
  • *****
  • Posts: 1307
    • Nxpascal home
Re: What is wrong with this object assignment?
« Reply #3 on: February 07, 2013, 01:36:35 pm »
Code: [Select]
  if not Assigned(FDataModule) then
  begin
    FDataModule := TdmDataModule.Create(self);
  end;
  FDataModule.DisplayForm;
The code block looks fine. What else is happening, for instance have you debugged inside DisplayForm()? And how is FDataModule initialized? Can TdmDataModule.Create(self) take TdmCentral as parameter?
« Last Edit: February 07, 2013, 01:39:11 pm by User137 »
Win7, FPC and Lazarus 32-bit SVN version.

vfclists

  • Sr. Member
  • ****
  • Posts: 385
    • HowTos Considered Harmful?
Re: What is wrong with this object assignment?
« Reply #4 on: February 07, 2013, 04:15:32 pm »
The question is concerned with the part that is in comments. In any case the semantics were not what I thought they were, but Blaazen pointed out the error.

Code: [Select]
  if not Assigned(FDataModule) then
  begin
    FDataModule := TdmDataModule.Create(self);
  end;
  FDataModule.DisplayForm;
The code block looks fine. What else is happening, for instance have you debugged inside DisplayForm()? And how is FDataModule initialized? Can TdmDataModule.Create(self) take TdmCentral as parameter?
Windows - Lazarus 0.9.30.1/FPC 2.4.4
Windows - Lazarus 1.0.8/FPC 2.6.0
Linux - Lazarus 1.3 /FPC 2.6.2

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads