Yes, I'm going to bring this up because the codebase I'm working on PROVES MY FRIGGIN' POINT! (Sorry for yelling....)

Yes, I'm going to bring this up because the codebase I'm working on PROVES MY FRIGGIN' POINT!  (Sorry for yelling....)

Okay -- the programmer who wrote this stuff uses FreeAndNil all over.   And as a result, I have to thoroughly investigate each one to figure out why.  Is it because they were just duped by the FreeAndNillers?  Or is there actually a reason for the pointer to be set to nil?  

Sometimes there is a reason, and sometimes there isn't.  But I have to check every single time.

Great.

Fire away, I don't care.  ;-)

Comments

  1. It is a matter of preference.
    /flogdeadhorse

    ReplyDelete
  2. Well, I ALWAYS use FAN. For a very simple reason - it makes the code crash if I make a mistake and try to access the freed resource when I shouldn't have.

    ReplyDelete
  3. Lars --

    LOL -- sorry, I had to get it off my chest.

    ReplyDelete
  4. Well, I hope you FreeAndNillers at least have pity for the people that have to maintain your code.  ;-)

    ReplyDelete
  5. I clean up my kitchen after lunch. For the same reason I clean up my pointers when I am done with them. Pointers pointing to something where they shouldnt point to are pointless.

    ReplyDelete
  6. Nick Hodges I don't see why FAN would make it hard to maintain any code.

    My typical allocation pattern (and I think it covers about 99.99% of my code) is

    obj := TSomething.Create;
    try
      ...
    finally FreeAndNil(obj); end;

    It will always be like this. There are no hidden meanings. It's just a way that helps the machine tell me that there's something wrong with my code if I make a mistake.

    ReplyDelete
  7. Primož Gabrijelčič that's very true and useful indeed.

    ReplyDelete
  8. I use FreeAndNil, but only when I actually have to nil, otherwise I stick with Free. I can't say I really have an issue with it...

    ReplyDelete
  9. Primož Gabrijelčič If that were all that this code did, then there wouldn't be a problem.  ;-)

    ReplyDelete
  10. I'm not quite consistent in how I use FreeAndNil, but generally speaking - if it is a property field, I FreeAndNil - if it is a local variable, I don't.

    On the other hand - if all variables were reference counted, assigning a nil would eventually mean a free.

    ReplyDelete
  11. I also use FreeAnNil within my destructors.

    ReplyDelete
  12. I only use FreeAndNil when I have to as well.  But I never have to.  ;-)

    ReplyDelete
  13. I almost never use FreeAndNil :) If something will fail it fails anyway!

    ReplyDelete
  14. try/except? ;)

    As mentioned above, in these cases I .Free - but if FGooglePlusComment is a field accessed through a property getter, I FreeAndNil.

    ReplyDelete
  15. I guess it is a result of passing a lot of objects around between threads, and preferring things to go out immediate with a nil bang, instead of the chance that a dangling reference may work for a while.

    ReplyDelete
  16. It seems you can call members of destroyed objects as long as the memory is reasonably intact.  We found out after upgrading from EurekaLog 6 to EurekaLog 7, which has protection to catch such fun.

    ReplyDelete
  17. I use FreeAndNil because it's painful enough to have to release yourself the usual objects in the XXIst century  

    ReplyDelete
  18. One bad thing in FreeAndNil - it first make pointer nil, and after that calls Free.  It should be called NilAndFree :)

    ReplyDelete
  19. Lars Fosdal Indeed, that's why I tend to use it. Unless I forget, which is too frequent... old habbits die hard. I also tend to use it on local variables, as it's easier to catch when something goes wrong then. Wouldn't be an issue if Delphi had RAII but...

    ReplyDelete
  20. I had to lookup FreeAndNil in the documentation to understand what all the fuss is about because I've never used it. :-)

    ReplyDelete
  21. Kenneth -- you are a wise and insightful developer.  ;-)

    ReplyDelete

Post a Comment