Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delphi panics when returning a custom class in a function?

Status
Not open for further replies.

jamesp0tter

Programmer
Feb 20, 2003
119
PT
I have defined a custom class:

Code:
type
  TWhtv = class
    one: string;
    two: string;
  end;

and the following code

Code:
function abc: TWhtv;
var
  u: TWhtv;
begin
  u.one := 'first';
  u.two := 'second';
  result := u;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  o: TWhtv;         
  s: string;
begin
  o := abc;
  s := o.one + o.two;
  caption := s;
end;

When I execute this code, caption changes to 'firstsecond' as expected, but when I exit the application it panics with an Access Violation!

I tried only with

Code:
procedure TForm1.Button1Click(Sender: TObject);
var
  o: TWhtv;         
  s: string;
begin
  o := abc;
  s := o.one;
  caption := s;
end;

where caption will only become 'first' (asking only one of the variables of TWhtv), and the caption changes accordingly and NO access violation!

So, Delphi panics when I ask for more than one var of the returned class? Or am I doing something wrong?

Thanks!

PS: using Delphi 7

jamesp0tter
 

My Delphi is getting rusty, but shouldn't you be using something like
Code:
function abc: TWhtv;
var
  u: TWhtv;
begin
  [b][COLOR=blue]u := TWhtv.Create[/color][/b];
  u.one := 'first';
  u.two := 'second';
  result := u;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  o: TWhtv;         
  s: string;
begin
  o := abc;
  s := o.one + o.two;
  caption := s;
  [b][COLOR=blue]o.Free;[/color][/b]
end;

Personally, I don't like creating things in functions that have to be destroyed elsewhere. I would do something more like this:
Code:
Procedure abc (var u:TWhtv);
begin
  u.one := 'first';
  u.two := 'second';
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  o: TWhtv;         
begin
  o := TWhtv.Create;
  abc(o);
  caption := o.one + o.two;
  o.Free;
end;
Looks a bit cleaner, doesn't it?

 
whenever you are dealing with classes, you need to call the constructor.

so let's take your example :

Code:
type
  TWhtv = class(TObject) //this not really necessary, but it is more explicit
  private 
    one: string;
    two: string;
  public 
    construtor Create; 
  end;

implementation
// class implementation

constructor TWhtv.Create;
begin
 // use the constructor to init your class vars
 One:='';
 Two:='';
end;

correct class utilization:

Code:
CODE
function abc: TWhtv;
begin
 Result:=TWhtv.Create;// very important
 Result.one := 'first';
 Result.two := 'second';
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  o: TWhtv;         
  s: string;
begin
  o := abc;
  s := o.one + o.two;
  caption := s;
  FreeAndNil(o); // don't forget to deallocate self created objects
end;

Hope this helps,
Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Damn you zathras!

I guess I need to speed up my typing

[spin]

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 

[lol]

I usually like to test my code before posting here, but in this case I didn't bother.

BTW, do you agree with me that it is preferable to keep the construction and destruction of objects close together. I realize that the function is just returning a pointer to the memory where the class resides and will work just fine, but somehow it just feels wrong to me.

 
yeah, I do agree.
I prefer creating the class, then try to do something with the class and finally free the class. with this workmethod I never have memoryleaks. if this is not possible, I use objectlists.

/Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top