Recent

Author Topic: PSA: Don't use FreeAndNil  (Read 16691 times)

Thaddy

  • Hero Member
  • *****
  • Posts: 16520
  • Kallstadt seems a good place to evict Trump to.
Re: PSA: Don't use FreeAndNil
« Reply #105 on: May 10, 2024, 05:39:57 am »
Double free usually means the programmer does not understand his own code.
That goes for my own code too in a few cases. If I detect it, I know I made a logic error.
Strongest reason I want them in plain sight and not hide them.
« Last Edit: May 13, 2024, 04:53:39 pm by Thaddy »
But I am sure they don't want the Trumps back...

PascalDragon

  • Hero Member
  • *****
  • Posts: 5855
  • Compiler Developer
Re: PSA: Don't use FreeAndNil
« Reply #106 on: May 12, 2024, 10:25:22 pm »
But FreeAndNil won't protect you if you have multiple references to the class instance, cause it will only set the one reference to Nil. In that sense it will give you a false sense of security.
Duplication in programming is as "good" as duplication in databases.

I don't consider having multiple references to a single object instance as “duplication”. Or would you consider having multiple pointers to some data blob as “duplication” as well (completly independant of OOP)?

440bx

  • Hero Member
  • *****
  • Posts: 4993
Re: PSA: Don't use FreeAndNil
« Reply #107 on: May 12, 2024, 11:06:08 pm »
I don't consider having multiple references to a single object instance as “duplication”. Or would you consider having multiple pointers to some data blob as “duplication” as well (completly independant of OOP)?
I would.  Of course it isn't the blob that is being duplicated but the references to it.   Every reference has to be managed independently and, most importantly, kept in synch with the other references.  That's a nasty source of problems that are a direct consequence of having multiple references (reference duplication) to the same item ("item" to avoid using the term object in this case as it would not be fitting.)
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Joanna from IRC

  • Hero Member
  • *****
  • Posts: 1317
Re: PSA: Don't use FreeAndNil
« Reply #108 on: May 12, 2024, 11:09:39 pm »
Quote
But FreeAndNil won't protect you if you have multiple references to the class instance, cause it will only set the one reference to Nil. In that sense it will give you a false sense of security.
It would be great if all pointers to anything that is freed would become nil automatically. Is there any way to do this?

I actually use multiple pointer references to things but have flags to avoiding freeing them more than once.
✨ 🙋🏻‍♀️ More Pascal enthusiasts are needed on IRC .. https://libera.chat/guides/ IRC.LIBERA.CHAT  Ports [6667 plaintext ] or [6697 secure] channel #fpc  #pascal Please private Message me if you have any questions or need assistance. 💁🏻‍♀️

440bx

  • Hero Member
  • *****
  • Posts: 4993
Re: PSA: Don't use FreeAndNil
« Reply #109 on: May 13, 2024, 01:02:32 am »
Quote
But FreeAndNil won't protect you if you have multiple references to the class instance, cause it will only set the one reference to Nil. In that sense it will give you a false sense of security.
It would be great if all pointers to anything that is freed would become nil automatically. Is there any way to do this?
No, there isn't.

I actually use multiple pointer references to things but have flags to avoiding freeing them more than once.
A very bad habit you should try to lose asap.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: PSA: Don't use FreeAndNil
« Reply #110 on: May 13, 2024, 01:16:57 am »
Quote
But FreeAndNil won't protect you if you have multiple references to the class instance, cause it will only set the one reference to Nil. In that sense it will give you a false sense of security.
It would be great if all pointers to anything that is freed would become nil automatically. Is there any way to do this?
No, there isn't.
[sarcasm]
Yes there is, do by code reboot and reload app = voilá, all pointers are freed :D
[/sarcasm]
I actually use multiple pointer references to things but have flags to avoiding freeing them more than once.
A very bad habit you should try to lose asap.
100% agreed!
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

cdbc

  • Hero Member
  • *****
  • Posts: 1814
    • http://www.cdbc.dk
Re: PSA: Don't use FreeAndNil
« Reply #111 on: May 13, 2024, 07:48:31 am »
Hi
@Joanna:
Quote
I actually use multiple pointer references to things but have flags to avoiding freeing them more than once.
Someday, sometime, that is going to *bite* you in the ass! I learnt that the hard way  %)
@440bx is right, loose that habbit, like yesterday...  8-)
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

Thaddy

  • Hero Member
  • *****
  • Posts: 16520
  • Kallstadt seems a good place to evict Trump to.
Re: PSA: Don't use FreeAndNil
« Reply #112 on: May 13, 2024, 04:58:55 pm »
Actually Johanna should work around her problem by always instantianting through interface references instead of direct class instances, let an interface control the life time.
COM Interfaces are reference counted, so there is less of an issue if you maintain proper programmer discipline. The smart pointer code on this forum, with different implementations by ASerge, avk too I think, and me, pick and choose, would help her to achieve that with few hassle.
And you do not need FreeAndNil at all...
« Last Edit: May 13, 2024, 05:13:39 pm by Thaddy »
But I am sure they don't want the Trumps back...

cdbc

  • Hero Member
  • *****
  • Posts: 1814
    • http://www.cdbc.dk
Re: PSA: Don't use FreeAndNil
« Reply #113 on: May 13, 2024, 05:21:47 pm »
Hi
@Thaddy: I couldn't agree more, with you...
Though, I sometimes feel it a bit /longwinded/, to try and turn people on to / around to using interfaces... %) Most seem to have trouble, 'keeping their grubby hands off the class' when using interfaces  :D
Personally, I love them (most of the time).
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

Joanna from IRC

  • Hero Member
  • *****
  • Posts: 1317
Re: PSA: Don't use FreeAndNil
« Reply #114 on: May 14, 2024, 02:20:27 pm »
It’s not perfect solution but it’s the only way I can think of tho avoid redundancy at the current time. It’s quite possible it will be changed in future. I change things when I find better solution.
✨ 🙋🏻‍♀️ More Pascal enthusiasts are needed on IRC .. https://libera.chat/guides/ IRC.LIBERA.CHAT  Ports [6667 plaintext ] or [6697 secure] channel #fpc  #pascal Please private Message me if you have any questions or need assistance. 💁🏻‍♀️

Handoko

  • Hero Member
  • *****
  • Posts: 5387
  • My goal: build my own game engine using Lazarus
Re: PSA: Don't use FreeAndNil
« Reply #115 on: May 14, 2024, 02:25:54 pm »
It is okay for using not perfect solution. The most important thing is, we should keep learning new things, try new things, later use the better things.

In the past I used and could only use TDbf. Now I'm using SQLite. In the future I probably will use PostgreSQL.

 

TinyPortal © 2005-2018