Recent

Author Topic: Saving TFont  (Read 5945 times)

aussieian

  • New Member
  • *
  • Posts: 47
Saving TFont
« on: October 28, 2010, 07:41:57 pm »
I need to save the FONT properties of objects before changing them,
so tried the following:

var

   OldFont : TFont;

Begin

   OldFont.Name := '';

   or

   oldfont := TheImage.Canvas.Font

The compiler accepts the code, but when it executes, the following
error occurs:

" project xxx raised exception class external 'error SIGSEGV'"

The problem doesn't appear to happen if OldFont is a LOCAL variable, only when I try to make it a GLOBAL variable.

I'm using the Windows 64 version.

Any suggestions appreciated.

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Saving TFont
« Reply #1 on: October 28, 2010, 07:47:25 pm »
Code: [Select]
var
   var
   OldFont : TFont;
Begin
   OldFont := TFont.Create;
   OldFont := Font;
   ShowMessage(OldFont.Name);
   OldFont.Free;
end;               
« Last Edit: October 28, 2010, 08:06:06 pm by typo »

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12202
  • Debugger - SynEdit - and more
    • wiki
Re: Saving TFont
« Reply #2 on: October 28, 2010, 08:36:37 pm »
lets make it more correct
Code: [Select]
   OldFont := TFont.Create;
   OldFont.assign(Font);
 

OldFont can be local or global or whatever. And you must free OldFont ourself or memory leaks.

Assign copies all the attributes from one font to another

Reason is:
"Font" is an object. and objects are internally pointers.

therefore:
MyFont := SomeFont;
SomeFont.Free;

Since MyFont simply points to the same memory as SomeFont, once SomeFont is gone MyFont points to random data.

aussieian

  • New Member
  • *
  • Posts: 47
Re: Saving TFont
« Reply #3 on: October 28, 2010, 08:54:02 pm »
Thanks for the prompt suggestions from both typo and Martin_fr.

I'm obviously missing something because the suggested solutions don't work.

I have made the TFont variable a global variable.

I put in the FORMCREATE procedure

          OldFont.Create

However I still get the same error generated on the CREATE itself.

Am I possibly missing a directive or something less obvious?

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1934
Re: Saving TFont
« Reply #4 on: October 28, 2010, 09:02:21 pm »
Am I possibly missing a directive or something less obvious?

You are probably missing the basics of Object Pascal ;-)
It's written above:
OldFont := TFont.Create;

not OldFont.Create

aussieian

  • New Member
  • *
  • Posts: 47
Re: Saving TFont
« Reply #5 on: October 28, 2010, 10:25:22 pm »
Thanks very much Theo.

You are correct. Shows what happens if you rush and don't pay attention to the advice.

Cheers.


 

TinyPortal © 2005-2018