Recent

Author Topic: Added a smart pointers subject to the wiki  (Read 1332 times)

Thaddy

  • Hero Member
  • *****
  • Posts: 18344
  • Here stood a man who saw the Elbe and jumped it.
Added a smart pointers subject to the wiki
« on: October 04, 2025, 12:00:32 pm »
I added a smart pointer entry to the wiki that describes three different ways to create smartpointers.
Please review.
https://wiki.freepascal.org/smartpointers

Comments and additions are welcome.
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

cdbc

  • Hero Member
  • *****
  • Posts: 2466
    • http://www.cdbc.dk
Re: Added a smart pointers subject to the wiki
« Reply #1 on: October 04, 2025, 12:29:52 pm »
Hi Thaddy
That's a very good write-up, "Me Likey" =^
Clear & to the point -- Well done mate  8)
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6 -> FPC 3.2.2 -> Lazarus 4.0 up until Jan 2025 from then on it's both above &: KDE6/QT6 -> FPC 3.3.1 -> Lazarus 4.99

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12531
  • FPC developer.
Re: Added a smart pointers subject to the wiki
« Reply #2 on: October 04, 2025, 12:36:55 pm »
I see RAII like behaviour in these examples and code, but not really ARC.

Nowhere references are passed around and refcounted.

Thaddy

  • Hero Member
  • *****
  • Posts: 18344
  • Here stood a man who saw the Elbe and jumped it.
Re: Added a smart pointers subject to the wiki
« Reply #3 on: October 04, 2025, 12:55:06 pm »
Nowhere references are passed around and refcounted.
Well, it works because all three examples are refcounted types. Makes them not only RAII but ARC too.
Or is that not what you mean?
« Last Edit: October 04, 2025, 03:12:20 pm by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12531
  • FPC developer.
Re: Added a smart pointers subject to the wiki
« Reply #4 on: October 04, 2025, 05:06:55 pm »
So what is the refcounted type in the advanced record example ? I see a free at the end, but no reference counting, so big fat exception if you add the object to some global list since that will lead to a double free.

The other two interface based once have reference count somewhere, but non of the demos really demonstrate that by increasing references beyond the one instance used locally in that procedure. All three examples seem to only care about skipping finalization code for the simplest case.

That has its place, certainly, (*) but is it really ARC/Smartpointer?

(*) e.g. in web request processing. Anything that simplifies the endless try-finally blocks is welcome.





Thaddy

  • Hero Member
  • *****
  • Posts: 18344
  • Here stood a man who saw the Elbe and jumped it.
Re: Added a smart pointers subject to the wiki
« Reply #5 on: October 04, 2025, 05:24:38 pm »
So what is the refcounted type in the advanced record example ? I see a free at the end, but no reference counting, so big fat exception if you add the object to some global list since that will lead to a double free.

The other two interface based once have reference count somewhere, but non of the demos really demonstrate that by increasing references beyond the one instance used locally in that procedure. All three examples seem to only care about skipping finalization code for the simplest case.

That has its place, certainly, (*) but is it really ARC/Smartpointer?

(*) e.g. in web request processing. Anything that simplifies the endless try-finally blocks is welcome.

The managed record itself is refcounted. That's how they work.
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12531
  • FPC developer.
Re: Added a smart pointers subject to the wiki
« Reply #6 on: October 04, 2025, 08:34:51 pm »
Well, I can't seem to get it to work then. I didn't see reference counting when I compiled to assembler (other than for strings), and assigning an instance to another variable of the type crashes at finalization:

Code: [Select]
program smartrec;
{ A simple smart pointer based on advanced records.
  Works for classes with parameterless constructors }
{$mode delphi}
uses classes;
type
  TSmartPointer<T:class, constructor> = record
  FInvoke:T;
  public
  class operator initialize(var value: TSmartPointer<T>);
  class operator finalize(var value: TSmartPointer<T>);
  function invoke:T;
  end;
 
  class operator TSmartPointer<T>.initialize(var value: TSmartPointer<T>);
  begin
    Value.FInvoke := T.Create;
  end;
 
  class operator TSmartPointer<T>.finalize(var value: TSmartPointer<T>);
  begin
    Value.FInvoke.Free;
  end;
 
  function TSmartPointer<T>.invoke:T;
  begin
    Result := FInvoke;
  end;

Type  TSmartStringList=TSmartPointer<TStringlist>;
var
  smList:TSmartStringList; 
  sm2list:TSmartStringList;
  List:TStringlist;
begin
  { Use the list }
  smList.Invoke.Add('some text');
  sm2list:=smlist;                                  // copy instance. If reference counted this should be fine. If only initialized and finalizated it will bomb.
  { It is also possible to assign to an
    uninitialized stringlist variable }
  list := smList.Invoke;
  writeln(List.Text);
  readln;
  { no leaks }
end.


And it bombs.

jamie

  • Hero Member
  • *****
  • Posts: 7308
Re: Added a smart pointers subject to the wiki
« Reply #7 on: October 04, 2025, 09:01:58 pm »
Something tells me that using the "Class Copy" operator may help in that regard to clean up the existing one before overwriting it with a new one?

Jamie
The only true wisdom is knowing you know nothing

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12531
  • FPC developer.
Re: Added a smart pointers subject to the wiki
« Reply #8 on: October 04, 2025, 10:16:47 pm »
Something tells me that using the "Class Copy" operator may help in that regard to clean up the existing one before overwriting it with a new one?

But that might make it more dependent of the type you nest in it, and less general. Depending on if it is a class, an interface, an array/record etc. Maybe things are possible (e.g. with gettypekind, ismanagedtype etc), but I feel this is all incomplete.
« Last Edit: October 04, 2025, 10:19:31 pm by marcov »

Thaddy

  • Hero Member
  • *****
  • Posts: 18344
  • Here stood a man who saw the Elbe and jumped it.
Re: Added a smart pointers subject to the wiki
« Reply #9 on: October 05, 2025, 10:31:35 am »
Yes, the record based approach needs addref and copy. I had them in, but left them out. Will  put them back in.
Anyway, I only use the last (ref) solution.
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

avra

  • Hero Member
  • *****
  • Posts: 2566
    • Additional info
Re: Added a smart pointers subject to the wiki
« Reply #10 on: October 05, 2025, 05:56:21 pm »
I wish you kept naming as in your initial example from 2019:
Auto<> instead of TSmartPointer<>

If FPC ever gets an official unit for smart pointers, I would like to see "auto" naming because I prefer naming by functionality over naming by implementation detail as I find it much more clear.
« Last Edit: October 05, 2025, 05:58:44 pm by avra »
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

Thaddy

  • Hero Member
  • *****
  • Posts: 18344
  • Here stood a man who saw the Elbe and jumped it.
Re: Added a smart pointers subject to the wiki
« Reply #11 on: October 05, 2025, 06:49:53 pm »
I thought "Auto" to be a good but maybe too C++ like idea. (_auto_ is indeed similar)
Whereas "SmartPointer", although a faux description, seems well settled in the Object Pascal community.
Since I will have to do the repairs on the first entry, I might return to Auto too (or my very first: Box).
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

avra

  • Hero Member
  • *****
  • Posts: 2566
    • Additional info
Re: Added a smart pointers subject to the wiki
« Reply #12 on: October 06, 2025, 03:34:02 pm »
I thought "Auto" to be a good but maybe too C++ like idea. (_auto_ is indeed similar)
I didn't know that, so not so C++ like, I guess. It simply sounded more natural and more self explaining when looking at examples to a beginner. Smart pointers term needs a little research before understanding.

Just my 2 cents on what resonates better with my ears.  ;)
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

Thaddy

  • Hero Member
  • *****
  • Posts: 18344
  • Here stood a man who saw the Elbe and jumped it.
Re: Added a smart pointers subject to the wiki
« Reply #13 on: October 07, 2025, 01:47:39 pm »
Nehhh, we'll make it auto, it's  us. :D 8-)
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

nixbody

  • New Member
  • *
  • Posts: 15
Re: Added a smart pointers subject to the wiki
« Reply #14 on: October 07, 2025, 02:55:28 pm »
I use function references for objects/pointers with ARC and call the type TShared (inspired by std::shared_ptr) for objects and TSharedPointer for pointers. TAuto resembles to me a variable with automatic storage duration (a C/C++ term I believe) which is a block-scoped variable that cease to exist when goes out of its scope which is more akin to std::unique_ptr (a better version of now deprecated std::auto_ptr).
« Last Edit: October 07, 2025, 02:58:03 pm by nixbody »

 

TinyPortal © 2005-2018