Re: RE : [fpc-pascal] How are Assigned, Free, Nil and Destroy related?

2011-10-22 Thread Frank Church
On 22 October 2011 08:14, Ludo Brands ludo.bra...@free.fr wrote:

  Nil is not a routine, it is a value, it means that the object
  is empty, it does not exist / is not allocated. Nil in
  existing implementations that I know is represented by the value zero.
 
  The typical life-cycle of a object is:
 
  MyObject := TMyObject.Create;
  try
MyObject.DoSomething();
  finally
MyObject.Free;
  end;
 

 One pitfall: the variable MyObject before MyObject := TMyObject.Create; is
 undefined and not necessarily nil. Variables are not initialized by default
 and can contain anything. In general, MyObject.Free does not set MyObject
 to
 nil neither.  Good practice is to initialize pointer variables to nil and
 resetting them to nil after freeing them whenever assigned() or nil is
 going to be used.


Does that mean that Free itself reclaims the memory used by the object's
fields and properties but does not release the memory used by the TObject or
pointer itself, where as setting it to nil or executing Destroy does, or
does Destroy do something different?

Ludo

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




-- 
Frank Church

===
http://devblog.brahmancreations.com
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal

RE : RE : [fpc-pascal] How are Assigned, Free, Nil and Destroy related?

2011-10-22 Thread Ludo Brands
 



Does that mean that Free itself reclaims the memory used by the object's
fields and properties but does not release the memory used by the TObject or
pointer itself, where as setting it to nil or executing Destroy does, or
does Destroy do something different?

 

All memory is released including TObject but the variable pointing to the
TObject (fe: SCStrings) isn't set to nil. It still points to where the
TObject was. Warning: setting a TObject variable to nil does not free the
object unless it is reference counted! Strings and COM style interfaces are
reference counted. Pascal is not VB.
 
Ludo
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Re: RE : [fpc-pascal] How are Assigned, Free, Nil and Destroy related?

2011-10-22 Thread Felipe Monteiro de Carvalho
On Sat, Oct 22, 2011 at 9:36 AM, Frank Church vfcli...@gmail.com wrote:
 Does that mean that Free itself reclaims the memory used by the object's
 fields and properties but does not release the memory used by the TObject or
 pointer itself, where as setting it to nil or executing Destroy does, or
 does Destroy do something different?

No, you got parts of it wrong:

*Free calls Destroy which releases the memory of the object itself.

*Nothing releases the memory of the pointer to the object. You don't
want to release the 4 or 8 bytes of the pointer. If you did release
it, then your application would crash when trying to read the pointer
to check if it is nil.

*Destroy implements releasing the memory of the object. Free calls Destroy.

*Setting the object to nil does just that. It changes the pointer to
the object to have the value nil (zero). It does not release the
pointer.

-- 
Felipe Monteiro de Carvalho
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal