| View previous topic :: View next topic |
| Author |
Message |
David Reid Guest
|
Posted: Fri Nov 21, 2003 1:05 am Post subject: Results.Assign crashes. Why? |
|
|
Hello,
Does anybody know why the following code crashes on the line
"Results.Assign(myFont)" in the GetFont function? Please see the following
code.
Thank-you,
David Reid
<< Code Start >>
function TForm1.GetFont:TFont;
var
myFont:TFont;
begin
myFont := TFont.Create;
try
myFont.Size := 10;
Result.Assign(myFont);
finally
FreeAndNil(myFont);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Label1.Font.Assign(GetFont);
end;
<< Code End >>
|
|
| Back to top |
|
 |
Bryan Crotaz Guest
|
Posted: Fri Nov 21, 2003 1:05 am Post subject: Re: Results.Assign crashes. Why? |
|
|
Because you haven't created the result font object.
try this:
function TForm1.GetFont:TFont;
begin
Result := TFont.Create;
try
Result.Size := 10;
except
on Exception do
begin
Result.Free;
Raise;
end;
end;
end;
"David Reid" <dreid (AT) racltd (DOT) com> wrote
| Quote: | Hello,
Does anybody know why the following code crashes on the line
"Results.Assign(myFont)" in the GetFont function? Please see the
following
code.
Thank-you,
David Reid
Code Start
function TForm1.GetFont:TFont;
var
myFont:TFont;
begin
myFont := TFont.Create;
try
myFont.Size := 10;
Result.Assign(myFont);
finally
FreeAndNil(myFont);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Label1.Font.Assign(GetFont);
end;
Code End
|
|
|
| Back to top |
|
 |
Harley Pebley Guest
|
Posted: Fri Nov 21, 2003 1:11 am Post subject: Re: Results.Assign crashes. Why? |
|
|
| Quote: | Does anybody know why the following code crashes on the line
"Results.Assign(myFont)" in the GetFont function?
|
Yes.
| Quote: | Please see the following code.
Code Start
function TForm1.GetFont:TFont;
var
myFont:TFont;
begin
myFont := TFont.Create;
try
myFont.Size := 10;
Result.Assign(myFont);
finally
FreeAndNil(myFont);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Label1.Font.Assign(GetFont);
end;
Code End
|
I assume you want an answer as to why. <g>
Result does not point to an object. You need to assign an object to it
before calling Assign. I assume you want to put a Font property on an
object. Generally it's done like so:
TMyObject = class(TObject)
private
fFont: TFont;
protected
procedure SetFont(const aValue: TFont);
function GetFont: TFont;
public
procedure AfterConstruction; override;
procedure BeforeDestruction; override;
property Font: TFont read GetFont write SetFont;
end;
procedure TMyObject.AfterConstruction;
begin
inherited;
fFont := TFont.Create;
end;
procedure TMyObject.BeforeDestruction;
begin
fFont.Free;
inherited;
end;
procedure TMyObject.SetFont(const aValue: TFont);
begin
fFont.Assign(aValue);
end;
function TMyObject.GetFont: TFont;
begin
result := fFont;
end;
If you want to set default values, like the point size, that would be done
in the AfterConstruction method. If you have a method called GetFont that
creates a TFont and assigns it to result (i.e. result := TFont.Create),
then it will leak memory when used as in Button1Click.
HTH,
Harley Pebley
|
|
| Back to top |
|
 |
Joanna Carter (TeamB) Guest
|
Posted: Fri Nov 21, 2003 7:58 am Post subject: Re: Results.Assign crashes. Why? |
|
|
Harley Pebley wrote:
| Quote: | If you want to set default values, like the point size, that would be
done in the AfterConstruction method. If you have a method called
GetFont that creates a TFont and assigns it to result (i.e. result :=
TFont.Create), then it will leak memory when used as in Button1Click.
|
Harley, unfortunately, your explanation does not answer David's question; he
was asking about returning an object from a function.
Also you do not need to override the AfterConstruction and BeforeDestruction
methods to do what you demonstrated, the constructor and destructor are
there for that kind of thing and it is much more normal to override them.
:-)
Joanna
--
Joanna Carter (TeamB)
Consultant Software Engineer
TeamBUG support for UK-BUG
TeamMM support for ModelMaker
|
|
| Back to top |
|
 |
Harley Pebley Guest
|
Posted: Fri Nov 21, 2003 7:27 pm Post subject: Re: Results.Assign crashes. Why? |
|
|
| Quote: | | If you want to set default values, like the point size, that would be
| done in the AfterConstruction method. If you have a method called
| GetFont that creates a TFont and assigns it to result (i.e. result :=
| TFont.Create), then it will leak memory when used as in Button1Click.
Harley, unfortunately, your explanation does not answer David's question; he
was asking about returning an object from a function.
|
Oh, I think my explanation did answer his question. But it wasn't the quote
above. ;-)
| Quote: | Also you do not need to override the AfterConstruction and BeforeDestruction
methods to do what you demonstrated, the constructor and destructor are
there for that kind of thing and it is much more normal to override them.
|
Hmm, normal describes such a broad range. <g> Of course one can implement
constructors and destructors which do this, but, if they're static they
don't work too well with factories. AfterConstruction and BeforeDestruction
work in both cases. (I know you know all this, this is for the lurkers. :-)
Cheers,
Harley Pebley
|
|
| Back to top |
|
 |
Martin James Guest
|
Posted: Fri Nov 21, 2003 7:46 pm Post subject: Re: Results.Assign crashes. Why? |
|
|
AfterConstruction and BeforeDestruction
| Quote: | work in both cases. (I know you know all this, this is for the lurkers.
:-)
|
My dog shows no interest in returning objects from functions.
Martin
|
|
| Back to top |
|
 |
Bob Dawson Guest
|
Posted: Fri Nov 21, 2003 9:56 pm Post subject: Re: Results.Assign crashes. Why? |
|
|
"Martin James" wrote
| Quote: |
My dog shows no interest in returning objects from functions.
|
I take it your dog's not into factory patterns.
bobD
|
|
| Back to top |
|
 |
Joanna Carter (TeamB) Guest
|
Posted: Fri Nov 21, 2003 10:02 pm Post subject: Re: Results.Assign crashes. Why? |
|
|
Bob Dawson wrote:
| Quote: | I take it your dog's not into factory patterns.
|
But I bet he has a fascination for trees :-)
Joanna
--
Joanna Carter (TeamB)
Consultant Software Engineer
TeamBUG support for UK-BUG
TeamMM support for ModelMaker
|
|
| Back to top |
|
 |
Bryan Crotaz Guest
|
Posted: Sat Nov 22, 2003 12:12 am Post subject: Re: Results.Assign crashes. Why? |
|
|
| Quote: |
AfterConstruction and BeforeDestruction
work in both cases. (I know you know all this, this is for the lurkers.
:-)
My dog shows no interest in returning objects from functions.
|
That's lurchers you're a-thinkin' of.
Bryan
|
|
| Back to top |
|
 |
Woody (TMW) Guest
|
Posted: Sat Nov 22, 2003 11:04 pm Post subject: Re: Results.Assign crashes. Why? |
|
|
"Bryan Crotaz" <bryan@%nospam%ivisionsystems.com> wrote
| Quote: |
AfterConstruction and BeforeDestruction
work in both cases. (I know you know all this, this is for the
lurkers.
:-)
My dog shows no interest in returning objects from functions.
That's lurchers you're a-thinkin' of.
|
"You rang"
Lurch - Addams Family
--
Woody (TMW)
Freeware Page: http://users.eonet.net/woodytmw
All computers wait at the same speed.
|
|
| Back to top |
|
 |
|