* * *

Author Topic: [SOLVED]function returns tmemo. How to  (Read 3101 times)

Leledumbo

  • Hero Member
  • *****
  • Posts: 4294
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: function returns tmemo. How to
« Reply #15 on: August 19, 2012, 03:03:37 am »
Quote
Memo1:= Memo2
AFAIK it follows pointer assignment rule, so it would simply change the pointer value of the first variable to point to the same memo structure, discarding the one Memo1 held previously (memory leak here if you create the memo dynamically and no other references that will free it exist).
Quote
I don't need parent or owner . Function value must be destroyed right afret function calling. So behaviour of my function is same as all ordinal functions. Does function with tmemo type need Free-ing?
You don't need them, but your dynamically created component does. Otherwise, you have to manually Free the created TMemo. Have you read the link I gave? Parent is still required because it reflects the visual hierarchy (where should this component belong to?), while Owner is required for automatic freeing.
Quote
In my code there is no "memo to text" assign. There is "memo  to memo". Why this does not work?
If you still want to use your code, try repainting (call Invalidate) the memo after assignment. Perhaps it's just not updated because class assignment doesn't trigger any painting event.
Quote
Theoretically I can create temporary memo variable (before func's begin-end section) and type result:=tempmemo.text. But such way is more memory-demanding
Nope, that's memory safe since the memo creation is local (you'll free the tempmemo after assignment to result, right?).

anna

  • Sr. Member
  • ****
  • Posts: 304
Re: function returns tmemo. How to
« Reply #16 on: August 19, 2012, 10:40:15 am »
So I'm using next code for main unit:
Code: [Select]
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,

  annaunit;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
begin
  memo1.Text:=funcANNA(edit1.text).text;
end;

end.

...and next code for additional unit for functions:
Code: [Select]
unit annaunit; {$H+}

{$IFDEF UNDER_DEBUG}
    {$R+,Q+,I+}
{$ELSE}
    {$R-,Q-,I-}
{$ENDIF}

interface
uses StdCtrls;

function funcANNA(const inside: String): TMemo;

implementation
 
function funcANNA(const inside: String): TMemo;
begin
    result:=tmemo.create(nil);
    result.text:='';
    result.text:=inside;
end;

initialization
finalization
 
end.

It works fine. But where must I put free procedure for funcANNA ?
WinXP SP3 Pro Russian 32-bit (5.1.2600)

Avishai

  • Hero Member
  • *****
  • Posts: 712
Re: function returns tmemo. How to
« Reply #17 on: August 19, 2012, 10:44:15 am »
One possibility is to put it in the TForm.OnClose event.
Lazarus 1.1 - FPC 2.6.2 - Win7-64

anna

  • Sr. Member
  • ****
  • Posts: 304
Re: function returns tmemo. How to
« Reply #18 on: August 19, 2012, 10:53:29 am »
One possibility is to put it in the TForm.OnClose event.
You meen like this:

procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
  funcAnna(edit1.text).Free;
end;   

But in this case superfluous function call will made!
WinXP SP3 Pro Russian 32-bit (5.1.2600)

anna

  • Sr. Member
  • ****
  • Posts: 304
Re: function returns tmemo. How to
« Reply #19 on: August 19, 2012, 10:55:37 am »
Quote
In my code there is no "memo to text" assign. There is "memo  to memo". Why this does not work?
If you still want to use your code, try repainting (call Invalidate) the memo after assignment. Perhaps it's just not updated because class assignment doesn't trigger any painting event.
Invalidate or RePaint don't make any effect. Does not work.
WinXP SP3 Pro Russian 32-bit (5.1.2600)

Leledumbo

  • Hero Member
  • *****
  • Posts: 4294
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: function returns tmemo. How to
« Reply #20 on: August 19, 2012, 11:24:53 am »
Quote
It works fine. But where must I put free procedure for funcANNA ?
Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
begin
  with funcANNA(edit1.text) do
    try
      memo1.Text:=text;
    finally
      Free;
    end;
end;

Honestly, I'm still confused with your function since it doesn't do anything special. Direct assignment to TMemo.Text object should do the same.
Quote
Invalidate or RePaint don't make any effect. Does not work.
Simple question:
Quote
TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;
Did you write that bold declaration above yourself? Better, attach your .lfm (or write its contents here).

anna

  • Sr. Member
  • ****
  • Posts: 304
Re: function returns tmemo. How to
« Reply #21 on: August 19, 2012, 11:37:27 am »
Did you write that bold declaration above yourself? Better, attach your .lfm (or write its contents here).
I have put them through pallette in design time.
« Last Edit: August 19, 2012, 11:44:27 am by anna »
WinXP SP3 Pro Russian 32-bit (5.1.2600)

eny

  • Hero Member
  • *****
  • Posts: 1000
Re: function returns tmemo. How to
« Reply #22 on: August 19, 2012, 12:42:15 pm »
Lazarus/FPC is not Java.
WinXP Prof SP3 & Win7(64); Lazarus 1.0.2; FPC 2.6.0; 2012-10-09 (#39019)

anna

  • Sr. Member
  • ****
  • Posts: 304
Re: function returns tmemo. How to
« Reply #23 on: August 19, 2012, 12:48:17 pm »
Quote
It works fine. But where must I put free procedure for funcANNA ?
Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
begin
  with funcANNA(edit1.text) do
    try
      memo1.Text:=text;
    finally
      Free;
    end;
end;
But if I use create method inside function , free method must be also inside. Or maybe it's better to put create method outside of funcANNA?

Lazarus/FPC is not Java.
Heh? Java?
« Last Edit: August 19, 2012, 12:52:30 pm by anna »
WinXP SP3 Pro Russian 32-bit (5.1.2600)

Avishai

  • Hero Member
  • *****
  • Posts: 712
Re: function returns tmemo. How to
« Reply #24 on: August 19, 2012, 12:52:13 pm »
If you Free it in funcAnna, it will already be gone before you try to assign Text to Memo1.  It will cause an Error.
Lazarus 1.1 - FPC 2.6.2 - Win7-64

eny

  • Hero Member
  • *****
  • Posts: 1000
Re: function returns tmemo. How to
« Reply #25 on: August 19, 2012, 02:02:06 pm »
WinXP Prof SP3 & Win7(64); Lazarus 1.0.2; FPC 2.6.0; 2012-10-09 (#39019)

anna

  • Sr. Member
  • ****
  • Posts: 304
Re: function returns tmemo. How to
« Reply #26 on: August 19, 2012, 02:17:52 pm »
Don't use funcANNA, use assignment: http://www.templetons.com/brad/alice/language/language5.html
Funny suggestion. Of couse funcANNA is simplification. So I cannot "don't use funcANNA". I have already reached goal, so the issue  is settled
WinXP SP3 Pro Russian 32-bit (5.1.2600)

eny

  • Hero Member
  • *****
  • Posts: 1000
Re: function returns tmemo. How to
« Reply #27 on: August 19, 2012, 02:26:26 pm »
I have already reached goal, so the issue  is settled
:)
WinXP Prof SP3 & Win7(64); Lazarus 1.0.2; FPC 2.6.0; 2012-10-09 (#39019)

geno

  • Full Member
  • ***
  • Posts: 135
Re: [SOLVED]function returns tmemo. How to
« Reply #28 on: August 19, 2012, 06:00:06 pm »
I realize the issue is settled, however, I thought I might throw my own thoughts  into the mix. :D

main form:
Code: [Select]
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  ExtCtrls;

type

  { TForm1 }
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;

implementation

 {$I anna.inc}
 {$R *.lfm}

 procedure TForm1.Button1Click(Sender: TObject);
   var memo1 : TMemo;
 begin
    memo1 :=  funcANNA('this works for me...', self);
    memo1.Parent := Form1;
    memo1.Left := 15;
    memo1.Top := 15;
    memo1.Width := 200;
    memo1.Height := 50;
    memo1.Visible := True;
 end;

end.
and contents of anna.inc
Code: [Select]
function funcANNA(const inside:string; AOwner : TComponent):tMemo;
begin
  result := TMemo.Create(AOwner);
  result.text:=inside;
end;


This way, Memo1 is created dynamically (and with a little tweaking can create as many memos as needed); it is owned by the parent form, so therefore Free is taken care of when the application closes.
version:      Lazarus 1.08  FPC 2.6.2
widget set:  x-86_64-linux-gtk 2
OS:             Fedora 18 / Xfce 4.10

Leledumbo

  • Hero Member
  • *****
  • Posts: 4294
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: [SOLVED]function returns tmemo. How to
« Reply #29 on: August 19, 2012, 07:06:46 pm »
Quote
I have put them through pallette in design time.
Now that explains why you can't change the Memo1 reference... (actually I'm not quite sure about it since I never modify components that I don't create myself, I'll make some tests).
Quote
But if I use create method inside function , free method must be also inside. Or maybe it's better to put create method outside of funcANNA?
Nope, it can be outside. Some library functions also returns a class instance which you have to manually free after use.

If you put the create method outside funcANNA... well then you don't need funcANNA at all %)

 

Recent

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