* * *

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

vfclists

  • Sr. Member
  • ****
  • Posts: 292
    • 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.3/FPC 2.6.0
Linux - Lazarus 0.9.31.1/FPC 2.5.1

Mooji and the Sound of Wood Chopping
your Self, The Projector
The Strange Case of the ZX81

Blaazen

  • Hero Member
  • *****
  • Posts: 1468
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.1 r41316M FPC 2.7.1 r24245 x86_64-linux-qt openSuSE 12.3, KDE4.10.2

vfclists

  • Sr. Member
  • ****
  • Posts: 292
    • 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.3/FPC 2.6.0
Linux - Lazarus 0.9.31.1/FPC 2.5.1

Mooji and the Sound of Wood Chopping
your Self, The Projector
The Strange Case of the ZX81

User137

  • Hero Member
  • *****
  • Posts: 1039
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 »

vfclists

  • Sr. Member
  • ****
  • Posts: 292
    • 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.3/FPC 2.6.0
Linux - Lazarus 0.9.31.1/FPC 2.5.1

Mooji and the Sound of Wood Chopping
your Self, The Projector
The Strange Case of the ZX81

 

Recent

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