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 

How to parameters without leaking memory?

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi OO design
View previous topic :: View next topic  
Author Message
Aaron Miles
Guest





PostPosted: Sun Dec 05, 2004 9:10 am    Post subject: How to parameters without leaking memory? Reply with quote



function GetCadParam(inRado: tRadoConnection; szSetName, szListName,
szCfgName, szParamName: string; var outszParamValue: string): boolean;
var
szSql: string;
szOrigSql: string;
rsParams: recordset;
intGetCadParamRecords: OleVariant;
begin
szOrigSql := 'select param_value from cfg_param_lists where
upper(set_name) = ''%s'' and upper(list_name) = ''%s'' and upper(cfg_name) =
''%s'' and upper(param_name) = ''%s''';
GetCadParam := False;
szSql := format(szOrigSql, [uppercase(szsetName), uppercase(szListName),
uppercase(szCfgName), uppercase(szParamName)]);
try
begin
rsparams := inRado.Execute(szSql, intGetCadParamRecords, - 1);
if intGetCadParamRecords > 0 then
begin
outszParamValue := vartostr(rsParams.Fields[0].Value);
GetCadParam := True;
end
else
begin
szSql := format(szOrigSql, [uppercase(szsetName),
uppercase(szListName), 'DEFAULT', uppercase(szParamName)]);
rsparams := inRado.Execute(szSql, intGetCadParamRecords, - 1);
if intGetCadParamRecords > 0 then
begin
outszParamValue := vartostr(rsParams.Fields[0].Value);
GetCadParam := True;
end
else
outSzParamValue := '';
GetCadParam := False;
end;
end
finally
begin
rsParams.Close;
end
end;
end;


Back to top
Aaron Miles
Guest





PostPosted: Sun Dec 05, 2004 9:18 am    Post subject: Re: How to parameters without leaking memory? Reply with quote



All,

One of my friends who is learning DELPHI with me suggested changing the
parameter definition for GetCadParam from

function GetCadParam(inRado: tRadoConnection; szSetName, szListName,
szCfgName, szParamName: string; var outszParamValue: string): boolean;

to

function GetCadParam(var inRado: tRadoConnection; szSetName, szListName,
szCfgName, szParamName: string; var outszParamValue: string): boolean;

Does that sound right?

Cheers,
Aaron Miles

"Aaron Miles" <miles3719(at)msn.com> wrote

Quote:
function GetCadParam(inRado: tRadoConnection; szSetName, szListName,
szCfgName, szParamName: string; var outszParamValue: string): boolean;
var
szSql: string;
szOrigSql: string;
rsParams: recordset;
intGetCadParamRecords: OleVariant;
begin
szOrigSql := 'select param_value from cfg_param_lists where
upper(set_name) = ''%s'' and upper(list_name) = ''%s'' and upper(cfg_name)
=
''%s'' and upper(param_name) = ''%s''';
GetCadParam := False;
szSql := format(szOrigSql, [uppercase(szsetName), uppercase(szListName),
uppercase(szCfgName), uppercase(szParamName)]);
try
begin
rsparams := inRado.Execute(szSql, intGetCadParamRecords, - 1);
if intGetCadParamRecords > 0 then
begin
outszParamValue := vartostr(rsParams.Fields[0].Value);
GetCadParam := True;
end
else
begin
szSql := format(szOrigSql, [uppercase(szsetName),
uppercase(szListName), 'DEFAULT', uppercase(szParamName)]);
rsparams := inRado.Execute(szSql, intGetCadParamRecords, - 1);
if intGetCadParamRecords > 0 then
begin
outszParamValue := vartostr(rsParams.Fields[0].Value);
GetCadParam := True;
end
else
outSzParamValue := '';
GetCadParam := False;
end;
end
finally
begin
rsParams.Close;
end
end;
end;





Back to top
Philip Jander
Guest





PostPosted: Sun Dec 05, 2004 9:48 am    Post subject: Re: How to parameters without leaking memory? Reply with quote



Hi Aaron,

Quote:
One of my friends who is learning DELPHI with me suggested changing the
parameter definition for GetCadParam from

function GetCadParam(inRado: tRadoConnection; szSetName, szListName,
szCfgName, szParamName: string; var outszParamValue: string): boolean;

to

function GetCadParam(var inRado: tRadoConnection; szSetName, szListName,
szCfgName, szParamName: string; var outszParamValue: string): boolean;

No, as far as I can see, there is no need to do this change.

Changing inRado to a var parameter means that the pointer it contains is
not copied anymore but rather passed by reference. I.e. if you were to
call
GetCadParam(someRadoConnection,...);
and then assign e.g.
inRado:=nil;
your outside variable someRadoConnection would become nil, too (leading
to a memory leak if you don't free it anywhere). However, I cannot see
any assignment made to inRado in your code, so the var parameter is not
necesary.

But for just changing fields and properties of an object, you don't need
to pass the pointer by reference. Actually, the pointer *is* a
reference, so object instances are always passed by reference :)

Your initial subject indicates that you or your friend fear a memory
leak in your original code; where would that be?

Cheers,
Phil


Back to top
Aaron Miles
Guest





PostPosted: Mon Dec 06, 2004 12:23 am    Post subject: Re: How to parameters without leaking memory? Reply with quote

Hi Phil,

Thanks for the detail ..... I will play with it some-more ... I just saw the
memory usage jumping from 17mb at idle to 22 or so after running 50
iterations (without updating the memo box).

I will continue to play with it and see what I find.

I was just primarily concerned about not leaving objects sitting in limbo
without being cleared up, as the RADO functions are called quite often, to
access a database that we have. I notice you email address is .DE, if you
are in germany the system we use here at the RACV (Royal Automobile Club of
Victoria) is the same Intergraph product that ADAC uses.

(leading to a memory leak if you don't free it anywhere)
leads me to ask another question if possible. I am a little bit fuzzy on
the rules for freeing / releasing of objects. I understand that objects are
destroyed when the become out of scope like when a procedure / function ends
etc, but is there any instances where you would need to free an object
manually. I don't any functions like getmem etc.

I have bought Mastering Delphi 7 which I am reading and quite enjoying.

I hope this message finds you well

Cheers,
Aaron


Back to top
Bob Dawson
Guest





PostPosted: Mon Dec 06, 2004 12:49 am    Post subject: Re: How to parameters without leaking memory? Reply with quote

"Aaron Miles" wrote
Quote:
leads me to ask another question if possible. I am a little bit fuzzy on
the rules for freeing / releasing of objects. I understand that objects
are
destroyed when the become out of scope like when a procedure / function
ends


Absolutely not--that isn't true of Delphi win32 objects.

Memory leak: 'ao' goes out of scope at the end of this procedure but it
isn't freed--whatever memory it occupies won't be returned to the system:

procedure MyObject.DoSomething;
var
ao : TAnotherObject;
begin
ao := TAnotherObject.Create;
ao.DoWhatever;
end;

Correct procedure: 'ao' gets freed deterministically and its memory released
even if the DoWhatever procedure throws an exception.

procedure MyObject.DoSomething;
var
ao : TAnotherObject;
begin
ao := TAnotherObject.Create;
try
ao.DoWhatever;
finally
ao.Free;
end;
end;

Things are different with interfaces--they are reference counted and going
out of scope will trigger the destruction of the implementing object--but
you really need to understand how object lifetime works before getting into
interfaces or mixing the two.

Most basic rule of thumb: if you created it, you destroy it (usually by
calling Free).
Most common exception: Objects can be given a concept of ownership so that
when the owning object is freed, it frees all the other objects it
contains--for example, if I wrote
myEdit := TEdit.Create(aForm)
then I'm creating the edit component instance, but I'm also passing
responsiblity for it to the form, which will add the instance to an internal
component collection and free it when its own destruction is triggered.

Quote:
I have bought Mastering Delphi 7 which I am reading and quite enjoying.

That's a good reference--also look at his free online books (Essential
Pascal, Essential Delphi) at
http://www.marcocantu.com/epascal/default.htm

bobD



Back to top
Aaron Miles
Guest





PostPosted: Mon Dec 06, 2004 12:56 am    Post subject: Re: How to parameters without leaking memory? Reply with quote

Hi Bob,

Thanks for that .... points me in the right direction :)

Cheers,
Aaron

"Bob Dawson" <RBDawson (AT) prodigy (DOT) net> wrote

Quote:
"Aaron Miles" wrote
leads me to ask another question if possible. I am a little bit fuzzy
on
the rules for freeing / releasing of objects. I understand that objects
are
destroyed when the become out of scope like when a procedure / function
ends

Absolutely not--that isn't true of Delphi win32 objects.

Memory leak: 'ao' goes out of scope at the end of this procedure but it
isn't freed--whatever memory it occupies won't be returned to the system:

procedure MyObject.DoSomething;
var
ao : TAnotherObject;
begin
ao := TAnotherObject.Create;
ao.DoWhatever;
end;

Correct procedure: 'ao' gets freed deterministically and its memory
released
even if the DoWhatever procedure throws an exception.

procedure MyObject.DoSomething;
var
ao : TAnotherObject;
begin
ao := TAnotherObject.Create;
try
ao.DoWhatever;
finally
ao.Free;
end;
end;

Things are different with interfaces--they are reference counted and going
out of scope will trigger the destruction of the implementing object--but
you really need to understand how object lifetime works before getting
into
interfaces or mixing the two.

Most basic rule of thumb: if you created it, you destroy it (usually by
calling Free).
Most common exception: Objects can be given a concept of ownership so that
when the owning object is freed, it frees all the other objects it
contains--for example, if I wrote
myEdit := TEdit.Create(aForm)
then I'm creating the edit component instance, but I'm also passing
responsiblity for it to the form, which will add the instance to an
internal
component collection and free it when its own destruction is triggered.

I have bought Mastering Delphi 7 which I am reading and quite enjoying.

That's a good reference--also look at his free online books (Essential
Pascal, Essential Delphi) at
http://www.marcocantu.com/epascal/default.htm

bobD





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.