Board index » delphi » Advanced: Free doesn't assign Nil?

Advanced: Free doesn't assign Nil?

Edward,

Free simply wraps to a destroy call with a single check to see if the
pointer is nil.

I suppose you'll have to ask Borland why they implement there objects
the way they do. Personally I am content with the way they handle their
pointers.

Say you have an object reference declared in a var section (on the
stack) your reference variable contains whatever garbage happens to be
on the stack. You have to initialize the pointer to nil under certain
operations. i.e. tBookMark.

I don't see any difference here.

At another level freemem and dispose do not set pointers to nil, whereas
realoc does.

Garry Kernan

Quote
Edward Garson wrote:

> Greetings,

>         I recently tried to 'free' a (created) object twice in a short
> experimental program. Two subsequent calls to Free for a created object
> results in an access violation. If I free the object, and then assigned nil
> to the object (pointer), then no access violation occurred. My question is,
> why doesn't Delphi assign nil to an object after freeing it?

> Regards

> Edward Garson
> Flairbase Inc.
> edward@NO_SPAMshuya.ml.org

 

Re:Advanced: Free doesn't assign Nil?


Because that would be too easy :-)

Quote
Edward Garson wrote in message <01bce3db$e316da80$041410ac@edward>...
>Greetings,
>My question is,
>why doesn't Delphi assign nil to an object after freeing it?

Re:Advanced: Free doesn't assign Nil?


Greetings,

        I recently tried to 'free' a (created) object twice in a short
experimental program. Two subsequent calls to Free for a created object
results in an access violation. If I free the object, and then assigned nil
to the object (pointer), then no access violation occurred. My question is,
why doesn't Delphi assign nil to an object after freeing it?

Regards

Edward Garson
Flairbase Inc.
edward@NO_SPAMshuya.ml.org

Re:Advanced: Free doesn't assign Nil?


No.

All the free does is release the associated memory/hooks.  Just as with any
other pointer, it is the programmer's responsibility to set the pointer to nil
if that is required for your needs.

Quote
Edward Garson wrote:
> Greetings,

>         I recently tried to 'free' a (created) object twice in a short
> experimental program. Two subsequent calls to Free for a created object
> results in an access violation. If I free the object, and then assigned nil
> to the object (pointer), then no access violation occurred. My question is,
> why doesn't Delphi assign nil to an object after freeing it?

> Regards

> Edward Garson
> Flairbase Inc.
> edward@NO_SPAMshuya.ml.org

Re:Advanced: Free doesn't assign Nil?


An Object  doesn't know about Pointers that reference it!

Try

  Setting the Pointer to Self in the constructor
  and nil in the destructor

  be careful to do this only for objects you create only one instance at a
time!

Peter Michael Franta
pfra...@world.net
Relate Systems Consultants P/L

Quote
Edward Garson wrote in message <01bce3db$e316da80$041410ac@edward>...
>Greetings,

>why doesn't Delphi assign nil to an object after freeing it?

Other Threads