BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

anything wrong when free

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi OO design
View previous topic :: View next topic  
Author Message
Ronaldo Rezende Vilela Lu
Guest





PostPosted: Mon Jul 14, 2003 10:57 am    Post subject: anything wrong when free Reply with quote



my tstudent class is derived from TComponent.
I'm sending as Owner, the Form of the button.
So, when the form (self) has been destroyed,
the form will try to destroy my student object,
that already has been destroyed. Why any error
has been generated when the form is destroyed?

var student: tstudent;
begin
student:=tstudent.create(self);
student.free;
end;


--
Ronado Rezende Vilela Luiz
Uberaba, MG - Brazil
Back to top
Joanna Carter
Guest





PostPosted: Mon Jul 14, 2003 12:40 pm    Post subject: Re: anything wrong when free Reply with quote



Ronaldo Rezende Vilela Luiz wrote:

Quote:
var student: tstudent;
begin
student:=tstudent.create(self);
student.free;
end;


If this is your exact code then you are freeing off the Student as soon as
you have created it. Passing the form as the Owner will not keep alive
anything that is being freed.

Apart from that, I agree with Jim that deriving from TComponent is not a
good idea. what's wrong with passing a Student object to the form, holding
it in a private field of the form and freeing it off in the destructor of
the form??

TStudent = class
private
// fields and accessors, etc
public
// properties and methods, etc
end;

TMyForm = class(TForm)
// Delphi components, etc
private
fStudent: TStudent;
public
constructor Create(const Student: TStudent); reintroduce; virtual;
destructor Destroy; override;
end;

constructor TMyForm.Create(const Student: TStudent);
begin
inherited Create(nil)
fstudent := Student;
end;

destructor TMyForm.Destroy;
begin
fStudent.Free;
inherited Destroy;
end;

Joanna

--
Joanna Carter
Consultant Software Engineer
TeamBUG support for UK-BUG
TeamMM support for ModelMaker



Back to top
Ronaldo Rezende Vilela Lu
Guest





PostPosted: Mon Jul 14, 2003 2:10 pm    Post subject: Re: anything wrong when free Reply with quote



Jim Cooper wrote:

Quote:

Passing the form in as the owner here:

student:=tstudent.create(self);

means that when the form is destroyed, it thinks it owns the student and
tries to free it. It was implemented this way to make handling
components on forms easier. However you have already freed the student
object:

student.free;


what I don't understand is: I already freed the student. So, when
the form try to free student, an error must be generated, because
the student already has been freed. Why this error hasn't be
generated?

Back to top
Ronaldo Rezende Vilela Lu
Guest





PostPosted: Mon Jul 14, 2003 2:32 pm    Post subject: Re: anything wrong when free Reply with quote

Quote:
Apart from that, I agree with Jim that deriving from TComponent is not a
good idea.

I'm deriving from TComponent, because some times, I will pass the Form as
the owner of the object. In this case, the destruction of the object will
be automatically.

Quote:
what's wrong with passing a Student object to the form, holding
it in a private field of the form and freeing it off in the destructor of
the form??

I agree that the best pratice to do is develope the system using an design
pattern such MVP and a OPF. My problem is the OPF. I'm developing in Kylix.
I don't have time and money to create or by an OPF. So, I'm developing with
ClientDataSets, DataModules and CLX Data Controls.

Each of my object has:
two datamodules (server and client. but in real world my application has
only two phisical tier. My application will create only one
serverDataModule and one ClientDataModule for each object created.);
* a register property (clientdataset with all fields used by formEditor, to
insert and edit records. This cds requires the primary key to locate the
record or null values when is used to insert record.);
* a index property (clientdataset with only fields necessary to find a
record. e.g.: the name of the customer. this is used by the SearchForm);
* a form used to edit and insert records;
* a a form used to locate records; (in this form, several objects of the
same class can be created, to create several EditForms.
When I create forms, I pass the DataSource of the object as parameter to
form. So, the form will use the dataSource of the object that is creating
the form.
So, in this model that I'm trying to create, the object has the form, and
not the form has the object. Each "DBForm" will have the DataSource, that
will receive as a parameter of DBForm's constructor.

Is there any sugestions or comments in this model of development?


Back to top
Jim Cooper
Guest





PostPosted: Mon Jul 14, 2003 3:40 pm    Post subject: Re: anything wrong when free Reply with quote


Quote:
Why this error hasn't be generated?

Luck. It'll come back and bite you eventually, particularly if you do
anything in the OnFormClose event handler.

Cheers,
Jim Cooper

____________________________________________

Jim Cooper [email]jcooper (AT) tabdee (DOT) ltd.uk[/email]
Tabdee Ltd http://www.tabdee.ltd.uk

TurboSync - Connecting Delphi with your Palm
____________________________________________

Back to top
Mauricio I. Magni
Guest





PostPosted: Mon Jul 14, 2003 3:49 pm    Post subject: Re: anything wrong when free Reply with quote

Hello

The Form doesn't raise an error because the Component.Destroy execute the
following sentence:
if FOwner <> nil then FOwner.RemoveComponent(Self);

When the form.destroy method is called, the form's component list doesn't
have a reference to the Component.

Mauricio.


"Ronaldo Rezende Vilela Luiz" <ronaldinho79 (AT) bol (DOT) com.br> wrote

Quote:
Jim Cooper wrote:


Passing the form in as the owner here:

student:=tstudent.create(self);

means that when the form is destroyed, it thinks it owns the student and
tries to free it. It was implemented this way to make handling
components on forms easier. However you have already freed the student
object:

student.free;


what I don't understand is: I already freed the student. So, when
the form try to free student, an error must be generated, because
the student already has been freed. Why this error hasn't be
generated?



Back to top
Ronaldo Rezende Vilela Lu
Guest





PostPosted: Wed Jul 16, 2003 4:41 pm    Post subject: Re: anything wrong when free Reply with quote

Joanna Carter wrote:

Quote:
Apart from that, I agree with Jim that deriving from TComponent is not a
good idea.

Why not deriving from TComponent?

Back to top
Jim Cooper
Guest





PostPosted: Wed Jul 16, 2003 6:14 pm    Post subject: Re: anything wrong when free Reply with quote


Quote:
Why not deriving from TComponent?

Because unless there is a need to be able to drop a TSudent object on a
form as a (perhaps non-visual) control, then there is nothing to be
gained by doing that. One of the heuristics for good OO design is to
push things as far up the inheritance hierarchy (ie towards TObject) as
possible. If you want to use RTTI on TSudent objects, then inherit from
TPersistent, otherwise use TObject.

Cheers,
Jim Cooper

____________________________________________

Jim Cooper [email]jcooper (AT) tabdee (DOT) ltd.uk[/email]
Tabdee Ltd http://www.tabdee.ltd.uk

TurboSync - Connecting Delphi with your Palm
____________________________________________

Back to top
Ronaldo Rezende Vilela Lu
Guest





PostPosted: Wed Jul 16, 2003 6:59 pm    Post subject: Re: anything wrong when free Reply with quote

Jim Cooper wrote:

Quote:

Why not deriving from TComponent?

Because unless there is a need to be able to drop a TSudent object on a
form as a (perhaps non-visual) control, then there is nothing to be
gained by doing that. One of the heuristics for good OO design is to
push things as far up the inheritance hierarchy (ie towards TObject) as
possible. If you want to use RTTI on TSudent objects, then inherit from
TPersistent, otherwise use TObject.

In the first time, I was derived my TBO class from nothing.
What's better?
TBO = class(TObject)
or
TBO = class
?

--
Ronado Rezende Vilela Luiz
Uberaba, MG - Brazil

Back to top
Jean-Francois Nifenecker
Guest





PostPosted: Wed Jul 16, 2003 7:12 pm    Post subject: Re: anything wrong when free Reply with quote

Ronaldo Rezende Vilela Luiz wrote:
Quote:

In the first time, I was derived my TBO class from nothing.
What's better?
TBO = class(TObject)
or
TBO = class
?

It's the exact same thing: class without parameter means class(TObject)



Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi OO design All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.