Board index » delphi » Free vs Destroy?

Free vs Destroy?

According to the document, Free check if object is nil and then calls
Destroy. Free is always recommended but Destroy.

However, based on my experience with D6, all my classes are inherited
from TObject, Free doesn't clean up memory as good as Destroy. My codes
ran into exception error if  Free was used. Once I turned to Destroy,
the exception was gone. Can somebody tell me what difference between
Free and Destroy?

Also, it seems what FreeAndNil does is to call Free and assign nil to
object. So

oTest.Destroy;
Assigned(oTest);        // True
                                  // it makes more sense if it is False.

FreeAndNil(oTest);
Assigned(oTest);     // False;

 

Re:Free vs Destroy?


  This would be better in the objectpascal newsgroup, but to address your
question: calling Free causes Destroy to be called, after the Self variable
is checked that it is not nil.  That's all that Free is.  If you're getting
exceptions using Free, you've got another problem.  Also, neither Free nor
Destroy set a variable to nil afterwards, so the Assigned check will return
True after Freeing or Destroying an object.  That's what FreeAndNil was
invented for.  (It might be a better practice to write code that does not
have to check if the variable is nil, if possible.)
-Howard

Re:Free vs Destroy?


When I call Free, the execution never stop at the debug stop I set in
Destroy. Why?
Quote
Howard Moon wrote:
>   This would be better in the objectpascal newsgroup, but to address your
> question: calling Free causes Destroy to be called, after the Self variable
> is checked that it is not nil.  That's all that Free is.  If you're getting
> exceptions using Free, you've got another problem.  Also, neither Free nor
> Destroy set a variable to nil afterwards, so the Assigned check will return
> True after Freeing or Destroying an object.  That's what FreeAndNil was
> invented for.  (It might be a better practice to write code that does not
> have to check if the variable is nil, if possible.)
> -Howard

Re:Free vs Destroy?


  I would need to see your class's declaration and implementation of
Destroy, and how you call it in code, to begin to guess.  It's possible that
the variable is nil already and so Destroy on that variable does not need to
call Free.  It's also possible that you've left off the 'override' on your
Destroy declaration, or are calling Free on an ancestor class instance
instead of your class.  I can't tell without seeing the code.
-Howard

Re:Free vs Destroy?


Quote
"sims zhou" <s...@dol-esa.gov> wrote in message

news:3C9B9698.7060107@dol-esa.gov...

Quote
> When I call Free, the execution never stop at the debug stop I set in
> Destroy. Why?

If this is your own component or class, the most common reason for this is
forgetting to add "override" to the declaration of the Destroy method:

public
    procedure Destroy; override;

--
Wayne Niddery (Logic Fundamentals, Inc.)
RADBooks: http://www.logicfundamentals.com/RADBooks/delphibooks.html
In a tornado, even turkeys can fly. - unknown

Other Threads