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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Freeing TStringlists and Functions

Status
Not open for further replies.

sggaunt

Programmer
Jul 4, 2001
8,620
0
0
GB
Tower's answer to the 'searching in a string' question got me thinking about this again.

It is possible to return the value of a TStringlist in 3 ways

1. You can pass it into a procedure as a var parameter, I assume this passes a pointer to the list so there is no need to 'create' it within the procedure, and freeing it within the procedure is plainly wrong.

2. A Function can return as Result a Tstringlist created internally, but is the memory freed? You cannot 'free' the Tstringlist within the function.
Local variable are not normally 'static' so as
Towerbase's function uses the Tstringlist internally (nothing is passed out),then he frees it when its use is over, but is this necessary?

3. A Function can return as Result a TStringlist passed in as a var parameter 'Bad form' to do this?
But what are the implications?

Steve: Delphi a feersum engin indeed.
 
sggaunt,
2: andrew's function returns a string so it is correct to free the stringlist after passing the string to the result variable.

response to 1 and 3:

the tumbrule is that the creator of an object is also the destroyer of an object. so passing a sringlist object into a function and freeing it in this function is generally a very bad idea...

I switched over my memory manager to fastmm4 (now default in D2006) instead of borland's and one of the advantages is that is reports memory leaks.

take a look :
Cheers,
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