Re: [fpc-pascal] Basic question about TStringList

2008-03-28 Thread Crause, Christo (JC)
 From: [EMAIL PROTECTED]

 As a consequence I have another question. Suppose I have a function  
 that returns a TStringList:
 
 Myfunction(): TStringList;
 
 I must have inside a line like:
 
 Result:=TStringList.Create;
 
 Let A be a TStringList, I have two ways to catch the result 
 of my function:
 
 A:=Myfunction();
 
 or
 
 A.Assign(Myfunction());
 
 In the first case, if do A.Free, I release the memory 
 allocated by the  
 function. What's arriving in the second case? I can do A.Free, but  
 does that action will also release the memory allocated by 
 the function?
 
 By the way, do you have some tricks to detect this kind of error?

This consequence of returning references to newly created objects is a
very subtle one and I have seen some spectacular memory leaks caused by
this practice.  My programming convention is to try to always call
.Create
and .Free in the same context if possible i.e.

procedure Myfunction(const ASL: TStringList);
begin
// add stuff to ASL
end;

And in code using this function:

SL := TStringList.Create;
Myfunction(SL);
// do stuff
SL.Free;

Not quite bullet proof yet, since one can call Myfunction without
actually
instantiating SL, but at least that should give you a runtime error that
should be easy to trace.

Regards
Christo Crause

NOTICE: Please note that this eMail, and the contents thereof, 
is subject to the standard Sasol eMail legal notice which may be found at: 
http://www.sasol.com/legalnotices   
   

If you cannot access the legal notice through the URL attached and you wish 
to receive a copy thereof please send an eMail to 
[EMAIL PROTECTED]

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


[fpc-pascal] Basic question about TStringList

2008-03-27 Thread g . marcou

Hi,

I seek help to clarify a very basic use of TStringList.

Let A and B be TStringLists. What is the difference between:

A:=B;

and

A.Assign(B); ?

Thanks in advance for your help,
Gilles Marcou


___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Basic question about TStringList

2008-03-27 Thread Joost van der Sluis
Op donderdag 27-03-2008 om 11:35 uur [tijdzone +0100], schreef
[EMAIL PROTECTED]:
 Hi,
 
 I seek help to clarify a very basic use of TStringList.
 
 Let A and B be TStringLists. What is the difference between:
 
 A:=B;
 
 and
 
 A.Assign(B); ?

That's more a basic(?) question about Pascal.

When you do A:=B; then A has become equal to B. That means for example
that if you add an new string to B, this new string will also be in A.
After all A and B are the same. 

Note that if A contained a TStringList that you can not acces it anymore
through A, but it's still present in memory.

When you do A.Assign(B); then all strings in A are cleared and
afterwards all strings in B are copied to A. Further all relevant
properties of B are also set to A.

Note that in this case, A should already contain a TStringList, and if
you add values to B after the assignment, those strings will not appear
in B.

Joost

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Basic question about TStringList

2008-03-27 Thread Andreas Berger



Hi,

I seek help to clarify a very basic use of TStringList.

Let A and B be TStringLists. What is the difference between:

A:=B;

and

A.Assign(B); ?



That's more a basic(?) question about Pascal.

When you do A:=B; then A has become equal to B. That means for example
that if you add an new string to B, this new string will also be in A.
After all A and B are the same. 


Note that if A contained a TStringList that you can not acces it anymore
through A, but it's still present in memory.

When you do A.Assign(B); then all strings in A are cleared and
afterwards all strings in B are copied to A. Further all relevant
properties of B are also set to A.

Note that in this case, A should already contain a TStringList, and if
you add values to B after the assignment, those strings will not appear
in B.
  

In other words: When you declare
  A, B : TStringList;
you actually allocated two pointer to TStringList since classes are 
inherently pointers. When you do A:=B you now have A pointing to the 
same memory space as B. It's a pity you can't do A^ := B^;


Andreas

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Basic question about TStringList

2008-03-27 Thread g . marcou

Hi,

thanks for your very clear answer. In fact this is chilling me as I am  
thinking to the huge amount of memory leaks that I certainly have in  
my developements...


This means that whenever the assignement operator := is used with  
TStringLists, the recieving list shall never be created!


As a consequence I have another question. Suppose I have a function  
that returns a TStringList:


Myfunction(): TStringList;

I must have inside a line like:

Result:=TStringList.Create;

Let A be a TStringList, I have two ways to catch the result of my function:

A:=Myfunction();

or

A.Assign(Myfunction());

In the first case, if do A.Free, I release the memory allocated by the  
function. What's arriving in the second case? I can do A.Free, but  
does that action will also release the memory allocated by the function?


By the way, do you have some tricks to detect this kind of error?

Thanks again,
Gilles Marcou



___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Basic question about TStringList

2008-03-27 Thread Jonas Maebe


On 27 Mar 2008, at 13:27, [EMAIL PROTECTED] wrote:

Let A be a TStringList, I have two ways to catch the result of my  
function:


A:=Myfunction();

or

A.Assign(Myfunction());

In the first case, if do A.Free, I release the memory allocated by  
the function. What's arriving in the second case? I can do A.Free,  
but does that action will also release the memory allocated by the  
function?


No.


By the way, do you have some tricks to detect this kind of error?


Compile with -ghl


Jonas
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal